Topic: Expanded row controls move to another customer after sorting
Sophie Lin
priority
asked 1 week ago
Each customer row contains a dropdown and a small expandable details area. If I expand one row and then reverse the sort order, the expanded controls sometimes appear beside another customer's name.
Our loop tracks rows by their array index, and expandedRows also stores indexes. Customer IDs are unique and remain present in the rendered objects. Do both the loop and the expansion state need to use those IDs?
Arkadiusz Idzikowski
staff
answered 1 week ago
Yes. An index changes meaning when the collection is reordered. Track the rendered rows by customer ID and store expansion state using that same ID, so the DOM identity and your application state both follow the customer.
For an existing ngFor loop, return item.id from trackBy. With the built in for syntax, track row.id. Also remove expansion entries when their records are removed; keeping the state in a Set of IDs makes that cleanup straightforward.
expandedIds = new Set<string>();
trackCustomer(_index: number, customer: Customer) {
return customer.id;
}
isExpanded(customer: Customer) {
return this.expandedIds.has(customer.id);
}
https://mdbootstrap.com/docs/angular/components/dropdowns/
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Priority
- Premium support: Yes
- Technology: MDB Angular
- MDB Version: MDB5 9.1.0
- Device: Laptop
- Browser: Safari
- OS: macOS
- Provided sample code: No
- Provided link: No