You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于SelectionModel的mat-checkbox多选获取共同possibleActions问题

Solution: Get Common 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, masterPossibleActions stays empty.
  • Single Selected Row: commonActions will just be that row's full possibleActions array.
  • 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 key property to compare actions because two ActionObject instances with the same key/value are still different objects (reference comparison won't work).
  • We spread the initial array ([...selectedRows[0].possibleActions]) to avoid modifying the original row's possibleActions array.

Example Scenario:

  • Row 1 has actions: DELETE, APPROVE, REMOVE
  • Row 2 has actions: DELETE, REMOVE
  • Row 3 has actions: DELETE
  • The resulting masterPossibleActions will be just [DELETE]—the only action common to all three rows.

内容的提问来源于stack exchange,提问作者Mohit Harshan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:26:16