- Install the NuGet package
KeudellCoding.Blazor.AdvancedBlazorSelect2
. - Add the following lines to _Host.cshtml (in the head tag)
<script src="_content/KeudellCoding.Blazor.AdvancedBlazorSelect2/Select2JsInterop.js" type="text/javascript" language="javascript"></script>
<script src="_content/KeudellCoding.Blazor.AdvancedBlazorSelect2/lib/select2/js/select2.full.min.js" type="text/javascript" language="javascript"></script>
<link href="_content/KeudellCoding.Blazor.AdvancedBlazorSelect2/lib/select2/css/select2.min.css" rel="stylesheet" />
- Make sure jQuery is installed.
- Add the following lines to _Imports.razor
@using KeudellCoding.Blazor.AdvancedBlazorSelect2
@page "/"
@inject IMemoryCache __MemoryCache
@inject ApplicationDbContext __DbContext
<Select2 TItem="ExampleItemForm"
TSource="DbSet<ExampleItemForm>"
IdSelector="c => c.Id.ToString()"
TextSelector="c => c.Text"
FilterFunction="filterFunction"
GetElementById="async (items, id, token) => await items.FindAsync(id, token)"
Datasource="__DbContext.ExampleItems"
Value="@SelectedItems"
Cache="__MemoryCache"
Multiselect="false" />
@code {
public class ExampleItemForm {
public Guid Id { get; set; }
public string Text { get; set; }
public ExampleItemForm(Guid id, string text) {
Id = id;
Text = text;
}
}
//======================================================
public List<ExampleItemForm> SelectedItems { get; set; }
//======================================================
private async Task<List<ExampleItemForm>> filterFunction(DbSet<ExampleItemForm> allItems, string filter, CancellationToken token) {
return await allItems.Where(i => i.Text.StartsWith(filter)).ToListAsync();
}
}
- It is ensured that the
SelectedItems
list contains only one item ifMultiselect
is set tofalse
. - As soon as
SelectedItems
is edited manually,StateHasChanged();
should be executed. GetElementById
must be set manually because the process also depends on the data source used.