I am using MultiSelect control from PrimeNg (PrimeNg Multiselect) & Angular4 with a Reactive Form, and I want to keep some my items default selected during form loading.
HTML
<div class="col-md-4">
<span translate>settings.account.webhook.label.additional.variables</span>
<p-multiSelect [options]="sensorsList" (onChange)="changeSensorsList($event)"
formControlName="selectedSensorsList" name="selectedSensorsList" [panelStyle]="{minWidth:'20em'}"></p-multiSelect>
</div>
Here is my TypeScript code
sensorsList = [
{ label: 'lat', value: {'id': 0, 'itemName': 'lat', 'selected': false }},
{ label: 'lng', value: {'id': 1, 'itemName': 'lng' , 'selected': false}},
{ label: 'address', value: {'id': 2, 'itemName': 'address' , 'selected': false}},
{ label: 'origin', value: {'id': 3, 'itemName': 'origin' , 'selected': false}}
];
this.addEditWebhookForm = this._formBuilder.group({
name: [(_webhookToEdit) ? _webhookToEdit['name'] : '', [Validators.required]],
selectedSensorsList: []
});
HTML
You need to set a ngModel in your primeng component
<div class="col-md-4">
<span translate>settings.account.webhook.label.additional.variables</span>
<p-multiSelect [options]="sensorsList" (onChange)="changeSensorsList($event)"
formControlName="selectedSensorsList" name="selectedSensorsList" [panelStyle]="{minWidth:'20em'}" [(ngModel)]="selectedDefault"></p-multiSelect>
</div>
TS
selectedDefault= [];
sensorsList.map((item) => selectedDefault.push(item.value));
Right here I'm selecting all of them.
You will also have to udpate the sensorlist object to changed the value selected to true