I want to access runtime the value by which the user filters.
===ADMIN EDIT===
Use the OnRead event handler to access the filter value:
@using Telerik.DataSource
@using Telerik.DataSource.Extensions
<TelerikComboBox OnRead="@OnComboBoxRead"
TItem="@ListItem"
TValue="@int"
@bind-Value="@SelectedValue"
TextField="@nameof(ListItem.Text)"
ValueField="@nameof(ListItem.Id)"
Filterable="true"
FilterOperator="@StringFilterOperator.Contains"
Width="300px">
<ItemTemplate>
@HighlightResult(context.Text)
</ItemTemplate>
</TelerikComboBox>
<style>
.k-list-item:has(u) {
gap: 0;
}
</style>
@code {
private List<ListItem> ListItems { get; set; } = new();
private int SelectedValue { get; set; } = 3;
private string ComboBoxFilterValue { get; set; } = string.Empty;
private async Task OnComboBoxRead(ComboBoxReadEventArgs args)
{
ComboBoxFilterValue = args.Request.Filters.Cast<FilterDescriptor>().FirstOrDefault()?.Value.ToString() ?? string.Empty;
DataSourceResult result = await ListItems.ToDataSourceResultAsync(args.Request);
args.Data = result.Data;
args.Total = result.Total;
}
public MarkupString HighlightResult(string text)
{
var result = string.IsNullOrWhiteSpace(ComboBoxFilterValue)
? text
: text.Replace(ComboBoxFilterValue, "<u>" + ComboBoxFilterValue + "</u>", StringComparison.OrdinalIgnoreCase);
return new MarkupString(result);
}
protected override void OnInitialized()
{
ListItems = new List<ListItem>() {
new ListItem(1, "Basketball"),
new ListItem(2, "Golf"),
new ListItem(3, "Baseball"),
new ListItem(4, "Table Tennis"),
new ListItem(5, "Volleyball"),
new ListItem(6, "Football"),
new ListItem(7, "Boxing"),
new ListItem(8, "Badminton"),
new ListItem(9, "Cycling"),
new ListItem(10, "Gymnastics"),
new ListItem(11, "Swimming"),
new ListItem(12, "Wrestling"),
new ListItem(13, "Snooker"),
new ListItem(14, "Skiing"),
new ListItem(15, "Handball"),
};
base.OnInitialized();
}
public class ListItem
{
public int Id { get; set; }
public string Text { get; set; } = string.Empty;
public ListItem(int id, string text)
{
Id = id;
Text = text;
}
}
}