I'm using Blazor server 19.3 version and I'm trying to apply the filtering rules to a specific column
My column can have the following data:
123
123NA
123NATER
123TER
123NPA
123PA
To filter rows having these specific values, I'm using the piece of code in attachment where I can filter data ends with "NATER" and "NAPA" but how can I capture only rows having numbers or ending with TER or ending with NA or ending with PA ?
If I use the operator "contains" supposing for "TER" I will get also NATER so it's not a solution
|
public void OnValueSelect(@Syncfusion.Blazor.DropDowns.SelectEventArgs<Data> args)
{
if (args.ItemData.CustomerID == "All")
{
Gridfatcli.ClearFiltering();
}
else
{
if (args.ItemData.CustomerID == "NATER" || args.ItemData.CustomerID == "NAPA")
{
Gridfatcli.FilterByColumn("CustomerID", "endswith", args.ItemData.CustomerID);
}
else
{
string customfiltervalue = "123" + args.ItemData.CustomerID;
Gridfatcli.FilterByColumn("CustomerID", "equal", customfiltervalue);
}
}
}
|
Thanks for this reply, but I think I didn't axplain my problem well.
In my attachment I used the ValueChange="@Change" event instead of your OnValueSelect="OnValueSelect" but I think it's the same
When I select in the dropdownlist "Digits" I want to show customerid made up of only digits like 123, 456, 788, and so on for this reason "equal" is not the right solution. I suppose using a RegExp expression would work but I can't use it in the filter options I think.
Also, if I select "TER" in the dropdownlist I want to show only customerid like 123TER, 456TER, 788TER and so on, but if I use "contains" or "endswith" I will show also 123NATER, 456NATER... and I don't want it
this is what is working at the moment for NA, PA, NATER, NAPA
public void Change(@Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, Data> args)
{
if (args.Value == "All")
{
Gridfatcli.ClearFiltering();
}
else
{
if (args.ItemData.NUMFATT == "NATER" || args.ItemData.NUMFATT == "NAPA" || args.ItemData.NUMFATT == "NA" || args.ItemData.NUMFATT == "PA")
Gridfatcli.FilterByColumn("NUMFATT", "endswith", args.Value);
else if (args.ItemData.NUMFATT == "Digits")
//What here ?
else if (args.ItemData.NUMFATT == "TER")
//what here ?
}
}
|
public class CustomAdaptor : DataAdaptor
{
public override object Read(DataManagerRequest dm, string key = null)
{
IEnumerable<Order> DataSource = Orders;
...
if (dm.Where != null && dm.Where.Count > 0)
{
// Filtering
if (dm.Where[0].predicates[0].value == "NATER" || dm.Where[0].predicates[0].value == "NAPA" ...)
{
//filter action using inbuilt PerformFiltering method
DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
//Handle custom filtering action based on your requirement as like below highlighted.
//Custom filtering action
else if(dm.Where[0].predicates[0].value == "TER")
{
DataSource = DataSource.Where(e => Regex.Replace(e.CustomerID, @"[\d-]", string.Empty).Equals(dm.Where[0].predicates[0].value));
}
else if(dm.Where[0].predicates[0].value == "Digits")
{
DataSource = DataSource.Where(e => Regex.Matches(e.CustomerID, @"[a-zA-Z]").Count == 0);
}
}
...
return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;
}
}
|
many thanks
It works as expected in this way.
I'd like also to filter the OrderDate field using a SfDateRangePicker component showing it when clicking inside the
FilterItemTemplate of that field but if I place it there, it's not opened
<FilterItemTemplate>
<SfDateRangePicker @bind-StartDate="@StartValue" @bind-EndDate="@EndValue" TValue="DateTime?" Placeholder="select range" ShowClearButton="true">
<DateRangePickerEvents TValue="DateTime?" ValueChange="FilterDateColumn" />
</SfDateRangePicker>
</FilterItemTemplate>
If I have to place this component outside the datagrid, how can it work in combination with the other filters inside the CustomAdaptor ?
|
<FilterTemplate>
<SfDateRangePicker ...></SfDateRangePicker>
</FilterTemplate>
|
I'm sorry... my mistake
using <FilterTemplate> the component is showed correctly
I add you in attachment your sample where I inserted the daterangepicker in the orderdate column
What can I write in the daterangepicker event to call the customadaptor and filter the data accordingly ?
I don't find a solution in the documentation
Thanks
|
private async Task FilterDateColumn(RangePickerEventArgs<DateTime?> args)
{
//what can I write here to filter for OrderDate range values ?
if (Gridfatcli.FilterSettings.Columns == null)
{
Gridfatcli.FilterSettings.Columns = new List<GridFilterColumn>();
}
var a = await Gridfatcli.GetColumns(); //Get all the Grid columns
string fUid = a[2].Uid; //Fetch the Uid of OrderDate column
List<GridFilterColumn> gridFilteredColumns = Gridfatcli.FilterSettings.Columns.ToList();
foreach(GridFilterColumn DateCol in gridFilteredColumns)
{
if (DateCol.Field == "OrderDate")
{
Gridfatcli.FilterSettings.Columns.Remove(DateCol);
}
}
Gridfatcli.FilterSettings.Columns.Add(new GridFilterColumn { Field = "OrderDate", Operator = Syncfusion.Blazor.Operator.GreaterThanOrEqual, Predicate = "and", Value = args.StartDate, Uid = fUid });
Gridfatcli.FilterSettings.Columns.Add(new GridFilterColumn { Field = "OrderDate", Operator = Syncfusion.Blazor.Operator.LessThanOrEqual, Predicate = "and", Value = args.EndDate, Uid = fUid });
Gridfatcli.Refresh(); //Call Refresh to reflect the filtered changes in Grid
}
|
It works in this way... thanks, but not completely because all the possible filters of the filter bar don't work together in and condition since the pretidicate index depends on the user's choice order
If I filter first by CustomerID and then by orderdate it doesn't filter both
Is there a way to accomplish this ?
|
if (dm.Where != null && dm.Where.Count > 0)
{
foreach(var filter in dm.Where[0].predicates)
{
//for CustomerID column
if (filter.value == "NATER" || filter.value == "NAPA" || filter.value == "NA" || filter.value == "PA")
{
DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
else if (filter.value == "TER")
{
DataSource = DataSource.Where(e => Regex.Replace(e.CustomerID, @"[\d-]", string.Empty).Equals(filter.value));
}
else if (filter.value == "Digits")
{
DataSource = DataSource.Where(e => Regex.Matches(e.CustomerID, @"[a-zA-Z]").Count == 0);
}
else if(filter.Field == null && filter.predicates[0].Field == "OrderDate")
{
//for OrderDate column
DataSource = DataSource.Where(e=> (e.OrderDate <= (DateTime)filter.predicates[1].value && e.OrderDate>=((DateTime)filter.predicates[0].value)));
}
else
{
//for other columns - OrderID, Freight
DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
}
}
|
thanks a lot
now it works perfectly