<ejs-listbox #listBoxDetails [dataSource]='data' [fields]='ddlFields' (created)="onCreated()"></ejs-listbox> |
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import { ListBoxComponent, FieldSettingsModel } from '@syncfusion/ej2-angular-dropdowns';
export class AppComponent {
@ViewChild('listBoxDetails', {static: true})
public ddlList: ListBoxComponent ;
// dataSource definition
public data: { [key: string]: Object }[] = [
{ text: 'Cabbage', type: 'Leafy and Salad', code: 'item1' },
//..
];
public ddlFields: FieldSettingsModel = { groupBy: 'type', text: 'text', value: 'code' };
onCreated(){
this.ddlList.selectItems(['Spinach']);
}
} |
<ejs-listbox #listBoxDetails [dataSource]='data' [selectionSettings]='selectionSettings' [fields]='ddlFields' (created)="onCreated()"></ejs-listbox> |
import { ListBoxComponent, FieldSettingsModel, SelectionSettingsModel } from '@syncfusion/ej2-angular-dropdowns';
//..
public selectionSettings: SelectionSettingsModel = {showCheckbox: true}; |
import { ListBoxAllModule, DropDownListModule, CheckBoxSelectionService } from '@syncfusion/ej2-angular-dropdowns';
//..
@NgModule({ declarations: [ AppComponent ], imports: [ ListBoxAllModule, DropDownListModule, BrowserModule], providers: [CheckBoxSelectionService], bootstrap: [AppComponent]
})
export class AppModule { } |
Hi Luciano,Thank you for contacting Syncfusion support.We have checked your requirement “To select Item in Listbox using selectItems method” and we have prepared a sample to achieve it using created event in ListBox as like in the below code example,HTML
<ejs-listbox #listBoxDetails [dataSource]='data' [fields]='ddlFields' (created)="onCreated()"></ejs-listbox>TS
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';import { ListBoxComponent, FieldSettingsModel } from '@syncfusion/ej2-angular-dropdowns';export class AppComponent {@ViewChild('listBoxDetails', {static: true})public ddlList: ListBoxComponent ;// dataSource definitionpublic data: { [key: string]: Object }[] = [{ text: 'Cabbage', type: 'Leafy and Salad', code: 'item1' },//..];public ddlFields: FieldSettingsModel = { groupBy: 'type', text: 'text', value: 'code' };onCreated(){this.ddlList.selectItems(['Spinach']);}}Could you please check the above sample and get back to us if you need any further assistance on this?Regards,Vinoth Kumar S
public data: { [key: string]: Object }[] = [
{ text: "Cabbage", type: "Leafy and Salad", code: "item1" },
{ text: "Spinach", type: "Leafy and Salad", code: "item2" },
{ text: "Wheat grass", type: "Leafy and Salad", code: "item3" },
{ text: "Yarrow", type: "Leafy and Salad", code: "item4" },
{ text: "Spinach", type: "Leafy and Salad", code: "item5" },
{ text: "Chickpea", type: "Beans", code: "item6" },
{ text: "Green bean", type: "Beans", code: "item7" },
{ text: "Horse gram", type: "Beans", code: "item8" },
{ text: "Garlic", type: "Bulb and Stem", code: "item9" },
{ text: "Bulb", type: "Bulb and Stem", code: "item10" },
{ text: "Onion", type: "Bulb and Stem", code: "item11" }
];
public ddlFields: FieldSettingsModel = {
groupBy: "type",
text: "text",
value: "code",
mode: "Multiple"
};
onCreated() {
this.ddlList.selectItems(["item2", "item5"], true, true);
}
|