4

Im using SelectionModel for mat-checkbox and im calling a function on each click :

 toggleSelection(row) {
    this.selection.toggle(row);
    console.log("Selection");
    console.log("this", this.selection.selected);
    this.selection.selected.forEach(selected => {
      this.masterPossibleActions = this.masterPossibleActions.filter(action => selected.possibleActions == action);
    });
    console.log(":MAster",this.masterPossibleActions)
  }

this.selection.selected is returning array of objects representing selected rows . There is a propery called possibleActions on each object. I want the masterPossibleActions Array to be the list of possible actions common between all selected rows.

PossibleACtion object class:

class ActionObject {
  key: string;
  value: string;
  constructor(key: string, value: string) {
    this.key = key;
    this.value = value;
  }
}

The toggle function:

 <td mat-cell *matCellDef="let row">
          <mat-checkbox appClickStopPropagation (change)="toggleSelection(row)" class="custom-checkbox"
            [checked]="selection.isSelected(row)" [aria-label]="checkboxLabel(row)">
          </mat-checkbox>
        </td>

this.selection is:

  selection = new SelectionModel<Enrolment>(true, []);

Enrolment object:

 export class Enrolment {
  id: string;
  user: any;
  enrollable: any;
  time_created: string;
  status: string;
  possibleActions: Array<ActionObject> = [];

  preparePossibleActions() {
    this.possibleActions = [];
    this.possibleActions.push(new ActionObject("DELETE", "Delete"));
    switch (this.status) {
      case "PENDING":
        this.possibleActions.push(new ActionObject("REMOVE", "Reject"));
        this.possibleActions.push(new ActionObject("APPROVE", "Approve"));
        break;
      case "REJECTED":
      case "CANCELLED":
      case "WITHDRAWN":
        break;
      case "APPROVED":
      case "WITHDRAW_PENDING":
      case "COMPLETED":
        this.possibleActions.push(new ActionObject("REMOVE", "Withdraw"));
        break;
      default:
        break;
    }
  }
  constructor(rawObj: any) {
    this.id = rawObj.id;
    this.user = rawObj.user;
    this.enrollable = rawObj.enrollable;
    this.time_created = rawObj.time_created;
    this.status = rawObj.status;
    this.preparePossibleActions();
  }
}

1 Answer 1

2

I'm assuming the problem you are adressing is how to filter correctly. You could try:

// masterPossibleActions is combined actions from all rows (array)
// this.selection.selected is all selected rows (need to call possibleActions)
let actions = this.selection.selected.map(selectedRow => selectedRow.possibleActions)
// initialize masterPossibleActions with first element -> can only shrink not grow because there wont be any actions that aren't in first element action's and common between all of them
this.masterPossibleActions = actions[0]
// filter the initial value to fit only common actions
this.masterPossibleActions = this.masterPossibleActions.filter(action => {
  let isCommon = true
  actions.forEach(rowActions => {
    if (rowActions.indexOf(action) < 0 ) {
      isCommon = false
    }
  })
  return isCommon
})

final function:

 toggleSelection(row) {
    this.selection.toggle(row);
    this.masterPossibleActions = actions[0]
    this.masterPossibleActions = this.masterPossibleActions.filter(action => {
      let isCommon = true
      actions.forEach(rowActions => {
        if (rowActions.indexOf(action) < 0 ) {
           isCommon = false
        }
      })
      return isCommon
    })
  }
Sign up to request clarification or add additional context in comments.

20 Comments

i want to create a final array with the common elements instead of returning a boolean
thats what it does, the boolean is inside the filter function to determine whether the action is common between all of them or not. return isCommontherefore just returns the boolean for filtering.
does it fit your needs? Maybe I've misunderstood something.
the masterPossible action is filtering out on selection of second item.. It is becoming empty. i want it to work like .. if for first row it is [1,2] and also for second row [1,2] ,i want master possible action to be [1,2] .. now it returns [].. if [1,3] and [1,2] ,it should be [1]
it could be any number of rows, [1,2],[1,3],[1,4] should return [1]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.