基于SelectionModel的mat-checkbox多选获取共同possibleActions问题
possibleActions for All Selected Rows It looks like your current logic for calculating masterPossibleActions isn't quite right—right now, you're repeatedly filtering masterPossibleActions against each selected row's actions, which will end up only keeping actions that exactly match the last selected row's list (or empty if there's no full match). Instead, we need to compute the intersection of all possibleActions arrays from the selected rows.
Here's how to fix this step-by-step:
Step 1: Add a Helper Function to Calculate Action Intersections
First, create a helper function that takes two arrays of ActionObject and returns their intersection. Since ActionObject is a class instance, we can't compare objects directly—we'll use the key property (which is unique for each action) to match equivalent actions.
private getActionIntersection(a1: ActionObject[], a2: ActionObject[]): ActionObject[] { // Get the keys from the second array for quick lookup const a2Keys = new Set(a2.map(action => action.key)); // Return actions from a1 that have a matching key in a2 return a1.filter(action => a2Keys.has(action.key)); }
Step 2: Rewrite the toggleSelection Function
Update your toggleSelection method to compute the intersection of all selected rows' possibleActions:
toggleSelection(row) { this.selection.toggle(row); console.log("Selection:", this.selection.selected); // Reset masterPossibleActions this.masterPossibleActions = []; const selectedRows = this.selection.selected; if (selectedRows.length === 0) { // No rows selected, keep empty return; } // Start with the first row's actions as the initial intersection let commonActions = [...selectedRows[0].possibleActions]; // Iterate over remaining rows to narrow down the intersection for (let i = 1; i < selectedRows.length; i++) { commonActions = this.getActionIntersection(commonActions, selectedRows[i].possibleActions); // If at any point there are no common actions, we can break early if (commonActions.length === 0) break; } // Assign the final common actions to masterPossibleActions this.masterPossibleActions = commonActions; console.log(":MAster", this.masterPossibleActions); }
How This Works:
- No Selected Rows: If nothing is checked,
masterPossibleActionsstays empty. - Single Selected Row:
commonActionswill just be that row's fullpossibleActionsarray. - Multiple Selected Rows: We start with the first row's actions, then repeatedly intersect with each subsequent row's actions. This ensures we only keep actions that exist in all selected rows.
- Early Termination: If at any point the intersection becomes empty, we break out of the loop early to save unnecessary computations.
Key Notes:
- We use the
keyproperty to compare actions because twoActionObjectinstances with the samekey/valueare still different objects (reference comparison won't work). - We spread the initial array (
[...selectedRows[0].possibleActions]) to avoid modifying the original row'spossibleActionsarray.
Example Scenario:
- Row 1 has actions:
DELETE,APPROVE,REMOVE - Row 2 has actions:
DELETE,REMOVE - Row 3 has actions:
DELETE - The resulting
masterPossibleActionswill be just[DELETE]—the only action common to all three rows.
内容的提问来源于stack exchange,提问作者Mohit Harshan

