ID | Name | Selected |
1 | Name 1 | false |
2 | Name 2 | true |
3 | Name 3 | true |
4 | Name 4 | false |
5 | Name 5 | true |
6 | Name 6 | false |
7 | Name 7 | false |
8 | Name 8 | true |
9 | Name 9 | false |
10 | Name 10 | true |
So in my listbox, items 2, 3, 5, 8 and 9 would appear as selected
Thanks
<div id="listbox-control">
<EjsListBox Value=@ListboxValue DataSource="@ListData" TValue="List<string>">
<ListBoxSelectionSettings ShowCheckbox="true"></ListBoxSelectionSettings>
<ListBoxFieldSettings Text="Name" Value="ID"></ListBoxFieldSettings>
</EjsListBox>
</div>
<style>
#listbox-control {
width: 25%;
margin: auto;
}
</style>
@code{
public List<string> ListboxValue = new List<string>();
public List<ListDataModel> ListData = new List<ListDataModel>
{
new ListDataModel { Name = "Name 1", ID = "1", Selected = false },
new ListDataModel { Name = "Name 2", ID = "2", Selected = true },
……..
};
protected override void OnInitialized()
{
foreach (ListDataModel data in ListData)
{
if (data.Selected)
{
this.ListboxValue.Add(data.ID);
}
}
}
public class ListDataModel
{
public string Name { get; set; }
public string ID { get; set; }
public bool Selected { get; set; }
}
} |