How to Retrieve Cell Details with Pagination in Flutter DataTable?
In this article, we will show you how to retrieve cell details when tapping cells with pagination without overriding the handlePageChange method in Flutter DataGrid.
Initialize the SfDataGrid widget with all the required properties. To support sorting and filtering across all pages, maintain a list of all rows in DataSource.effectiveRows. When using pagination, the rowIndex provided corresponds to the rows available on the current page. By utilizing the onPageNavigationEnd callback, which is triggered when page navigation is successfully completed, you can obtain the respective pageIndex
. This allows you to accurately calculate the rowIndex and retrieve the correct details on onCellTap when a cell is tapped.
int currentPageIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter DataGrid'),
),
body: LayoutBuilder(builder: (context, constraint) {
return Column(children: [
SizedBox(
height: constraint.maxHeight - _dataPagerHeight,
width: constraint.maxWidth,
child: _buildDataGrid(constraint)),
SizedBox(
height: _dataPagerHeight,
child: SfDataPager(
delegate: employeeDataSource,
pageCount: (employees.length / _rowsPerPage).ceil().toDouble(),
onPageNavigationEnd: (pageIndex) {
setState(() {
currentPageIndex = pageIndex;
});
},
),
)
]);
}),
);
}
Widget _buildDataGrid(BoxConstraints constraint) {
return SfDataGrid(
source: employeeDataSource,
rowsPerPage: _rowsPerPage,
columnWidthMode: ColumnWidthMode.fill,
onCellTap: (DataGridCellTapDetails details) {
// Avoid processing header row taps.
if (details.rowColumnIndex.rowIndex > 0) {
// Calculate the zero-based index across all pages.
int globalRowIndex = (currentPageIndex * _rowsPerPage) +
(details.rowColumnIndex.rowIndex - 1);
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Tapped Content'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'))
],
content: Text(employeeDataSource.effectiveRows[globalRowIndex]
.getCells()[details.rowColumnIndex.columnIndex]
.value
.toString())));
}
},
columns: <GridColumn>[
GridColumn(
columnName: 'id',
label: Container(
padding: const EdgeInsets.all(16.0),
alignment: Alignment.center,
child: const Text(
'ID',
))),
GridColumn(
columnName: 'name',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Name'))),
GridColumn(
columnName: 'designation',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text(
'Designation',
overflow: TextOverflow.ellipsis,
))),
GridColumn(
columnName: 'salary',
label: Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.center,
child: const Text('Salary'))),
],
);
}
Conclusion
I hope you enjoyed learning how to retrieve cell details with pagination in Flutter DataTable.
You can refer to our Flutter DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!