简体   繁体   English

如何预选 PrimeNG Checkbox 项目?

[英]How to preselect PrimeNG Checkbox items?

I am not able to get already selected item.我无法获得已经选择的项目。 In this code in rp which is an array of type Permission which has one element in it , So basically that value should be selected when I load this div.在 rp 中的这段代码中,它是一个 Permission 类型的数组,其中有一个元素,所以基本上当我加载这个 div 时应该选择该值。 What will be the mistake?会出现什么错误?

This is My HTML:-这是我的 HTML:-

<div class="gapRowSmall displayFlex flexColumn mb-small" *ngIf="(permissions$ | async)! as permissions">
    <div *ngFor="let permission of permissions" class="p-field-checkbox">
        <p-checkbox [value]="permission" [(ngModel)]="rp" class=" mr-small"></p-checkbox>
        <label>{{permission.name}}</label>
    </div>
    <div class="displayFlex flexJustifyEnd">
        <p-button type="submit" label="Save" styleClass="primary" (onClick)="savePermissions()"
            [appSubmitIndicator]="(isSubmitInProgress$ | async)!"></p-button>
    </div>
</div>

This is My ts file:-这是我的 ts 文件:-

permissions$ = this.store.select(permissionSelector)
    .pipe(takeUntil(this.permissionManagementSubject));

rp: Permission[] = [{ name: 'Create New Transitions', id: 'a45d7806-fbf8-4df7-8248-6f636288ff23' },];

The item in your rp array must completely match one of the items in your Observable permissions array. rp数组中的项目必须与 Observable 权限数组中的项目之一完全匹配。 If not all the fields in the object match, then it will not be selected.如果不是对象中的所有字段都匹配,则不会选择它。

So if the permissions would be loaded in the constructor like this:因此,如果权限将像这样在构造函数中加载:

  constructor() {
    this.permissions$ = of([
      {name: "aaaaa", id: '123456-789012'},
      {name: "bbbbb", id: '223456-789012'},
      {name: "ccccc", id: '323456-789012'}
    ])
  }

and the selected permissions in rp would be :并且在 rp 中选择的权限将是:

  rp: Permission[] = [
    { name: 'aaaaa', id: '123456-789012' },
    { name: 'bbbbb', id: '223456-' }, // This one is incomplete, thus no match
  ];

Then the second one, will not be selected although partially it is matching.然后第二个,虽然部分匹配,但不会被选中。

I've a working example of this in stackblitz.我在 stackblitz 中有一个工作示例。 https://stackblitz.com/edit/primeng-checkbox-demo-9rhzru?file=src/app/app.component.ts https://stackblitz.com/edit/primeng-checkbox-demo-9rhzru?file=src/app/app.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM