gpt4 book ai didi

angular - 在切换时从数组中获取公共(public)字符串

转载 作者:太空狗 更新时间:2023-10-29 17:55:30 25 4
gpt4 key购买 nike

我将 SelectionModel 用于 mat-checkbox 并在每次点击时调用一个函数:

 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 返回代表所选行的对象数组。每个对象上都有一个名为 possibleActions 的属性。我希望 masterPossibleActions 数组成为所有选定行之间常见的可能操作的列表。

PossibleACtion 对象类:

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

切换函数:

 <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>

这个选择是:

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

招生对象:

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

最佳答案

我假设您要解决的问题是如何正确过滤。你可以试试:

// 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
})

最终函数:

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

关于angular - 在切换时从数组中获取公共(public)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57725289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com