|
1 | | -# How-to-get-total-selected-rows-apart-from-filtering-in-Flutter-DataTable |
2 | | -This demo shows How to get total selected rows apart from filtering in Flutter DataTable? |
| 1 | +# How to get total selected rows apart from filtering in Flutter DataTable (SfDataGrid)? |
| 2 | + |
| 3 | +In this article, we will show you how to get total selected rows apart from filtering 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. Maintain a flag list to track selected rows. Use the [onSelectionChanged](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onSelectionChanged.html) event to add or remove rows from this list as they are selected or deselected. This callback is triggered after a row is selected. By accessing the list, you can retrieve the total number of selected rows, even after filtering. |
| 6 | + |
| 7 | +```dart |
| 8 | +@override |
| 9 | + Widget build(BuildContext context) { |
| 10 | + return Scaffold( |
| 11 | + appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')), |
| 12 | + body: Column( |
| 13 | + children: [ |
| 14 | + TextButton( |
| 15 | + child: const Text('Total selected rows'), |
| 16 | + onPressed: () { |
| 17 | + // Get total selected rows by accessing the flag list. |
| 18 | + showDialog( |
| 19 | + context: context, |
| 20 | + builder: (BuildContext context) { |
| 21 | + return AlertDialog( |
| 22 | + title: const Text('Selected Rows'), |
| 23 | + content: Text( |
| 24 | + 'Total selected rows: ${selectedRows.length}', |
| 25 | + ), |
| 26 | + actions: [ |
| 27 | + TextButton( |
| 28 | + onPressed: () { |
| 29 | + Navigator.of(context).pop(); |
| 30 | + }, |
| 31 | + child: const Text('OK'), |
| 32 | + ), |
| 33 | + ], |
| 34 | + ); |
| 35 | + }, |
| 36 | + ); |
| 37 | + }, |
| 38 | + ), |
| 39 | + Expanded( |
| 40 | + child: SfDataGrid( |
| 41 | + controller: dataGridController, |
| 42 | + source: employeeDataSource, |
| 43 | + allowFiltering: true, |
| 44 | + selectionMode: SelectionMode.multiple, |
| 45 | + columnWidthMode: ColumnWidthMode.fill, |
| 46 | + onSelectionChanged: (addedRows, removedRows) { |
| 47 | + // Add rows if not already in selectedRows. |
| 48 | + selectedRows.addAll( |
| 49 | + addedRows.where((row) => !selectedRows.contains(row)), |
| 50 | + ); |
| 51 | +
|
| 52 | + // Remove rows if they exist in selectedRows. |
| 53 | + selectedRows.removeWhere((row) => removedRows.contains(row)); |
| 54 | + }, |
| 55 | + columns: <GridColumn>[ |
| 56 | + GridColumn( |
| 57 | + columnName: 'id', |
| 58 | + label: Container( |
| 59 | + padding: const EdgeInsets.all(16.0), |
| 60 | + alignment: Alignment.center, |
| 61 | + child: const Text('ID'), |
| 62 | + ), |
| 63 | + ), |
| 64 | + GridColumn( |
| 65 | + columnName: 'name', |
| 66 | + label: Container( |
| 67 | + padding: const EdgeInsets.all(8.0), |
| 68 | + alignment: Alignment.center, |
| 69 | + child: const Text('Name'), |
| 70 | + ), |
| 71 | + ), |
| 72 | + GridColumn( |
| 73 | + columnName: 'designation', |
| 74 | + label: Container( |
| 75 | + padding: const EdgeInsets.all(8.0), |
| 76 | + alignment: Alignment.center, |
| 77 | + child: const Text( |
| 78 | + 'Designation', |
| 79 | + overflow: TextOverflow.ellipsis, |
| 80 | + ), |
| 81 | + ), |
| 82 | + ), |
| 83 | + GridColumn( |
| 84 | + columnName: 'salary', |
| 85 | + label: Container( |
| 86 | + padding: const EdgeInsets.all(8.0), |
| 87 | + alignment: Alignment.center, |
| 88 | + child: const Text('Salary'), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ], |
| 92 | + ), |
| 93 | + ), |
| 94 | + ], |
| 95 | + ), |
| 96 | + ); |
| 97 | + } |
| 98 | +``` |
| 99 | + |
| 100 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-get-total-selected-rows-apart-from-filtering-in-Flutter-DataTable). |
0 commit comments