Checkbox filter DataGrid behaviour change operator to "not Equal" when I have more items no selected than selected
Checkbox filter inside column DataGrid: The behaviour change the operator to "not Equal" when I have more items selected than no selected and only provide the items not selected (I think that it does by performance).
I only need to have the operatot "not equal" for the scennario where just one checkbox is remaining to be checked and also provide that value in that case for the other scennarios where I have more than one checkbox checked
I just want to use the equal operator and the checked values names for my payload.
This is my code where I'm returning the data and where I found the issue, debugging I can see that the - DataManagerRequest dm is the parameter that it's returning this values wrong inside.
-------------------------
PagingAndSorting(Syncfusion.Blazor.DataManagerRequest dm)
{
List<FieldFilter> filters = new();
if (dm.Where != null && dm.Where.Any(d => d != null && d.predicates.Any(p => p != null && (p?.value != null || p.predicates.Count > 0))))
{
filters = dm.Where.Where(p => p != null)
.SelectMany(x => x.predicates)
.Where( p => p != null && p.value != null || p.predicates.Count > 0)
.Select(f =>
{
var val = f.value;
FieldFilter? fieldFilterX = new FieldFilter();
if (f.value == null)
{
List<FilterByColumnInfo> filterBy2 = new List<FilterByColumnInfo>();
foreach (var item in f.predicates)
{
val = item.value;
if (val is LocalizedString localizedString)
{
val = localizedString.Neutral;
}
if (val is GenericLocalizedReference localizedReference)
{
val = localizedReference.DisplayName.Neutral;
}
if (val is DateTime dateTime)
{
val = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0).ToString("yyyy-MM-dd HH:mm:ss");
}
filterBy2.Add(
new FilterByColumnInfo
{
Value = ValueTrimmer(val),
Operator = (FilterOperator)Enum.Parse(typeof(FilterOperator), item.Operator, true)
});
fieldFilterX = new FieldFilter()
{
Field = item.Field, //f.Field,
FilterBy = filterBy2
};
}
return fieldFilterX;
}
return new FieldFilter()
{
Field = f.Field,
FilterBy = new List<FilterByColumnInfo>()
{
new()
{
Value = ValueTrimmer(val),
Operator = (FilterOperator)Enum.Parse(typeof(FilterOperator), f.Operator, true)
}
}
};
}).ToList();
}
--------------------------------------------------------------------
This is the code where the checkbox is enabled:
<DataGrid @[email protected]
TItem="TestModel"
AllowSelection=true
AllowPaging=true
ShowColumnChooser=true
AllowResizing=true
AllowFiltering=true
EnablePersistence=true
GridFilterType=FilterType.Menu
SearchAsync=@((x) => BindingContext.ResultsPagingAndSortingChanged(x))
ContextMenuCallback=@((x) => BindingContext.OnContextMenuClickCallback((ContextMenuClickEventArgs<TestModel>)x))>
<DataGridColumns>
<GridEvents [email protected]
ContextMenuItemClicked="BindingContext.OnContextMenuClickCallback"
TValue="TestModel"></GridEvents>
<DataGridColumn Field=@nameof(TestModel.status)
FilterSettings=@(new Syncfusion.Blazor.Grids.FilterSettings() { Type = Syncfusion.Blazor.Grids.FilterType.CheckBox })>
<Template>
@if (context is TestModel operation)
{
<div class="text-center" style="cursor: pointer" @onclick="@(() => BindingContext.OpenDetail(operation))">
</div>
}
</Template>
</DataGridColumn>
</DataGridColumns>
</DataGrid>
This is the payload that I'm expecting to have in the scennario just where only one item is remaining to check
This is the payload that I'm having currently (bad) when I have 2 items remaining to check.
In this case I need all the items checked in the Payload with the Operator: 2 (Equal) not the operator : 8 (notequal)
Regards.
Hi Matias,
We would like to inform you that by default checkbox filtering behavior is designed to optimize performance when a large number of items are filtered. By default, when more items are excluded than included, the grid switches to using the not equal operator (!=) for the filter condition. However, in your use case, you want the filter to always use the equal operator (=), even if there are only a few items selected, unless only one item remains unchecked.
So we suggest you to try customizing as highlighted below:
Here we have changed the operator based on the selected value count. Kindly try changing like below and get back to us if we misunderstood your query or if you have further queries.
|
PagingAndSorting(Syncfusion.Blazor.DataManagerRequest dm) { List<FieldFilter> filters = new(); if (dm.Where != null && dm.Where.Any(d => d != null && d.predicates.Any(p => p != null && (p?.value != null || p.predicates.Count > 0)))) { var allPredicates = dm.Where.Where(p => p != null) .SelectMany(x => x.predicates) .Where(p => p != null); var selectedValues = allPredicates.Where(p => p.value != null).ToList(); var unselectedValuesCount = allPredicates.Count() - selectedValues.Count;
filters = allPredicates.Select(f => { var val = f.value; var operatorType = selectedValues.Count <= 1 ? "NotEqual" : "Equal";
if (unselectedValuesCount == 1) { operatorType = "NotEqual"; }
FieldFilter? fieldFilterX = new FieldFilter(); return new FieldFilter() { Field = f.Field, FilterBy = new List<FilterByColumnInfo>() { new() { Value = ValueTrimmer(val), Operator = Enum.Parse<FilterOperator>(operatorType, true) } } }; }).ToList(); } } |
Regards,
Monisha
Hi Monisha,
Thanks for your reply.
The issue lies with the "dm" object. For example, when there are only two items left to check, the payload includes just these two items with a "notequal" (8) condition.
PagingAndSorting(Syncfusion.Blazor.DataManagerRequest dm)
Your proposed solution to change "notequal" seems fine. However, I cannot assign the VALUE because it is not provided within the dm object. The dm object only supplies the remaining VALUES and not the ones that have already been checked.
Your code works correctly when the number of remaining items is less than the number of checked items. But in scenarios where there are more items checked, the issue arises because the VALUES for those already checked items are not available in the dm object—only the remaining items' VALUES are provided.
What I need is for the dm object to include the other already-checked VALUES as well.
Hi
Matias,
We would like to clarify the current behavior of the grid filter. When
filtering more than half of the values from the list, the filter applies the
"and" operator with a "not equal" predicate, which is why
only the unchecked values are provided in the DataManager (dm). On the other
hand, when filtering less than half of the values, the filter uses the
"or" operator with an "equal" predicate. This is the
default behavior of the grid which helps to send the query with less
predicates.
Regards,
Prathap S
- 3 Replies
- 3 Participants
-
MP Matias Paco Viscarra
- Dec 11, 2024 06:45 PM UTC
- Dec 24, 2024 03:24 PM UTC