PrimeNg 多选,默认全选

sas*_*dev 2 javascript typescript primeng angular

我有一个来自 PrimeNg 的多选项目。如果需要,我可以选择它们,但是默认情况下是否可以将它们全部选中?

<p-multiSelect [options]="userconfig" [(ngModel)]="selectedScopes" optionLabel="name"
    maxSelectedLabels=3 (onChange)="getCheckboxScope($event)" 
    selectedItemsLabel="{0} users selected" defaultLabel="select users..."> 
</p-multiSelect>
Run Code Online (Sandbox Code Playgroud)

小智 6

如果要选择所有项目,则将多选中选项 ( userconfig) 的所有值绑定到 ( selectedScopes) 组件自动检测属性selectedScopes等于userconfig值并选择所有项目。

例子:

export class MyModel {

    userconfig: SelectItem[];

    selectedScopes: any[];

    constructor() {
        // Options of the multiselect
        this.userconfig = [
            {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'}}
        ];
       // Select all items
       selectedScopes = [];
       userconfig.map((item) => selectedScopes.push(item.value));
    }

}
Run Code Online (Sandbox Code Playgroud)