📜  Angular PrimeNG 芯片组件(1)

📅  最后修改于: 2023-12-03 15:29:23.810000             🧑  作者: Mango

Angular PrimeNG Chip 组件

介绍

Angular PrimeNG 是一个开源的 UI 组件库,它提供了各种丰富的组件用于构建现代化的 Web 应用程序。其中之一就是 Chip 组件,Chip 可以用来呈现数据和标记,也可以用来选择,输入和过滤信息,能够让用户更加高效的进行操作和交互。

特性
  • 可以显示文本、图像和删除按钮
  • 支持可选和只读模式
  • 提供丰富的样式和配置选项
  • 支持多种事件绑定
  • 可以使用 ngModel 和表单
示例代码
<p-chips [(ngModel)]="values" [separator]="','" [field]="'name'"></p-chips>
export class AppComponent {
  values: any[] = [
    { name: 'Apple' },
    { name: 'Banana' },
    { name: 'Orange' }
  ];
}
详细介绍
基本用法

添加 Chip 组件非常简单,只需引入 ChipsModule 模块,并在 HTML 模板中添加 <p-chips> 标签即可。

import { ChipsModule } from 'primeng/chips';

@NgModule({
  imports: [
    ChipsModule
  ]
})
export class AppModule { }
<p-chips></p-chips>
绑定数据

可以通过双向数据绑定或单向数据绑定的方式来绑定数据。使用 [(ngModel)] 可以实现双向绑定,使用 [ngModel] 可以实现单向绑定。例如:

<p-chips [(ngModel)]="values"></p-chips>
<p-chips [ngModel]="values"></p-chips>
多字段绑定

如果要显示 Chip 内的多个字段,可以使用 field 属性来绑定。例如:

values: any[] = [
  { name: 'Apple', color: 'Red' },
  { name: 'Banana', color: 'Yellow' },
  { name: 'Orange', color: 'Orange' }
];
<p-chips [(ngModel)]="values" [field]="'name,color'"></p-chips>
自定义分隔符

默认情况下,Chip 组件使用的是逗号作为分隔符。如果要更改分隔符,可以使用 separator 属性。例如:

<p-chips [(ngModel)]="values" [separator]="';'"></p-chips>
自定义模板

可以使用 itemTemplate 属性自定义 Chip 的显示模板。例如:

<p-chips [(ngModel)]="values" [field]="'name'" [itemTemplate]="customItemTemplate"></p-chips>

<ng-template #customItemTemplate let-item>
  <div class="custom-item">
    {{item.name}}
    <span class="custom-item-icon" (click)="removeItem(item)">X</span>
  </div>
</ng-template>
禁用状态

可以使用 disabled 属性来禁用 Chip 组件。

<p-chips [(ngModel)]="values" disabled="true"></p-chips>
只读状态

可以使用 readonly 属性来设置 Chip 组件为只读状态。

<p-chips [(ngModel)]="values" readonly="true"></p-chips>
样式

可以使用 CSS 对 Chip 组件进行自定义样式。

.ui-chips {
  &.my-chips {
    .ui-chips-token {
      background-color: #ffcccc;
      border-color: #ff6666;
      color: #cc0000;
      margin: 3px;
      padding: 3px 6px;

      .ui-chips-token-icon {
        color: #cc0000;
      }

      &:hover {
        background-color: #ffaaaa;
        border-color: #ff6666;
        color: #cc0000;
      }

      &:focus {
        background-color: #ffaaaa;
        border-color: #ff6666;
        color: #cc0000;
      }

      &:not(:first-child) {
        margin-left: 0;
      }

      &:not(:last-child) {
        margin-right: 0;
      }

      &:last-child {
        .ui-chips-token-icon {
          display: none;
        }
      }

      &.ui-state-disabled {
        background-color: #f0f0f0;
        border-color: #e9e9e9;
        color: #bbb;
      }

      &.ui-state-highlight {
        background-color: #ffaaaa;
        border-color: #ff6666;
        color: #cc0000;

        .ui-chips-token-icon {
          color: #cc0000;
        }
      }

      &.ui-state-active {
        background-color: #ff6666;
        border-color: #ff6666;
        color: #fff;

        .ui-chips-token-icon {
          color: #fff;
        }
      }
    }

    .ui-chips-input-token {
      background-color: #fff;
      border-color: #ccc;
      color: #333;
      margin: 3px;
      padding: 3px 6px;

      &:hover {
        background-color: #fafafa;
        border-color: #ccc;
        color: #333;
      }

      &:focus {
        background-color: #fff;
        border-color: #4d90fe;
        box-shadow: none;
        color: #333;
      }

      &.ui-state-disabled {
        background-color: #f0f0f0;
        border-color: #e9e9e9;
        color: #bbb;
      }
    }
  }
}
组件API

以下是 Chip 组件的一些属性和方法。

Attributes

| Name | Type | Default | Description | | ------------- | -------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------- | | id | string | null | Unique identifier of the element. | | name | string | null | Name of the input element. | | placeholder | string | null | A hint to the user of what can be entered in the control. | | tabIndex | number | null | Index of the element in tabbing order. | | allowDuplicate| boolean | false | Whether duplicate entries are allowed. | | display | string | comma | Delimiter to use between multiple values. | | field | string | null | Name(s) of the property to display. Multiple fields should be separated by commas. | | max | number | null | Maximum number of chips allowed. | | maxlength | number | null | Maximum character count of chips input. | | readonly | boolean | false | Whether the component is readonly. | | tabindex | number | null | A tab stop can be disabled by setting tabindex to -1. | | separator | string | null | String to separate chips. | | style | string | null | Inline style of the element. | | itemTemplate | TemplateRef<ChipsItemTemplateContext> | null | Template of each item. | | disabled | boolean | false | Whether the component is enabled. | | selectionLimit| number | null | Maximum number of selected chips. | | dataKey | string | null | A property to uniquely identify a value. | | inputStyle | string | null | Inline style of the input field. | | inputId | string | null | Identifier of the input element. | | inputStyleClass| string | null | Style class of the input field. | | ariaLabel | string | aria-labelledby attribute value or label of the component, depending on the existence of dropdown icon buttons. | Aria label to assign to the component element. | | ariaLabelledBy| string | null | Aria labelled by |

Events

| Name | Type | Description | | --------- | ------------------------------- | ------------------------------------------------------------------------------------- | | onBlur | EventEmitter | Event is emitted when the input field loses focus. | | onFocus | EventEmitter | Event is emitted when the input field receives focus. | | onRemove | EventEmitter<Object> | Event is emitted when an item is removed. The removed item is sent with the event. | | onAdd | EventEmitter | Event is emitted when an item is added. | | onComplete| EventEmitter<KeyboardEvent>| Event is emitted when the user clicked enter key. | | onChipClick| EventEmitter<Object> | Event is emitted when user clicks on a chip element. The clicked item is sent with the event. |

Methods

| Name | Description | | ------------- | -------------------------------------------------| | add(event) | Adds a new chip. event is the original event. | | hasMax() | Returns true if the maximum number of chips allowed is reached. | | onKeydown(event) | Actions to be performed when the user types something in the chips input field. | | remove(chip: any) | Removes a single chip. |

结语

Angular PrimeNG Chip 组件提供了丰富的功能和配置选项,可以用来构建各种复杂的交互式应用程序。希望通过本文的介绍,能够让读者更加了解 Chip 组件的用法和特性,从而更好地开发 Web 应用程序。