Hi,
Within our Blazor application I use a Grid with custom row filters:
<TelerikGrid Data="@RecordList"
SelectionMode="GridSelectionMode.Multiple"
Width="100%"
Pageable="true"
PageSize="@PageSize"
FilterMode="@GridFilterMode.FilterRow"
Sortable="true"
Groupable="false"
Height="760px"
Resizable="true">
<GridColumns>
<GridColumn Width="200px" Field="@nameof(Demo.Name)" Title="@L["Name"]">
<FilterCellTemplate>
<CustomListRowFilter Context="@context" ListValues="EmployeeFilterList" AddNoneItem="false" />
</FilterCellTemplate>
</GridColumn>
<GridColumn Width="200px" Field="@nameof(Demo.Datum)" Title="@L["Date"]">
<FilterCellTemplate>
<CustomDateSpanRowFilter Context="@context" InitialValueFrom="@InitialDateFromFilter" InitialValueTo="@InitialDateToFilter" />
</FilterCellTemplate>
</GridColumn>
<GridColumn Width="200px" Field="@nameof(Demo.Hours)" Title="@L["Hours"]">
<FilterCellTemplate>
<CustomNumberRowFilter Context="@context" />
</FilterCellTemplate>
<FooterTemplate>
<span>Total: @context.Sum?.ToString("0.00")</span>
</FooterTemplate>
</GridColumn>
<GridColumn Width="200px" Field="@nameof(Demo.Checked)" Title="@L["Checked"]">
<FilterCellTemplate>
<CustomBooleanRowFilter Context="@context" InitialValue="false" />
</FilterCellTemplate>
<Template>
<input type="checkbox" checked="@(((Demo) context).Checked)" @onchange="@(args => OnCheckChanged((Demo) context, args))" />
</Template>
</GridColumn>
</GridColumns>
<GridAggregates>
<GridAggregate Field=@nameof(Demo.Hours) Aggregate="@GridAggregateType.Sum" />
</GridAggregates>
</TelerikGrid>
For example, the custome date filter looks like:
<select value="@Value" @onchange="OnFilterChanged">
<option value="@string.Empty">All</option>
<option value="True">True</option>
<option value="False">False</option>
</select>
@code {
private string Field { get; set; }
private string Value { get; set; }
private bool? _boolValue;
[Parameter]
public FilterCellTemplateContext Context { get; set; }
[Parameter]
public bool? InitialValue { get; set; }
protected override async Task OnInitializedAsync()
{
Field = ((FilterDescriptor) Context.FilterDescriptor.FilterDescriptors[0])?.Member;
if (InitialValue != null)
{
SetValue(InitialValue.Value);
await SetFilter();
}
else
{
SetValue(((FilterDescriptor)Context.FilterDescriptor.FilterDescriptors[0]).Value);
}
}
private void SetValue(object value)
{
if (string.IsNullOrEmpty(value?.ToString()) || value.ToString() == "All")
{
_boolValue = null;
Value = string.Empty;
}
else
{
bool.TryParse(value.ToString(), out var parseResult);
_boolValue = parseResult;
Value = _boolValue.ToString();
}
}
protected async Task OnFilterChanged(ChangeEventArgs args)
{
var value = args.Value;
SetValue(value);
await SetFilter();
}
private async Task SetFilter()
{
if (_boolValue != null)
{
var filterDescriptors = Context.FilterDescriptor.FilterDescriptors.Where(descriptor =>
((FilterDescriptor)descriptor).Member == Field).ToList();
var filterDescriptor = (FilterDescriptor) filterDescriptors[0];
filterDescriptor.Value = _boolValue;
((FilterDescriptor)filterDescriptors[0]).Operator = FilterOperator.IsEqualTo;
await Context.FilterAsync();
}
else
{
await Context.ClearFilterAsync();
}
}
}
In my example the filter for the column "Checked" gets an initial value when page is loaded. The grid filters correctly. But the sum of the hours column show the overall sum and not the filtered one.
Do I miss something? Maybe within the filter component?
Best regards,
Rayko