Topic: A row action edits a different record after filtering the table
miloCodes87
asked a week ago
We added an Edit option to the dropdown in each table row. It edits the right record until a search filter is applied. After filtering, choosing Edit on the first visible result opens the first record from the complete dataset instead.
The template passes the rendered row index to editRow, and the handler reads records[index]. We keep filteredRecords as a separate array for rendering. Is there a reliable identifier that should be passed to the action instead of the displayed index?
Arkadiusz Idzikowski
staff answered a week ago
A rendered index describes the filtered view, so it no longer points to the same position in records. Pass the row's stable ID, or the row object itself, directly from the template to the action handler.
Use that ID again when applying the saved changes to the original collection. The same rule protects the action after sorting and pagination, where visible positions also change. The dropdown controls how the action opens; the record lookup should remain based on identity.
editRow(id: string) {
const record = this.records.find(item => item.id === id);
if (record) this.openEditor(record);
}
// Bind the action to editRow(row.id), not editRow(index).
https://mdbootstrap.com/docs/angular/components/dropdowns/
miloCodes87 commented a week ago
Passing row.id fixed filtering, but saving an edited record still changes the wrong entry after sorting. I found a second index lookup in the save callback. The API response includes the record ID, so we can use it there too.
saveResult(saved: RecordItem) {
this.records[this.selectedIndex] = saved;
this.applyFilter();
}
Arkadiusz Idzikowski
staff answered 7 days ago
Yes, replace that second position-based lookup as well. Match the returned ID against the backing collection and then rebuild the filtered view. Neither the selected row's old position nor its current visible position needs to be stored.
Keep the IDs unchanged when mapping API responses into display rows. Otherwise a correct ID-based action can still lose the connection to its source record.
saveResult(saved: RecordItem) {
this.records = this.records.map(record =>
record.id === saved.id ? saved : record
);
this.applyFilter();
}
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- User: Free
- Premium support: No
- Technology: MDB Angular
- MDB Version: MDB5 9.1.0
- Device: Desktop
- Browser: Chrome
- OS: macOS
- Provided sample code: No
- Provided link: No