|
1 | | -# How-to-sort-a-column-using-a-dropdown-in-Flutter-DataTable |
2 | | -This demo shows how to sort a column using a dropdown in Flutter DataTable?. |
| 1 | +# How to sort a column using a dropdown in Flutter DataTable (SfDataGrid)? |
| 2 | + |
| 3 | +In this article, we will show you how to sort a column using a dropdown 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. To sort a column using a dropdown, manually define [SortColumnDetails](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SortColumnDetails-class.html) objects and add them to the [sortedColumns](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/sortedColumns.html) collection of [SfDataGrid.source](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource-class.html). When a column is selected from the dropdown, it is added to this collection, and the grid sorts the data accordingly. Before applying a new sort, the existing sorted columns are cleared to ensure only the selected column is sorted. To perform sorting at runtime, call the [sort()](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/sort.html) method after updating the sortedColumns collection. The dropdown allows users to choose from available columns (eg : id, name, designation, salary), triggering sorting dynamically. |
| 6 | + |
| 7 | +```dart |
| 8 | +void _sortDataGrid(String columnName) { |
| 9 | + employeeDataSource.sortedColumns.clear(); |
| 10 | + employeeDataSource.sortedColumns.add( |
| 11 | + SortColumnDetails( |
| 12 | + name: columnName, |
| 13 | + sortDirection: DataGridSortDirection.ascending, |
| 14 | + ), |
| 15 | + ); |
| 16 | + employeeDataSource.sort(); |
| 17 | + } |
| 18 | +
|
| 19 | + @override |
| 20 | + Widget build(BuildContext context) { |
| 21 | + return Scaffold( |
| 22 | + appBar: AppBar(title: const Text('Syncfusion DataGrid Sorting')), |
| 23 | + body: Column( |
| 24 | + children: [ |
| 25 | + Padding( |
| 26 | + padding: const EdgeInsets.all(8.0), |
| 27 | + child: Container( |
| 28 | + height: 40, |
| 29 | + width: 200, |
| 30 | + decoration: BoxDecoration( |
| 31 | + border: Border.all(color: Colors.grey), |
| 32 | + borderRadius: BorderRadius.circular(5), |
| 33 | + ), |
| 34 | + padding: EdgeInsets.symmetric(horizontal: 8), |
| 35 | + child: DropdownButtonHideUnderline( |
| 36 | + child: DropdownButton<String>( |
| 37 | + value: selectedColumn, |
| 38 | + hint: const Text("Select Column to Sort"), |
| 39 | + isExpanded: true, |
| 40 | + icon: Icon(Icons.arrow_drop_down), |
| 41 | + style: TextStyle(fontSize: 14, color: Colors.black), |
| 42 | + items: |
| 43 | + ["id", "name", "designation", "salary"].map(( |
| 44 | + String column, |
| 45 | + ) { |
| 46 | + return DropdownMenuItem<String>( |
| 47 | + value: column, |
| 48 | + child: Text(column.toUpperCase()), |
| 49 | + ); |
| 50 | + }).toList(), |
| 51 | + onChanged: (String? newValue) { |
| 52 | + if (newValue != null) { |
| 53 | + setState(() { |
| 54 | + selectedColumn = newValue; |
| 55 | + }); |
| 56 | + _sortDataGrid(newValue); |
| 57 | + } |
| 58 | + }, |
| 59 | + ), |
| 60 | + ), |
| 61 | + ), |
| 62 | + ), |
| 63 | + Expanded( |
| 64 | + child: SfDataGrid( |
| 65 | + source: employeeDataSource, |
| 66 | + columnWidthMode: ColumnWidthMode.fill, |
| 67 | + columns: <GridColumn>[ |
| 68 | + GridColumn( |
| 69 | + columnName: 'id', |
| 70 | + label: Container( |
| 71 | + padding: EdgeInsets.all(16.0), |
| 72 | + alignment: Alignment.center, |
| 73 | + child: Text('ID'), |
| 74 | + ), |
| 75 | + ), |
| 76 | + GridColumn( |
| 77 | + columnName: 'name', |
| 78 | + label: Container( |
| 79 | + padding: EdgeInsets.all(8.0), |
| 80 | + alignment: Alignment.center, |
| 81 | + child: Text('Name'), |
| 82 | + ), |
| 83 | + ), |
| 84 | + GridColumn( |
| 85 | + columnName: 'designation', |
| 86 | + label: Container( |
| 87 | + padding: EdgeInsets.all(8.0), |
| 88 | + alignment: Alignment.center, |
| 89 | + child: Text('Designation'), |
| 90 | + ), |
| 91 | + ), |
| 92 | + GridColumn( |
| 93 | + columnName: 'salary', |
| 94 | + label: Container( |
| 95 | + padding: EdgeInsets.all(8.0), |
| 96 | + alignment: Alignment.center, |
| 97 | + child: Text('Salary'), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ], |
| 101 | + ), |
| 102 | + ), |
| 103 | + ], |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | +``` |
| 108 | + |
| 109 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-sort-a-column-using-a-dropdown-in-Flutter-DataTable). |
0 commit comments