matsura / ng2-dual-list-box

A dual list box component for Angular 4
http://ng2-duallistbox-demo.surge.sh/
28 stars 20 forks source link

How show default selected values? #6

Closed lazaro3487 closed 7 years ago

lazaro3487 commented 7 years ago

First, thanks for that component.

Is there any way for showing defaults values present in the model variable?

I need to edit the permissions of one user. How that component shows the current permissions of that user for droping or append permissions?

Regards.

matsura commented 7 years ago

@lazaro3487 If I understood you correctly, you need to preselect some values. To do this, you can use ngModelor formControlName. You should pass it an array of values. These values correspond to the valueFieldof your objects(for example IDs).

For example, if you have your permissions object like this:

{ name: 'Permission 1', id: 1 }, you should pass an array of IDs to the ngModelor formControlName

lazaro3487 commented 7 years ago

Thanks for your response but that does not works.

the component is created like this, but when userGroup.permissions has some default values the component does not shows that.

The userGroup var is loaded inside ngOnInit of my component when a want to edit the object, any help???

<ng2-dual-list-box id="permission" name="permission" [data]="permissions" valueField="id" textField="name" [(ngModel)]="userGroup.permissions"

permission="ngModel" required>

Regards.

matsura commented 7 years ago

@lazaro3487 Ok, since I don't know how your data looks I'm going to guess. Since your permissions array consist of objects with an idand name property I suppose your userGroup.permissions has the same structure. You should do something like this.

selectedPermissions: string[] = userGroup.permissions((permission: { id: string, name: string }) => permission.id);

and then in the template:

<ng2-dual-list-box id="permission" name="permission" [data]="permissions"
valueField="id"
textField="name"
[(ngModel)]="selectedPermissions"
#permission="ngModel" required>

Basically, the ngModelhas to be a one dimensional array of values.

matsura commented 7 years ago

Closed due to no answer.