Articles in this section
Category / Section

How to Use Callbacks for Server Sorting in Flutter DataTable?

8 mins read

In this article, we will show how to use callbacks for server sorting in Flutter DataTable.

Initialize the SfDataGrid widget with the necessary properties. The SfDataGrid provides the following callbacks for sorting: onColumnSortChanging and onColumnSortChanged. The onColumnSortChanged callback is triggered when a column is sorted in the SfDataGrid. You can use SfDataGrid.source.sortedColumns to retrieve the sorted column details, including the sort direction. Based on this direction, you can load the corresponding data into the DataGrid. After adding new data, make sure to call notifyListeners to refresh the DataGrid when the underlying data changes.

Additionally, note that you should override the performSorting method with an empty implementation to disable built-in DataGrid sorting.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')),
      body: StreamBuilder(
        stream: loadingController.stream,
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          return Stack(
            children: [
              SfDataGrid(
                source: employeeDataSource,
                columnWidthMode: ColumnWidthMode.fill,
                allowSorting: true,
                sortingGestureType: SortingGestureType.tap,
                onColumnSortChanged: (newSortedColumn, oldSortedColumn) async {
                  // Show loading indicator
                  loadingController.add(true);
                  await Future<void>.delayed(const Duration(seconds: 2));
                  // Hide loading indicator
                  loadingController.add(false);
                  employeeDataSource._employeeData.clear();
                  for (
                    int i = 0;
                    i < employeeDataSource.sortedColumns.length;
                    i++
                  ) {
                    if (employeeDataSource.sortedColumns[i].sortDirection ==
                        DataGridSortDirection.ascending) {
                      var employee2 = getEmployeeDataAscending();
                      employeeDataSource._buildDataGridRows(employee2);
                      employeeDataSource.updateDataGridSource();
                    } else if (employeeDataSource
                            .sortedColumns[i]
                            .sortDirection ==
                        DataGridSortDirection.descending) {
                      var employee2 = getEmployeeDataDescending();
                      employeeDataSource._buildDataGridRows(employee2);
                      employeeDataSource.updateDataGridSource();
                    }
                  }
                },
                columns: <GridColumn>[
                  GridColumn(
                    columnName: 'id',
                    label: Container(
                      padding: const EdgeInsets.all(16.0),
                      alignment: Alignment.center,
                      child: const Text('ID'),
                    ),
                  ),
                  GridColumn(
                    columnName: 'name',
                    label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text('Name'),
                    ),
                  ),
                  GridColumn(
                    columnName: 'designation',
                    label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text(
                        'Designation',
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ),
                  GridColumn(
                    columnName: 'salary',
                    label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text('Salary'),
                    ),
                  ),
                ],
              ),
              if (snapshot.data == true)
                const Center(child: CircularProgressIndicator()),
            ],
          );
        },
      ),
    );
  }

class EmployeeDataSource extends DataGridSource {

….


  @override
  Future<void> performSorting(List<DataGridRow> rows) async {}

  void updateDataGridSource() {
    notifyListeners();
  }
}

View the GitHub sample here.

Conclusion
I hope you enjoyed learning about how to implement unique field support in Syncfusion® Flutter DataTable.

You can refer to our Flutter DataTable feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter DataTable example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied