angular - geçişte diziden ortak stringi al
Mat-checkbox için SelectionModel kullanıyorum ve her tıklamada bir fonksiyonu çağırıyorum:
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, seçilen satırları temsil eden nesneler dizisi döndürüyor. Her nesnede possibleActions adında bir özellik var. masterPossibleActions dizisinin, tüm seçilen satırlar arasında ortak olan possibleActions listesini olmasını istiyorum.
PossibleACtion nesne sınıfı:
class ActionObject {
key: string;
value: string;
constructor(key: string, value: string) {
this.key = key;
this.value = value;
}
}
Geçiş (toggle) fonksiyonu:
<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>
bu.seçim şudur:
selection = new SelectionModel<Enrolment>(true, []);
Kayıt nesnesi:
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();
}
}
Cevap #1
Sanırım ele almakta olduğunuz sorun doğru şekilde nasıl filtreleneceği ile ilgili. Şunu deneyebilirsiniz:
// 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
})
nihai fonksiyon:
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
})
}
