how to supply multiple values for a Grid FilterView item

1 Answer 132 Views
Filter Grid
Gary
Top achievements
Rank 1
Gary asked on 24 Jun 2022, 09:20 PM
I'm trying to add a new filterview option to an existing grid filter with six other options that currently work fine. What's different about option 7 is that it needs to use two "or" values to filter the grid with. All the others have only one. Here is the code I've tried (along with one of the working filterview options), but it returns nothing and doesn't even generate any SQL in the console window. There is data in the database for both values.

                //this code works:
                new FilterView("CashR")
                {
                    Filters = new List<DataFilterValue>
                    {
                        new DataFilterValue
                        {
                            Field = nameof(InvoiceVM.InvoiceStatus),
                            Value = "CashR",
                            Operator = ExtendedFilterOperator.IsEqualTo
                        }
                    }
                },
                //this code doesn't work:
                new FilterView("Select Checks")
                {
                    Filters = new List<DataFilterValue>
                    {
                        new DataFilterValue
                        {
                            Field = nameof(InvoiceVM.InvoiceStatus),
                            Value = new string[] { "Verified", "Partial" },
                            Operator = ExtendedFilterOperator.IsInListEqualTo
                        }
                    }
                }

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 29 Jun 2022, 08:44 AM

Hi Gary,

If I understand correctly the requirement is to modify the request filters to return the InvoiceStatuses that have either Verified or Partial value, is that correct? For this scenario creating a CompositeFilter will be required. Create two separate FilterDescriptors for the Verified and Partial value and add them to a CompositeFilterDescriptor with a logical operator OR.

Check the following example:

        private IEnumerable<OrderViewModel> GetData()
        {
            var data = Enumerable.Range(1, 50).Select(i => new OrderViewModel
            {
                OrderID = i,
                Freight = i * 10,
                OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                ShipName = "ShipName " + i % 5,
                ShipCity = "ShipCity " + i
            }).ToList();
            return data;
        }
        public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request)
        {
            var result = GetData();
            if (!request.Filters.Any())
            {
                var firstFilter = new FilterDescriptor("OrderID", FilterOperator.IsEqualTo, 22);
                var secondFilter = new FilterDescriptor("ShipName", FilterOperator.Contains, 3);
                var compositeFilter = new CompositeFilterDescriptor();
                compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
                compositeFilter.FilterDescriptors.Add(firstFilter);
                compositeFilter.FilterDescriptors.Add(secondFilter);
                request.Filters.Add(compositeFilter);
            }

            var dsResult = result.ToDataSourceResult(request);
            return Json(dsResult);
        }

If no filters are present the data returned will contain the item with OrderID equal to 22, and all items that contain "3" in their ShipName. You can review this in the attached example.

I hope this helps.

Regards,
Aleksandar
Progress Telerik

The Premier Dev Conference is back! 

Coming to you live from Progress360 in-person or on your own time, DevReach for all. Register Today.


Tags
Filter Grid
Asked by
Gary
Top achievements
Rank 1
Answers by
Aleksandar
Telerik team
Share this question
or