deleteRows method

void deleteRows(
  1. List<String> idsToDelete
)

Delete rows by ID (handles row_id OR temp_row_id)

Implementation

void deleteRows(List<String> idsToDelete) {
  if (idsToDelete.isEmpty) return;

  print('[TableController] ❌ deleteRows: ${idsToDelete.length} rows');

  final deleteSet = Set<String>.from(idsToDelete);

  final updatedOriginalRows = _state.originalRows.where((row) {
    final rowId = row['row_id']?.toString();
    final tempId = row['temp_row_id']?.toString();
    return (rowId == null || !deleteSet.contains(rowId)) &&
           (tempId == null || !deleteSet.contains(tempId));
  }).toList();

  final updatedRegularRows = _state.regularRows.where((row) {
    final rowId = row['row_id']?.toString();
    final tempId = row['temp_row_id']?.toString();
    return (rowId == null || !deleteSet.contains(rowId)) &&
           (tempId == null || !deleteSet.contains(tempId));
  }).toList();

  final updatedVisibleRows = _state.visibleRows.where((row) {
    final rowId = row['row_id']?.toString();
    final tempId = row['temp_row_id']?.toString();
    return (rowId == null || !deleteSet.contains(rowId)) &&
           (tempId == null || !deleteSet.contains(tempId));
  }).toList();

  final deletedCount = _state.originalRows.length - updatedOriginalRows.length;

  _state = _state.copyWith(
    originalRows: updatedOriginalRows,
    regularRows: updatedRegularRows,
    visibleRows: updatedVisibleRows,
    totalRowCount: updatedVisibleRows.length,
  );

  notifyListeners();
  print('[TableController] ✅ Deleted $deletedCount rows');
}