|
1 |
| -# How-to-use-callbacks-for-server-sorting-in-Flutter-DataTable |
2 |
| -This demo shows How to use callbacks for server sorting in Flutter DataTable |
| 1 | +# How to use callbacks for server sorting in Flutter DataTable (SfDataGrid)? |
| 2 | + |
| 3 | +In this article, we will show how to use callbacks for server sorting in [Flutter DataTable](https://www.syncfusion.com/flutter-widgets/flutter-datagrid). |
| 4 | + |
| 5 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with the necessary properties. The SfDataGrid provides the following callbacks for sorting: [onColumnSortChanging](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onColumnSortChanging.html) and [onColumnSortChanged](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onColumnSortChanged.html). The onColumnSortChanged callback is triggered when a column is sorted in the SfDataGrid. You can use [SfDataGrid.source.sortedColumns](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/sortedColumns.html) 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](https://api.flutter.dev/flutter/foundation/ChangeNotifier/notifyListeners.html) to refresh the DataGrid when the underlying data changes. |
| 6 | + |
| 7 | +Additionally, note that you should override the [performSorting](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/performSorting.html) method with an empty implementation to disable built-in DataGrid sorting. |
| 8 | + |
| 9 | +```dart |
| 10 | +@override |
| 11 | + Widget build(BuildContext context) { |
| 12 | + return Scaffold( |
| 13 | + appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')), |
| 14 | + body: StreamBuilder( |
| 15 | + stream: loadingController.stream, |
| 16 | + builder: (BuildContext context, AsyncSnapshot<bool> snapshot) { |
| 17 | + return Stack( |
| 18 | + children: [ |
| 19 | + SfDataGrid( |
| 20 | + source: employeeDataSource, |
| 21 | + columnWidthMode: ColumnWidthMode.fill, |
| 22 | + allowSorting: true, |
| 23 | + sortingGestureType: SortingGestureType.tap, |
| 24 | + onColumnSortChanged: (newSortedColumn, oldSortedColumn) async { |
| 25 | + // Show loading indicator |
| 26 | + loadingController.add(true); |
| 27 | + await Future<void>.delayed(const Duration(seconds: 2)); |
| 28 | + // Hide loading indicator |
| 29 | + loadingController.add(false); |
| 30 | + employeeDataSource._employeeData.clear(); |
| 31 | + for ( |
| 32 | + int i = 0; |
| 33 | + i < employeeDataSource.sortedColumns.length; |
| 34 | + i++ |
| 35 | + ) { |
| 36 | + if (employeeDataSource.sortedColumns[i].sortDirection == |
| 37 | + DataGridSortDirection.ascending) { |
| 38 | + var employee2 = getEmployeeDataAscending(); |
| 39 | + employeeDataSource._buildDataGridRows(employee2); |
| 40 | + employeeDataSource.updateDataGridSource(); |
| 41 | + } else if (employeeDataSource |
| 42 | + .sortedColumns[i] |
| 43 | + .sortDirection == |
| 44 | + DataGridSortDirection.descending) { |
| 45 | + var employee2 = getEmployeeDataDescending(); |
| 46 | + employeeDataSource._buildDataGridRows(employee2); |
| 47 | + employeeDataSource.updateDataGridSource(); |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + columns: <GridColumn>[ |
| 52 | + GridColumn( |
| 53 | + columnName: 'id', |
| 54 | + label: Container( |
| 55 | + padding: const EdgeInsets.all(16.0), |
| 56 | + alignment: Alignment.center, |
| 57 | + child: const Text('ID'), |
| 58 | + ), |
| 59 | + ), |
| 60 | + GridColumn( |
| 61 | + columnName: 'name', |
| 62 | + label: Container( |
| 63 | + padding: const EdgeInsets.all(8.0), |
| 64 | + alignment: Alignment.center, |
| 65 | + child: const Text('Name'), |
| 66 | + ), |
| 67 | + ), |
| 68 | + GridColumn( |
| 69 | + columnName: 'designation', |
| 70 | + label: Container( |
| 71 | + padding: const EdgeInsets.all(8.0), |
| 72 | + alignment: Alignment.center, |
| 73 | + child: const Text( |
| 74 | + 'Designation', |
| 75 | + overflow: TextOverflow.ellipsis, |
| 76 | + ), |
| 77 | + ), |
| 78 | + ), |
| 79 | + GridColumn( |
| 80 | + columnName: 'salary', |
| 81 | + label: Container( |
| 82 | + padding: const EdgeInsets.all(8.0), |
| 83 | + alignment: Alignment.center, |
| 84 | + child: const Text('Salary'), |
| 85 | + ), |
| 86 | + ), |
| 87 | + ], |
| 88 | + ), |
| 89 | + if (snapshot.data == true) |
| 90 | + const Center(child: CircularProgressIndicator()), |
| 91 | + ], |
| 92 | + ); |
| 93 | + }, |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | +
|
| 98 | +class EmployeeDataSource extends DataGridSource { |
| 99 | +
|
| 100 | +…. |
| 101 | +
|
| 102 | +
|
| 103 | + @override |
| 104 | + Future<void> performSorting(List<DataGridRow> rows) async {} |
| 105 | +
|
| 106 | + void updateDataGridSource() { |
| 107 | + notifyListeners(); |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-use-callbacks-for-server-sorting-in-Flutter-DataTable). |
0 commit comments