简体   繁体   English

Angular2 组件 p-multiSelect Primeeng

[英]Angular2 component p-multiSelect Primeng

In template user.component.html i use component of Primeng在模板 user.component.html 中,我使用 Primeeng 的组件

<p-multiSelect name="roles_id" [(ngModel)]="selectedRoles" [options]="user.roles"></p-multiSelect>

When load the input data, how show selected values加载输入数据时,如何显示选定的值

This code in user.component.ts user.component.ts 中的这段代码

export class UserComponent implements OnInit {

private selectedStores: string[];

constructor(private roleService: RoleService){

     this.roleService.getRoleList().subscribe(response=>{this.user.roles=response.body.data.map(function(value){
            return {label:value.name, value: value.id};
        });})

    this.selectedRoles=['1','2'];
}
ngOnInit() {
    this.user={
         roles: [],
    };
}
}

But in interface multiselect show null,null , if i click multiselect the two items selected.但是在界面多选显示 null,null ,如果我点击多选所选的两个项目。 How to show label instead null,null ?如何显示标签而不是 null,null ?

Did you try to display the resulting user.roles in a console.log, for example?例如,您是否尝试在 console.log 中显示生成的 user.roles?

If the labels show up as null, something is wrong with the options object.如果标签显示为空,则选项对象有问题。 It has to be an array of objects key/values, so that you can iterate through it and display in the component.它必须是一个对象键/值数组,以便您可以遍历它并在组件中显示。 This is one example I have in a project I'm working on:这是我在一个项目中的一个例子:

columns = [
  { value: 'id',         label: 'ID' },
  { value: 'comment',    label: 'Comments' },
  { value: 'updated_at', label: 'Last Update' },
  { value: 'enabled',    label: 'Enabled' }
];

And apparently you're assigning an empty array to your user object, which may override the previous definition in the constructor.显然,您正在为用户对象分配一个空数组,这可能会覆盖构造函数中的先前定义。 The constructor runs before ngOnInit.构造函数在 ngOnInit 之前运行。 You can initialize user.roles as empty and assign data to it in ngOnInit().您可以将 user.roles 初始化为空并在 ngOnInit() 中为其分配数据。

Hope it helps.希望能帮助到你。

Funny enough, through your problem I found my solution.有趣的是,通过你的问题,我找到了我的解决方案。 My issue was to create a data-table and when a row is selected to display the already assigned roles of a user.我的问题是创建一个数据表,并在选择一行以显示用户已分配的角色时。 So i'll break it down for you.那我给你分解一下。 Hope it helps you to figure out yours.希望它可以帮助你弄清楚你的。

  1. My staff Model我的员工模型

    export interface IStaff { staffId: number; userRoles: string[]; }
  2. Selecting the roles选择角色

    export class UserComponent implements OnInit { staff: IStaff; staffRoles: SelectItem[]; selectedRoles: string[] = []; tempRoles: string[] = []; constructor(private roleService: RoleService){} ngOnInit() { this.roleService.getRoleList().subscribe(response=> { let tempRoles: IStaff[]; this.staffRoles = []; tempRoles = response; tempRoles.foreach(el => { this.staffRoles.push({ label: el.name, value: el.id }); }); }); staff.userRoles.forEach(el => { let value: any = el; this.tempRoles.push(value.roleName); this.selectedRoles = this.tempRoles; }); // then set the tempRoles back to null to prevent readding the existing roles this.tempRoles = []; }
  3. This should then work这应该可以工作

    <p-multiSelect name="roles_id" [(ngModel)]="selectedRoles" [options]="staffRoles"></p-multiSelect>

如果您将primeng 升级到版本^4.0.0,它将起作用

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

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