How to manage cell editing in Flutter DataTable?
In this article, we will show you how to manage cell editing and submission When enabling checkbox column in Flutter DataTable.
Initialize the SfDataGrid widget with all required properties. To enable the checkbox column, set the showCheckboxColumn property to true at the sample level. In SfDataGrid, the checkbox column is always added as the first column. If the showCheckboxColumn is enabled, we need to resolve the column index in onCellSubmit at the sample level. This ensures that you can perform editing and submit the new cell value to the correct cell on the DataGridRow.
class EmployeeDataSource extends DataGridSource {
….
// To check whether the checkbox column is enabled.
bool showCheckboxColumn = true;
@override
Future<void> onCellSubmit(DataGridRow dataGridRow,
RowColumnIndex rowColumnIndex, GridColumn column) async {
final dynamic oldValue = dataGridRow
.getCells()
.firstWhereOrNull((DataGridCell dataGridCell) =>
dataGridCell.columnName == column.columnName)
?.value ??
'';
final int dataRowIndex = _employeeData.indexOf(dataGridRow);
if (newCellValue == null || oldValue == newCellValue) {
return;
}
// Resolve the RowColumnIndex when showCheckboxColumn is true.
if (showCheckboxColumn) {
rowColumnIndex = RowColumnIndex(
rowColumnIndex.rowIndex, rowColumnIndex.columnIndex - 1);
}
if (column.columnName == 'ID') {
_employeeData[dataRowIndex].getCells()[rowColumnIndex.columnIndex] =
DataGridCell<int>(columnName: 'ID', value: newCellValue);
employees[dataRowIndex].id = newCellValue;
} else if (column.columnName == 'Name') {
_employeeData[dataRowIndex].getCells()[rowColumnIndex.columnIndex] =
DataGridCell<String>(columnName: 'Name', value: newCellValue);
employees[dataRowIndex].name = newCellValue.toString();
} else if (column.columnName == 'Designation') {
_employeeData[dataRowIndex].getCells()[rowColumnIndex.columnIndex] =
DataGridCell<String>(columnName: 'Designation', value: newCellValue);
employees[dataRowIndex].designation = newCellValue.toString();
} else if (column.columnName == 'Salary') {
_employeeData[dataRowIndex].getCells()[rowColumnIndex.columnIndex] =
DataGridCell<int>(columnName: 'Salary', value: newCellValue);
employees[dataRowIndex].salary = newCellValue;
} else if (column.columnName == 'Country') {
_employeeData[dataRowIndex].getCells()[rowColumnIndex.columnIndex] =
DataGridCell<String>(columnName: 'Country', value: newCellValue);
employees[dataRowIndex].country = newCellValue.toString();
}
}
}
Conclusion
I hope you enjoyed learning about how to manage cell editing in Flutter DataTable.
You can refer to our Flutter DataTable 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 DataTable 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!