|
1 | | -# How-to-clear-sorting-in-a-Flutter-DataTable |
2 | | -This demo shows how to clear sorting in a Flutter DataTable |
| 1 | +# How to clear sorting in a Flutter DataTable? |
| 2 | + |
| 3 | +In this article, we will show you how to clear sorting in a [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 all the necessary properties. The [SfDataGrid.source.sortedColumns](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/sortedColumns.html) collection contains the sorted columns. By clearing this collection, you can remove the sorting applied to the DataGrid. We have outlined three methods for clearing the sorting in various scenarios. |
| 6 | + |
| 7 | +* Clear all sorted columns from the DataGrid. |
| 8 | +```dart |
| 9 | +employeeDataSource.sortedColumns.clear(); |
| 10 | +``` |
| 11 | +* Retain sorting on the respective column and remove sorting from the remaining columns. |
| 12 | +```dart |
| 13 | +employeeDataSource.sortedColumns.removeWhere((column) => column.name != 'name'); |
| 14 | +``` |
| 15 | +* Retain only the last sorted column and clear the remaining columns. |
| 16 | +```dart |
| 17 | +employeeDataSource.sortedColumns.retainWhere((column) => column == employeeDataSource.sortedColumns.last); |
| 18 | +``` |
| 19 | + |
| 20 | +* Sample |
| 21 | +```dart |
| 22 | +@override |
| 23 | + Widget build(BuildContext context) { |
| 24 | + return Scaffold( |
| 25 | + appBar: AppBar( |
| 26 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 27 | + ), |
| 28 | + body: Column( |
| 29 | + children: [ |
| 30 | + Row( |
| 31 | + mainAxisAlignment: MainAxisAlignment.center, |
| 32 | + children: [ |
| 33 | + TextButton( |
| 34 | + onPressed: () { |
| 35 | + // Clicking the "clear sorting" button resets all sorting states, |
| 36 | + // clearing the current sort order. |
| 37 | + if (employeeDataSource.sortedColumns.isNotEmpty) { |
| 38 | + employeeDataSource.sortedColumns.clear(); |
| 39 | + employeeDataSource.sort(); |
| 40 | + } |
| 41 | + }, |
| 42 | + child: const Text('Clear sorting')), |
| 43 | + ], |
| 44 | + ), |
| 45 | + Expanded( |
| 46 | + child: SfDataGrid( |
| 47 | + source: employeeDataSource, |
| 48 | + columnWidthMode: ColumnWidthMode.fill, |
| 49 | + allowSorting: true, |
| 50 | + showSortNumbers: true, |
| 51 | + allowMultiColumnSorting: true, |
| 52 | + columns: <GridColumn>[ |
| 53 | + GridColumn( |
| 54 | + columnName: 'id', |
| 55 | + label: Container( |
| 56 | + padding: const EdgeInsets.all(16.0), |
| 57 | + alignment: Alignment.center, |
| 58 | + child: const Text( |
| 59 | + 'ID', |
| 60 | + ))), |
| 61 | + GridColumn( |
| 62 | + columnName: 'name', |
| 63 | + label: Container( |
| 64 | + padding: const EdgeInsets.all(8.0), |
| 65 | + alignment: Alignment.center, |
| 66 | + child: const Text('Name'))), |
| 67 | + GridColumn( |
| 68 | + columnName: 'designation', |
| 69 | + label: Container( |
| 70 | + padding: const EdgeInsets.all(8.0), |
| 71 | + alignment: Alignment.center, |
| 72 | + child: const Text( |
| 73 | + 'Designation', |
| 74 | + overflow: TextOverflow.ellipsis, |
| 75 | + ))), |
| 76 | + GridColumn( |
| 77 | + columnName: 'salary', |
| 78 | + label: Container( |
| 79 | + padding: const EdgeInsets.all(8.0), |
| 80 | + alignment: Alignment.center, |
| 81 | + child: const Text('Salary'))), |
| 82 | + ], |
| 83 | + ), |
| 84 | + ), |
| 85 | + ], |
| 86 | + ), |
| 87 | + ); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-clear-sorting-in-a-Flutter-DataTable). |
0 commit comments