启动多选预检项目

时间:2019-02-18 12:22:27

标签: angular primeng

我正在使用PrimeNG中的multiselect模块。我正在从文档中复制以下代码:

export class AppComponent {
  title = 'DataShit';
  cities1: SelectItem[];
  cities2: City[];
  selectedCities1: City[];
  selectedCities2: City[];

  constructor() {
    //SelectItem API with label-value pairs
    this.cities1 = [
      {label: 'New York', value: {id: 1, name: 'New York', code: 'NY'}},
      {label: 'Rome', value: {id: 2, name: 'Rome', code: 'RM'}},
      {label: 'London', value: {id: 3, name: 'London', code: 'LDN'}},
      {label: 'Istanbul', value: {id: 4, name: 'Istanbul', code: 'IST'}},
      {label: 'Paris', value: {id: 5, name: 'Paris', code: 'PRS'}}
    ];

    //An array of cities
    this.cities2 = [
      {name: 'New York', code: 'NY'},
      {name: 'Rome', code: 'RM'},
      {name: 'London', code: 'LDN'},
      {name: 'Istanbul', code: 'IST'},
      {name: 'Paris', code: 'PRS'}
    ];
  }
}

现在,我在组件中使用以下标记渲染cities2

<p-multiSelect [options]="cities2" [(ngModel)]="selectedCities2" defaultLabel="Select your city">
  <ng-template let-city pTemplate="item">
    {{city.name}}
  </ng-template>
</p-multiSelect>

现在我的问题是:我可以根据某些条件预先选中某些复选框吗?(例如:以“ S”开头的城市或其他过滤器)

1 个答案:

答案 0 :(得分:1)

是的,可以。

您需要将您选择的数据与[(ngModel)]="selectedCities2"绑定,这将允许您显示以multiselect填充的预先选择的数据。

例如-

selectedCities2 = ['NY', 'RM'];