- Home
- Forum
- React - EJ 2
- Grid gets stuck in an infinite loop when the filter returns the same results on two consecutive occasions
Grid gets stuck in an infinite loop when the filter returns the same results on two consecutive occasions
Hello,
As the title says, when the filter returns the same results for a second consecutive time, it gets stuck in an infinite loop. For instance, if I filter for a value that returns no results, it will work the first time, but if I do it again for a second time the spinning indicator comes up and does not go away. I've checked the incoming response and it does get refreshed on every call. The Grid though remains stuck.
I'm using remote data binding through an api call. All filtering functions are done server-side and I am not using DataManager. The app's framework is Next.js.
Could you please provide some insight into why that may be happening?
Hi George,
Greetings from Syncfusion support
Based on your query it appears that you are encountering an issue of Grid gets stuck in an infinite loop when the filter returns same result. Before we start providing solution on your query, we need some more information for our clarification. So please share the below details that would be helpful for us to provide better solution,
1) Complete Grid Rendering Code (both client-side and server-side): This will help us understand how you are invoking your API during filtering, how server-side filtering is handled, and how the filtered data is bound to the Grid.
2) Network Tab Screenshots: Please share screenshots of the request payload and server response from your browser’s Network tab. We’d like to review the filter query structure and the format of the returned data.
3) Issue Demonstration Video: If possible, provide a short video showing the issue along with the steps to reproduce it.
4) State Updates During Filtering: Let us know if you are performing any state updates in the Grid during the filtering process.
5) Script Error Details (if any): If you are encountering any errors in the browser console (JavaScript exceptions or warnings), please share the complete error messages along with a screenshot. These details will help us identify any potential client-side issues.
Regards,
Rajapandi R
Hello,
Unfortunately, I am away and I can't provide the requested details.
However, I remembered that when I inspected the GridComponent with the React Developer tools, when the grid is working correctly, the GridComponent's dataSource is the object {actionArgs, enablePersistance, result, count, etc}, whereas when it gets stuck to a loop, the dataSource contains only {result, count}. For some reason the filtering events aren't being triggered. I should also add that the grid is in a child component which receives the data as a prop from a parent component.
I hope that this makes sense and can help shed some light.
George,
Thanks for your update
Based on your query, it seems that you are using the API to fetch data and handle the Grid actions like filtering, paging, and sorting etc. in your own API service and facing the problem. You can achieve your requirement by using the Custom Binding feature of Grid.
The custom binding feature in the React Grid enables you to manage your own custom API for handling data processing externally and then binding the resulting data to the Grid. This allows you to implement your own custom data logic to your application’s requirements. When using custom binding, the Grid expects the result of the custom logic to be an object with properties result and count. The result property should contain the data to be displayed in the Grid, while the count property indicates the total number of records in the dataset for your application.
The dataStateChange event is triggered whenever you perform actions that modify the state of the grid’s data, such as changing pages, applying sorting, or grouping. This event provides detailed information about the action performed and the current state of the grid, including parameters like page number, sorting details, and filtering criteria.
When filtering operation is performed in the Grid, the dataStateChange event is triggered with following arguments you can handle this in your service and return the result in the required format result and count.
We have already discussed about this in our documentation which can be accessed from the below link,
Documentation: https://ej2.syncfusion.com/react/documentation/grid/data-binding/remote-data#custom-binding
Since that the dataSource object differs between working and non-working states, the absence of actionArgs and enablePersistence might point to an issue with how events or lifecycle methods are being executed. Here's a potential approach to address this issue:
1) Grid Events: Verify that all necessary events for the Grid component are properly captured and handled. This includes ensuring filtering events trigger the correct actions and that state updates occur as expected.
2) DataSource Structure: By default, in our Grid component, actions such as sorting and filtering trigger the dataStateChange event, which includes the corresponding state action arguments.
Please inspect why the actionArgs and enablePersistence properties are missing in the non-working scenario. Ensure that you are correctly receiving the relevant action arguments in the dataStateChange event and that your service is properly handling the filtering action. Additionally, your server response should include the required properties—such as result and count—along with any necessary state data to maintain functionality during filtering.
3) Actionargs and enablePersistence: Please ensure on your end that while returning the filter results from your service, the actionArgs and enablePersistence properties are not being handled at the application level in a way that could lead to script errors.
4) Error Handling: Implement error handling in your API calls and ensure any errors returned from the server are dealt with, such as retrying requests or providing user feedback to prevent infinite loops.
Once these areas are reviewed and adjusted, the issue may be resolved. If you are still facing difficulties, sharing your Grid code snippets related to these aspects once you are able to will help us provide a more precise solution.
Hello,
Thanks for getting back to me. Regarding the DataManager example in the custom binding section, I am aware of it and I purposefully decided against using it as it requires all the data from the server. In any case, I am attaching the relevant functions.
dataStateChange Method:Code Example : function dataStateChange(state: DataStateChangeEventArgs | any) { if ( state.action && (state.action.requestType == 'filterchoicerequest' || state.action.requestType == 'filterSearchBegin' || state.action.requestType == 'stringfilterrequest') ) { state.skip = 0; state.take = 1000; // Fetch filtered data execute(state).then((response: any) => { const data = response['result'] state.dataSource(data); }); } else{ // Handle other state changes like paging or sorting execute(state).then((gridData) => { grid.dataSource = gridData; }); } } |
execute Function:getData, which constructs and executes the query.function execute(state: DataStateChangeEventArgs | any): Promise<DataResult> { return getData(state); } |
getData Function:function getData(state: DataStateChangeEventArgs): Promise<DataResult> { const pageQuery = `$skip=${state.skip}&$top=${state.take}`; // Paging info // Filtering logic if (state.where) { filterQuery = `&$filter=` + state.where.map((obj) => { if (obj.isComplex && obj.predicates) { // Handle complex filters const predicates = obj.predicates.map((predObj) => { if (predObj.isComplex && predObj.predicates) { return predObj.predicates .filter((predicate) => predicate.operator === 'equal') .map((predicate) => { const value = typeof predicate.value === 'string' ? `'${predicate.value}'` : predicate.value; return `${predicate.field} eq ${value}`; }).join(' or '); } else { const value = typeof predObj.value === 'string' ? `'${predObj.value.toLowerCase()}'` : predObj.value; return predObj.operator === 'equal' ? `${predObj.field} eq ${value}` : `${predObj.operator}(tolower(${predObj.field}), '${predObj.value.toLowerCase()}')`; } }).join(' and '); return `(${predicates})`; } else { // Handle simple filter const value = typeof obj.value === 'string' ? `'${obj.value.toLowerCase()}'` : obj.value; return obj.operator === 'equal' ? `${obj.field} eq ${value}` : `${obj.operator}(tolower(${obj.field}), '${obj.value.toLowerCase()}')`; } }).join(' and '); } else { filterQuery = ''; } // Construct the full query URL ajax.url = `${BASE_URL}?${pageQuery}${sortQuery}${filterQuery}&$count=true`; // Execute the request and parse the response return ajax.send().then((response: any) => { const data: any = JSON.parse(response); return { result: data['value'], // Data array count: parseInt(data['@odata.count'], 10) // Total record count }; }); } |
Attachment: NextJS_Sample_f2f07248.zip
Ηello,
Thank you for taking the time to come up with a working solution. As it turns out, I didn't need it, but it did help me solve this by adding a ref to the grid and assigning the returned data from the api call to the grid's datasource. Everything is working correctly now.
Thanks once again.
George,
We are happy to hear that the provided solution was helpful. Please get back to us if you need any other assistance.
Thank you for taking the time to come up with a working solution. As it turns out, I didn't need it, but it did help me solve this by adding a ref to the grid and assigning the returned data from the api call to the grid's datasource. Everything is working correctly now.
Ali,
We are happy to hear that the provided solution was helpful. Please get back to us if you need any other assistance.
- 9 Replies
- 4 Participants
-
GE George
- Apr 12, 2025 12:20 PM UTC
- Jul 24, 2025 06:13 AM UTC