1

I'm using the ng-multiselect-dropdown in Angular to create a multi-select drop-down as a custom header. What I cannot seem to figure out is if there is a way to set which values I would pre-selected in the list. Here is now my drop-down is coded:

<ng-template *ngIf="column.dropdown" let-column="column" let-sort="sortFn" 
             ngx-datatable-header-template>
  <ng-multiselect-dropdown [placeholder]="column.name"
             class="d-inline-block" [data]="this[column.prop]"
             [settings]="dropdownSettings">
  </ng-multiselect-dropdown>
</ng-template>

Is there a property I can set that will pre-select certain values in the list? I haven't been able to find any documentation on how to do this. Thanks in advance.

1
  • can you provide us a plunker, jsfiddle or something like that, please? Commented Jul 12, 2018 at 18:44

7 Answers 7

6

Here, the ng-multiselect-dropdown pre-selection option value binds with ngModel and also I have shown the validation for ng-multiselect-dropdown:

<ng-multiselect-dropdown
    formControlName="location"
    id="location"
    [data]="supplierList"
    [(ngModel)]="selectedSupplier"
    [settings]="supplierDropDownSettings"
    (onSelect)="onLocationSelect($event)">
</ng-multiselect-dropdown>
<div *ngIf="submitted && formGroup.controls['location'].invalid"
    class="text-danger mt-1">
    <div *ngIf="formGroup.controls['location'].errors.required">
                            This filed is required
    </div>
</div>
public selectedSupplier: Array<SupplierModal> = [];
public warehouse: SupplierModal = { "id": 1, "name" : Company }
this.selectedSupplier = new Array<SupplierModal>();
this.selectedSupplier.push(this.warehouse);
Sign up to request clarification or add additional context in comments.

1 Comment

this.myForm = this.fb.group({ city: [this.selectedItems] }); Setting the selected item through formGroup fixed the issue for me. Follow exact steps in nileshpatel17.github.io/ng-multiselect-dropdown
2

Component HTML (With FormControl)

<ng-multiselect-dropdown
 [placeholder]="'Select Controller'"
 [data]="controllerList"
 formControlName="controller"
 [settings]="dropdownSettings"
 (onSelect)="onItemSelectController($event)"
 (onSelectAll)="onSelectAllController($event)" 
 (onDeSelect)="onItemDeSelectController($event)"  
 (onDeSelectAll)="onDeSelectAllController($event)" 
 (onDropDownClose)="onDropDownCloseController()">
</ng-multiselect-dropdown>

Component TS (With FormControl)

controllerList: any;
addRoleForm: FormGroup;

dropdownSettings: any = {
  singleSelection: false,
  idField: 'item_id',
  textField: 'item_text',
  selectAllText: 'Select All',
  unSelectAllText: 'UnSelect All',
  itemsShowLimit: 3,
  allowSearchFilter: true
};

this.addRoleForm = this.formBuilder.group({    
      controller: ['', Validators.compose(
        [Validators.required]
      )]          
});

ngAfterViewInit(){
  this.selectedConcrollers = [
    {item_id: 1, item_text: 'value'},
    {item_id: 1, item_text: 'value'}
  ];

  this.addRoleForm.patchValue({
    controller: this.selectedConcrollers 
  });
}

1 Comment

This is the right solution because using [(ngModel)]="selectedItems" within reactive form will be prohibited in future Angular (check warning in the console when doing that).
2

.push() didn't work for me, seems like component is not reacting to push event.. I had to replace whole array in order to make it work.

<ng-multiselect-dropdown [(ngModel)]="selected" ...></ng-multiselect-dropdown>

this.selected = [{item_id: 1, item_name: 'xxxxx'}, {item_id: 2, item_name: 'yyyyyy'}];

Comments

1

I suppose I should have read the API documentation more carefully. It actually specifies how to do this by setting the [(ngModel)] attribute on the ng-multiselect-dropdown tag.

2 Comments

so weird. this is working only if there is nothing wrapping the push statement...
In my case, item is not displayed 'selected' in drop-down list though it is displayed 'selected' in header / closed drop-down state. Why?
1
<ng-multiselect-dropdown [(ngModel)]="selected" ...></ng-multiselect-dropdown>

In typescript:

this.selected.push({item_id: 1, item_name: 'xxxxx'});

Comments

1

ts file:

dropdownList = [];
selectedItems = [];
dropdownSettings = {};
this.dropdownList = [
    { item_id: 1, item_text: 'Mumbai' },
    { item_id: 2, item_text: 'Bangaluru' },
    { item_id: 3, item_text: 'Pune' },
    { item_id: 4, item_text: 'Navsari' },
    { item_id: 5, item_text: 'New Delhi' }
];
this.selectedItems = [
    { item_id: 3, item_text: 'Pune' },
    { item_id: 4, item_text: 'Navsari' }
]; 

html file:

<ng-multiselect-dropdown
  [placeholder]="'custom placeholder'"
  [data]="dropdownList"
  [(ngModel)]="selectedItems"
  [settings]="dropdownSettings"
>
</ng-multiselect-dropdown>

Source: https://www.npmjs.com/package/ng-multiselect-dropdown

1 Comment

It makes no sense to have to provide the item_text for the selected items, the component already has them in dropDownList. I only keep ids in the backend, not the names. This is not a very good component.
0

You can still use ngModel inside the formGroup if its a standalone property or else it will give error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.