'Using drop-down as filter with Blazorise.DataGrid

The Blazorise DataGrid supports textbox filters.

I would like to use a drop-down component to allow filtering by specific values. What do I need to do to make the grid react to the change of the Select value?

Code Example
@page "/ItemList"
@using DataAccessLibrary
@using Blazorise
@using Blazorise.DataGrid

@inject IItemList _items;

<div align="center">
    <h3>Item List</h3>
</div>
@if (ItemListItems is null)
{
    <p>Loading...</p>
}
else
{
<DataGrid Data="@ItemListItems" TItem="ItemListItem" PageSize="20" ShowPager="true" Filterable="true" Striped="true" Narrow="true" @bind-SelectedRow="@selectedItem">
    <EmptyTemplate>
        <div class="box">
            No items were found!
        </div>
    </EmptyTemplate>
    <DataGridColumns>
        <DataGridCommandColumn TItem="ItemListItem" Caption="Action" EditCommandAllowed="true">
            <EditCommandTemplate>
                <Button Color="Color.Primary" Clicked="@context.Clicked">Edit</Button>
            </EditCommandTemplate>
        </DataGridCommandColumn>
        <DataGridNumericColumn TItem="ItemListItem" Field="@nameof(ItemListItem.ItemID)" Caption="Item ID" Sortable="true" TextAlignment="TextAlignment.Right">
            <DisplayTemplate>
                <a href="ItemDetails/?Itemid=@(context.ItemID)">@(context.ItemID)</a>
            </DisplayTemplate>
        </DataGridNumericColumn>
        <DataGridSelectColumn TItem="ItemListItem" Field="@nameof(ItemListItem.TypeShortDesc)" Caption="Item Type" Sortable="true">
            // This filter should replace the default textbox with a dropdown listing only specific values
            <FilterTemplate>
                <Select TValue="string" @bind-SelectedValue="@ItemTypeFilter">
                    @foreach (string type in ItemTypeList)
                    {
                        <SelectItem Value="@type">@type</SelectItem>
                    }
                </Select>
            </FilterTemplate>
        </DataGridSelectColumn>
        <DataGridSelectColumn TItem="ItemListItem" Field="@nameof(ItemListItem.Description)" Caption="Item Description" Sortable="true" TextAlignment="TextAlignment.Left" />
        <DataGridSelectColumn TItem="ItemListItem" Field="@nameof(ItemListItem.StatusShortDesc)" Caption="Status" Sortable="true">
            <FilterTemplate>
            // This filter should replace the default textbox with a dropdown listing only specific values
                <Select TValue="string" @bind-SelectedValue="@ItemStatusFilter">
                    @foreach(string status in ItemStatusList)
                    {
                        <SelectItem Value="@status">@status</SelectItem>
                    }
                </Select>
            </FilterTemplate>
        </DataGridSelectColumn>
        <DataGridNumericColumn TItem="ItemListItem" Field="@nameof(ItemListItem.ItemPrice)" Caption="Amount" Sortable="false" TextAlignment="TextAlignment.Right" DisplayFormat="{0:C}" />
    </DataGridColumns>
</DataGrid>
}

@code {
    private List<ItemListItem> ItemListItems;
    private ItemListItem selectedItem;

    private List<string> ItemStatusList;
    private string ItemStatusFilter;
    private List<string> ItemTypeList;
    private string ItemTypeFilter;

    protected override async Task OnInitializedAsync()
    {
        ItemListItems = await _items.GetItems();
        ItemTypeList = await _items.GetItemTypes();
        ItemStatusList = await _items.GetItemStatuses();
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source