anc.dev
November 21, 2024, 8:48am
1
I have a DataGrid with a simple filter setup. For one of the columns, I need to use a custom filter template, allowing users to query for IsEmpty
records. I am programmatically changing the filter operator for that column.
However, the filter retrieved from the LoadData
method ignores the IsEmpty
condition and defaults to Contains
. How can I resolve this issue?
builder.OpenComponent<RadzenDataGridColumn<T>>(0 + i * 41);
.
.
builder.AddAttribute(1 + i * 41, "Property", colDef.Property);
.
.
builder.AddAttribute(9 + i * 41, "FilterOperator", colDef.FilterOperator);
enchev
November 21, 2024, 8:51am
2
I'm afraid that the code provided is not enough to provide any advice. You can attach Radzen.Blazor.csproj to your solution to debug your case.
anc.dev
November 21, 2024, 9:23am
3
Hi @enchev ,
I think I reproduced it with your demo page.
<RadzenDataGrid style="height: 335px" @ref="grid" FilterMode="FilterMode.Simple" IsLoading=@isLoading Count="@count" Data="@employees" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="4" PagerHorizontalAlign="HorizontalAlign.Center" ColumnWidth="200px">
<Columns>
<RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="First Name" FilterOperator="FilterOperator.IsEmpty" Frozen="true" Width="160px"/>
<RadzenDataGridColumn Property="@nameof(Employee.LastName)" Title="Last Name" Width="160px"/>
</Columns>
</RadzenDataGrid>
@code {
async Task LoadData(LoadDataArgs args)
{
if (!string.IsNullOrEmpty(args.Filter))
{
filterTerm = args.Filter;
}
}
}
I changed FilterOperator in FirstName column. When I add filter to LastName, FirstName IsEmpty is ignored.
enchev
November 21, 2024, 9:27am
4
Which page? This code is incomplete and cannot be used as is.
anc.dev
November 21, 2024, 9:41am
5
Sorry for that.. This is the full code that I have edited from datagrid-loaddata
@using System.Linq.Dynamic.Core
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@inherits DbContextPage
<RadzenButton Text="Reset" Click="@Reset" Style="margin-bottom: 20px;" />
<RadzenDataGrid style="height: 335px" @ref="grid" FilterMode="FilterMode.Simple" IsLoading=@isLoading Count="@count" Data="@employees" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="4" PagerHorizontalAlign="HorizontalAlign.Center" ColumnWidth="200px">
<Columns>
<RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="First Name" FilterOperator="FilterOperator.IsEmpty" Frozen="true" Width="160px"/>
<RadzenDataGridColumn Property="@nameof(Employee.LastName)" Title="Last Name" Width="160px"/>
</Columns>
</RadzenDataGrid>
<RadzenCard class="rz-mt-6">
<RadzenText TextStyle="TextStyle.Body1">
@filterTerm
</RadzenText>
</RadzenCard>
@code {
RadzenDataGrid<Employee> grid;
int count;
IEnumerable<Employee> employees;
bool isLoading = false;
string filterTerm = "";
List<string> titles = new List<string> {"Sales Representative", "Vice President, Sales", "Sales Manager", "Inside Sales Coordinator" };
IEnumerable<string> selectedTitles;
async Task OnSelectedTitlesChange(object value)
{
if (selectedTitles != null && !selectedTitles.Any())
{
selectedTitles = null;
}
await grid.FirstPage();
}
async Task Reset()
{
grid.Reset(true);
await grid.FirstPage(true);
}
async Task LoadData(LoadDataArgs args)
{
isLoading = true;
await Task.Yield();
// This demo is using https://dynamic-linq.net
var query = dbContext.Employees.AsQueryable();
if (!string.IsNullOrEmpty(args.Filter))
{
filterTerm = args.Filter;
// Filter via the Where method
query = query.Where(args.Filter);
}
if (!string.IsNullOrEmpty(args.OrderBy))
{
// Sort via the OrderBy method
query = query.OrderBy(args.OrderBy);
}
// Important!!! Make sure the Count property of RadzenDataGrid is set.
count = query.Count();
// Perform paging via Skip and Take.
employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
isLoading = false;
}
}
enchev
November 21, 2024, 9:44am
6
This is what I see in our demo using this code:
The first column filter expression is part of the filtering.
anc.dev
November 21, 2024, 9:46am
7
Please try to reset and add filter...
enchev
November 21, 2024, 10:02am
8
anc.dev:
grid.Reset(true);
You've intentionally reset the column state including filtering.
anc.dev
November 21, 2024, 10:10am
9
Yes, In my case I have to change filter operator programmatically. Same thing happening there.
protected void OnFilterSetByTemplate()
{
var column = GridSettings.columnDefinitions.FirstOrDefault(c => c.Property.Equals(Template.Prop);
if (column != null)
{
column.FilterValue = Template.FilterValue;
column.FilterOperator = Template.FilterOperator
StateHasChanged();
Grid.Reload();
}
}
I think when we call grid.Reset(true)
, It is need to reset to the filter operator that we set on the Blazor component.
in the above example FirstName filter operator needs to reset to FilterOperator.IsEmpty
<RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="First Name" FilterOperator="FilterOperator.IsEmpty" Frozen="true" Width="160px"/>
enchev
November 21, 2024, 10:22am
10
As I already noted Reset(true) and Reset() will reset all columns state including filtering. If you do not want that you can use Reset(false) or set desired settings to the column after Reset().