简体   繁体   中英

How to bind data of multiselect dropdown of primeng Angular

I am using primeng Multiselect dropdown in a form.

While clicking on the edit button I want to display the existed data in all the field. for Input text it is displaying as I am using [(ngModel)] and I have tried the same for Multiselect dropdown as well but it is not working.

so I am unable to bind the values of multiselect dropdown. I have tried using the for Loop but the values are not coming in the field.

      <div *ngFor ="let role of resource.roles;">
        {{role.name}}
        <p-multiSelect
        [options]="resourceRoles | dropdownToValuePipe"
        placeholder="Select"
        name = "roles"
        id="roles"
        [(ngModel)]="role.name"
        ngModel
        required
        appendTo="body"
      >
      </p-multiSelect>
      </div>

角色字段是多选下拉菜单

With your current code, you are creating one multiselect per role the resource/user has. I assume this is not what you want, otherwise you would probably just use a normal dropdown.

So if you really want only one multiselect, you would do something like this:

  <p-multiSelect
    [options]="resourceRoles"
    optionLabel="name"
    placeholder="Select"
    name="roles"
    id="roles"
    [(ngModel)]="resource.roles"
    required
    appendTo="body"
  >
  </p-multiSelect>

I wasn't sure what the pipe was doing, you might not need it if you use optionLabel . I also removed the duplicate ngModel .

You might want to also play around with using dataKey="roleId" if the objects in resource.roles are not the same objects as in resourceRoles . This would match selections based on roleId instead of using object equality

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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