CXJS技术问询:获取选中项的所有键及实现完整对象选中方法
Got it, let's break this down into two parts to solve your CXJS questions:
1. Getting All Keys from Selected Items
Depending on whether you're dealing with a single selected object or an array of selected items, here's how to grab all keys:
- Single selected object: Use JavaScript's native
Object.keys()method directly on your bound selected object. For example:// Assuming $linkWindow.selectedObject holds your selected record const allObjectKeys = Object.keys($linkWindow.selectedObject); - Array of selected objects: You can either get keys for each individual object, or collect all unique keys across all selected items:
const selectedItems = $linkWindow.selectedObjects; // Array of selected records // Get keys for each object in the selection const keysPerItem = selectedItems.map(item => Object.keys(item)); // Get all unique keys across all selected objects const allUniqueKeys = [...new Set(selectedItems.flatMap(item => Object.keys(item)))];
2. Binding Full Objects in List Component Selection
The core issue here is that KeySelection requires explicitly listing keyFields to extract specific properties—this isn't feasible for dynamic or large sets of properties. Instead, use ObjectSelection, which is purpose-built to return the full record object when an item is selected:
Updated Code Snippet
<List records:bind="$myWindow.ObjectList" selection={{ type: ObjectSelection, // Replace KeySelection with ObjectSelection // No need for keyFields anymore - this binds the complete object bind: "$linkWindow.selectedObject" }} // ... your other List component props />
Why This Works
ObjectSelection skips the property extraction step entirely and directly binds the full record from your records array to your target variable. This means $linkWindow.selectedObject will always hold the complete selected object, even if the record's properties change dynamically or you have hundreds of fields to handle.
Once you've switched to this setup, you can use the Object.keys() method from part 1 to get all keys of the selected full object whenever you need them.
内容的提问来源于stack exchange,提问作者timo18146

