|
1 | | -# How-to-display-the-total-row-count-at-the-bottom-in-Flutter-DataTable |
2 | | -How to display the total row count at the bottom in Flutter DataTable |
| 1 | +# How to display the total row count at the bottom, with active filtering in Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show you how to display the total row count at the bottom, with active 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) widget with all the necessary properties. You can fetch the total row count, even when filtering is applied, by using [DataGridSource.effectiveRows](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/effectiveRows.html). By utilizing WidgetsBinding.instance.addPostFrameCallback in the initState, the row count is updated after the DataGrid completes its initial build. The [onFilterChanged](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/onFilterChanged.html) callback updates the row count in the UI whenever filtering is applied. The [footer](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid/footer.html) of the SfDataGrid can display the total row count, and an additional row can be shown beneath the last row. |
| 6 | + |
| 7 | +```dart |
| 8 | + int rowCount = 0; |
| 9 | +
|
| 10 | + @override |
| 11 | + void initState() { |
| 12 | + super.initState(); |
| 13 | + employees = getEmployeeData(); |
| 14 | + employeeDataSource = EmployeeDataSource(employeeData: employees); |
| 15 | + // Update the rowCount after the data source is initialized. |
| 16 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 17 | + setState(() { |
| 18 | + rowCount = employeeDataSource.effectiveRows.length; |
| 19 | + }); |
| 20 | + }); |
| 21 | + } |
| 22 | +
|
| 23 | + void updateRowCount() { |
| 24 | + setState(() { |
| 25 | + rowCount = employeeDataSource.effectiveRows.length; |
| 26 | + }); |
| 27 | + } |
| 28 | +
|
| 29 | + @override |
| 30 | + Widget build(BuildContext context) { |
| 31 | + return Scaffold( |
| 32 | + appBar: AppBar( |
| 33 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 34 | + ), |
| 35 | + body: SfDataGrid( |
| 36 | + source: employeeDataSource, |
| 37 | + allowFiltering: true, |
| 38 | + // Footer displaying the current row count. |
| 39 | + footer: Container( |
| 40 | + color: Colors.grey[400], |
| 41 | + child: Center( |
| 42 | + child: Text( |
| 43 | + 'Total Rows count : $rowCount', |
| 44 | + style: const TextStyle(fontWeight: FontWeight.bold), |
| 45 | + ))), |
| 46 | + // Update row count when the filter is applied. |
| 47 | + onFilterChanged: (details) { |
| 48 | + updateRowCount(); |
| 49 | + }, |
| 50 | + columnWidthMode: ColumnWidthMode.fill, |
| 51 | + columns: <GridColumn>[ |
| 52 | + GridColumn( |
| 53 | + columnName: 'id', |
| 54 | + label: Container( |
| 55 | + padding: const EdgeInsets.all(16.0), |
| 56 | + alignment: Alignment.center, |
| 57 | + child: const Text( |
| 58 | + 'ID', |
| 59 | + ))), |
| 60 | + GridColumn( |
| 61 | + columnName: 'name', |
| 62 | + label: Container( |
| 63 | + padding: const EdgeInsets.all(8.0), |
| 64 | + alignment: Alignment.center, |
| 65 | + child: const Text('Name'))), |
| 66 | + GridColumn( |
| 67 | + columnName: 'designation', |
| 68 | + label: Container( |
| 69 | + padding: const EdgeInsets.all(8.0), |
| 70 | + alignment: Alignment.center, |
| 71 | + child: const Text( |
| 72 | + 'Designation', |
| 73 | + overflow: TextOverflow.ellipsis, |
| 74 | + ))), |
| 75 | + GridColumn( |
| 76 | + columnName: 'salary', |
| 77 | + label: Container( |
| 78 | + padding: const EdgeInsets.all(8.0), |
| 79 | + alignment: Alignment.center, |
| 80 | + child: const Text('Salary'))), |
| 81 | + ], |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | +``` |
| 86 | + |
| 87 | +You can download this example on [GitHub](https://github.com/SyncfusionExamples/How-to-display-the-total-row-count-at-the-bottom-in-Flutter-DataTable). |
0 commit comments