|
1 | | -# How-to-get-sorted-column-details-in-Flutter-DataTable |
2 | | -This demo shows how to get sorted column details in Flutter DataTable?. |
| 1 | +# How to get sorted column details in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show how to get sorted column details 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. SfDataGrid provides the following callbacks for sorting: The [onColumnSortChanging](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onColumnSortChanging.html) callback is triggered when a column is being sorted, while the [onColumnSortChanged](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onColumnSortChanged.html) callback is triggered after a column has been sorted. Using onColumnSortChanged, you can retrieve the sorted column details through the newSortedColumn parameter, which includes the [column name](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SortColumnDetails/name.html) and [sort direction](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SortColumnDetails/sortDirection.html). |
| 6 | + |
| 7 | +```dart |
| 8 | +@override |
| 9 | + Widget build(BuildContext context) { |
| 10 | + return Scaffold( |
| 11 | + appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')), |
| 12 | + body: SfDataGrid( |
| 13 | + source: employeeDataSource, |
| 14 | + columnWidthMode: ColumnWidthMode.fill, |
| 15 | + allowSorting: true, |
| 16 | + onColumnSortChanged: (newSortedColumn, oldSortedColumn) { |
| 17 | + if (newSortedColumn != null) { |
| 18 | + showDialog( |
| 19 | + context: context, |
| 20 | + builder: (BuildContext context) { |
| 21 | + return AlertDialog( |
| 22 | + title: Text("Sorted Column"), |
| 23 | + content: Text( |
| 24 | + "Column: ${newSortedColumn.name}\n" |
| 25 | + "Sort Direction: ${newSortedColumn.sortDirection}", |
| 26 | + ), |
| 27 | + actions: [ |
| 28 | + TextButton( |
| 29 | + onPressed: () { |
| 30 | + Navigator.of(context).pop(); |
| 31 | + }, |
| 32 | + child: Text("OK"), |
| 33 | + ), |
| 34 | + ], |
| 35 | + ); |
| 36 | + }, |
| 37 | + ); |
| 38 | + } |
| 39 | + }, |
| 40 | + columns: <GridColumn>[ |
| 41 | + GridColumn( |
| 42 | + columnName: 'id', |
| 43 | + label: Container( |
| 44 | + padding: EdgeInsets.all(16.0), |
| 45 | + alignment: Alignment.center, |
| 46 | + child: Text('ID'), |
| 47 | + ), |
| 48 | + ), |
| 49 | + GridColumn( |
| 50 | + columnName: 'name', |
| 51 | + label: Container( |
| 52 | + padding: EdgeInsets.all(8.0), |
| 53 | + alignment: Alignment.center, |
| 54 | + child: Text('Name'), |
| 55 | + ), |
| 56 | + ), |
| 57 | + GridColumn( |
| 58 | + columnName: 'designation', |
| 59 | + label: Container( |
| 60 | + padding: EdgeInsets.all(8.0), |
| 61 | + alignment: Alignment.center, |
| 62 | + child: Text('Designation', overflow: TextOverflow.ellipsis), |
| 63 | + ), |
| 64 | + ), |
| 65 | + GridColumn( |
| 66 | + columnName: 'salary', |
| 67 | + label: Container( |
| 68 | + padding: EdgeInsets.all(8.0), |
| 69 | + alignment: Alignment.center, |
| 70 | + child: Text('Salary'), |
| 71 | + ), |
| 72 | + ), |
| 73 | + ], |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | +``` |
| 78 | + |
| 79 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-get-sorted-column-details-in-Flutter-DataTable). |
0 commit comments