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();
}
}