|
1 | 1 | # How-to-apply-initial-multi-column-sorting-in-Flutter-DataTable-SfDataGrid |
2 | | -This demo shows how to apply initial multi-column sorting in Flutter DataTable (SfDataGrid) |
| 2 | + |
| 3 | +In this article, we will show how to apply initial multi-column 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) with the necessary properties. The DataGrid supports programmatic column sorting. You can manually define [SortColumnDetails](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SortColumnDetails-class.html) objects and add them to the [SfDataGrid.source.sortedColumns](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/sortedColumns.html) collection. The DataGrid then sorts the data based on the SortColumnDetails objects in this collection. By handling this in the [initState](https://api.flutter.dev/flutter/widgets/State/initState.html) method, you can define a multi-column sort order that is automatically applied when the DataGrid is first rendered. |
| 6 | + |
| 7 | +```dart |
| 8 | + @override |
| 9 | + void initState() { |
| 10 | + super.initState(); |
| 11 | + employees = getEmployeeData(); |
| 12 | + employeeDataSource = EmployeeDataSource(employeeData: employees); |
| 13 | + //Initial-multi-column-sorting |
| 14 | + employeeDataSource.sortedColumns.addAll([ |
| 15 | + SortColumnDetails( |
| 16 | + name: 'id', |
| 17 | + sortDirection: DataGridSortDirection.ascending, |
| 18 | + ), |
| 19 | + SortColumnDetails( |
| 20 | + name: 'name', |
| 21 | + sortDirection: DataGridSortDirection.ascending, |
| 22 | + ), |
| 23 | + ]); |
| 24 | + } |
| 25 | +
|
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + return Scaffold( |
| 29 | + appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')), |
| 30 | + body: SfDataGrid( |
| 31 | + source: employeeDataSource, |
| 32 | + columnWidthMode: ColumnWidthMode.fill, |
| 33 | + columns: <GridColumn>[ |
| 34 | + GridColumn( |
| 35 | + columnName: 'id', |
| 36 | + label: Container( |
| 37 | + padding: EdgeInsets.all(16.0), |
| 38 | + alignment: Alignment.center, |
| 39 | + child: Text('ID'), |
| 40 | + ), |
| 41 | + ), |
| 42 | + GridColumn( |
| 43 | + columnName: 'name', |
| 44 | + label: Container( |
| 45 | + padding: EdgeInsets.all(8.0), |
| 46 | + alignment: Alignment.center, |
| 47 | + child: Text('Name'), |
| 48 | + ), |
| 49 | + ), |
| 50 | + GridColumn( |
| 51 | + columnName: 'designation', |
| 52 | + label: Container( |
| 53 | + padding: EdgeInsets.all(8.0), |
| 54 | + alignment: Alignment.center, |
| 55 | + child: Text('Designation', overflow: TextOverflow.ellipsis), |
| 56 | + ), |
| 57 | + ), |
| 58 | + GridColumn( |
| 59 | + columnName: 'salary', |
| 60 | + label: Container( |
| 61 | + padding: EdgeInsets.all(8.0), |
| 62 | + alignment: Alignment.center, |
| 63 | + child: Text('Salary'), |
| 64 | + ), |
| 65 | + ), |
| 66 | + ], |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | +``` |
| 71 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-apply-initial-multi-column-sorting-in-Flutter-DataTable-SfDataGrid.git). |
0 commit comments