fix(BTableLite): Use primary key to persist row-details state when items change #2871
+110
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using BTable/BTableLite with row-details, if the items array is replaced with new object references (even with the same data), the row-details would be hidden. This commonly happens in "Load more" scenarios where the entire items array is recreated with new objects.
Root Cause: The
detailsMapused aWeakMapwith object references as keys. When items were replaced with new objects, the WeakMap lost track of which rows had details open because the object references changed.Solution
Updated BTableLite to use primary keys for tracking row-details state when the
primary-keyprop is provided:detailsMapfromWeakMap<object>toMap<object | string>to support both primary key strings and object referencesgenerateDetailsItemto return the primary key string whenprimaryKeyprop is provided and the item contains that keygetDetailsMapKeyhelper function for consistent map key generation throughout the componentgetDetailsMapKey(item)for map lookupsprimaryKeyis provided (maintains full backward compatibility)Usage
To enable row-details persistence across item updates, provide a
primary-keyprop:When items are updated (e.g., in a "Load more" scenario):
Demo
The fix is demonstrated with two tables - one with
primary-keyand one without:primary-key): Alice's details remain open after clicking "Load More" ✅primary-key): Alice's details are closed after clicking "Load More" (expected behavior for backward compatibility)Changes
Core Implementation (
BTableLite.vue):detailsMap:WeakMap<object>→Map<object | string>to support string keysgenerateDetailsItem: Returns primary key string whenprimaryKeyprop is providedgetDetailsMapKey: New helper function for consistent key generationgetDetailsMapKey(item)instead of directitemreferencesTests (
table-lite.spec.ts):Backward Compatibility
✅ Fully backward compatible - when no
primaryKeyis provided, the component uses object references as before. This is a progressive enhancement that only activates when theprimary-keyprop is used.Testing
Fixes #[issue-number]
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.