Access DataGridSource from outside class to show summary of filtered data
Hello everyone.
I have an A class that encapsulates the SfDatagrid class. It is this class that is in charge of internally constructing the DataGridSource object using a couple of input parameters, which are basically the row and column information.
The problem comes when for example I want to do the following. I want to show outside the object A a summary of the information that is seen in each moment of the table. Imagine that in the table there is a “Status” column, whose values can be “Opened”, “Closed” and “Cancelled”. Well, I want to show in a widget out of A, how many rows there are in “Cancelled”, “Opened” and “Closed” status. And I want to show this information every time the user filters something in the table. In the onFilterChanged event I don't have access to the DataGridSource to do the count.
What do you think is the most correct way to do what I want?
Hi Fernando,
You can achieve these requirements using DataGridSource.effectiveRows. It returns a copy of DataGridSource.rows, holding the sorted and filtered collection when sorting and filtering are applied in the DataGrid. In our sample, we encapsulated the SfDataGrid within a custom class called DataGrid. We created variables to store the count of each "Status." A method, countStatusRows(), is defined to count how many rows are "Opened," "Closed," and "Cancelled."
The DataGrid class accepts a callback function, onCountsChanged, as a parameter. This callback is called to pass the counts back to the parent widget (MyHomePage) whenever the counts are updated. The parent widget listens for these updates via the updateCounts method, which is triggered by the callback from the DataGrid.
If no filter is applied (i.e., filterConditions is empty), we count the rows in _employeeData. If a filter is applied, we use the filtered rows stored in effectiveRows. In the onFilterChanged event of the SfDataGrid, we call countStatusRows() to recount the statuses whenever the filter changes. This ensures that whenever the table is filtered, the summary outside of the SfDataGrid is updated in real time
We’ve
included a sample for your reference. Please review it for further details.
If there are any misunderstandings regarding your requirements, or if your
needs differ in any way, please provide us with specific and clear details.
This additional information will help us thoroughly address your request and
provide an appropriate solution.
Regards,
Abinesh P
Attachment: SfDataGrid_a6aa7f84.zip
Thank you very much
Abinesh.
The example is not exactly what I wanted but it has served as an idea for my particular case. What I have done is to publish in my class that encapsulates the SfDatagrid object, a function Function(DataGridSource source)? onFilterChangedSource to have access from outside to the DatagridSource object and to be able to control what I want in every situation I want.
I found your input helpful. Thank you.
Hi Fernando,
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help.
Regards,
Abinesh P
- 3 Replies
- 2 Participants
- Marked answer
-
FD Fernando de Miguel
- Sep 25, 2024 06:08 AM UTC
- Sep 26, 2024 11:51 AM UTC