Ticket T707120
Visible to All Users

How to filter data with DataSourceLoadOptions on the server

created 6 years ago

[DevExpress Support Team: CLONED FROM T699966: DataGrid - Filter DataSourceOptions option is not filled when the client "filter" method is called]
Hi Maxim,

One more issue related to the same scenario, Like when i click on first gridview row checkbox then it sends the selected code (CCount) and pass to the second grid action method where based on CCount value gets data from table and bind to the second grid. In this case CCount column is not present in second gridview and in this grid oncontentready event always getting totalcount 0 and second grid not loading.

How could i fix this issue.

I have attached one sample project where the same issue is reproduced.

Kindly review and provide some suggestion.

Thanks,

Veera

Answers approved by DevExpress Support

created 6 years ago (modified 6 years ago)

Hello,
The issue is that you apply the filter two times to the same collection in the ReportDetails action method on the server. As a result, DataSourceLoader returns an empty source from the controller, and your CPT_byPayorDetails grid is empty.

Here is a part of your implementation.

C#
public ActionResult ReportDetails(DataSourceLoadOptions loadOptions) { List<OrderData> lstData = new List<OrderData>(); // 1. filter the OrdersData manually if (!string.IsNullOrEmpty(loadOptions.Filter[1].ToString())) { lstData = SampleData.OrdersData.Where(a => a.CCount == Convert.ToInt32(loadOptions.Filter[2])).Select(x => new OrderData { L1 = x.L1, L2 = x.L2, L3 = x.L3, DGroup = x.DGroup, ORank = x.ORank, RCount = x.RCount }).ToList(); } // 2. apply the filter to a filtered lstData var loadResult = DataSourceLoader.Load(lstData, loadOptions); return Content(JsonConvert.SerializeObject(loadResult), "application/json"); }

As you can see, when the first condition is true, the OrdersData collection is filtered by the LINQ Where, and the resulting lstData collection contains a single OrderData item. When lstData is forwarded to DataSourceLoader.Load(), it is filtered for the second time. Besides, since the CCode property value is missing (thus, CCode = 0), the Load() always returns an empty collection.

To resolve the issue, don't filter your collection manually. Instead, forward the entire SampleData.OrdersData collection to the DataSourceLoader.Load() method.

C#
public ActionResult ReportDetails(DataSourceLoadOptions loadOptions) { var loadResult = DataSourceLoader.Load(SampleData.OrdersData, loadOptions); return Content(JsonConvert.SerializeObject(loadResult), "application/json"); }

In addition, if you are planning to filter the server-side source by multiple CCode values (if you are selecting multiple rows in the main grid), consider redesigning your client-side cellClick() logic so that it builds an appropriate filter expression. For example,

JavaScript
function cellClick(e) { var getSelectedData = e.selectedRowsData; var filter = []; for (var i = 0; i < getSelectedData.length; i++) { var ccountValue = getSelectedData[i].CCount; if (i > 0) { filter.push("or"); } filter.push(['CCount', '=', ccountValue]) } if (filter.length > 0) { var dataSource = $("#CPT_byPayorDetails").dxDataGrid("instance").getDataSource(); dataSource.filter(filter); dataSource.load(); } }

I suggest you review the filter documentation for more information on how to build a complex filter.

    Show previous comments (5)
    DevExpress Support Team 6 years ago

      Hi Vikash,
      I've created a separate ticket on your behalf (T727544: DataGrid - An error occurs in the browser on sending a long data filtering request to the server). Let's continue our discussion there.

        What is the javascript for the filter Array's PUSH order when you want to AND two sets of ORs.

        For this to work, the ORs should be grouped together in order for the AND to apply to different sets of ORs. But just pushing parenthesis and/or brackets leads to an EXCEPTION.

        SO, HOW IS THIS DONE ??

        (

        ['Color', '=', 'Red']
        or
        ['Color', '=', 'Blue']

        )

        AND

        (

        ['Size', '=', 'Large']
        or
        ['Size', '=', 'Small']

        )

        DevExpress Support Team 3 years ago

          Hi Brian,

          I created a separate ticket on your behalf: T1062743: How to construct a hierarchical filtering expression. We placed it in our processing queue and will process it shortly.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.