【问题标题】:get common string from array on toggle在切换时从数组中获取公共字符串
【发布时间】:2020-01-03 14:56:22
【问题描述】:

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

this.selection 是:

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

【问题讨论】:

    标签: angular typescript angular7


    【解决方案1】:

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

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

    【讨论】:

    • 我想用公共元素创建一个最终数组,而不是返回一个布尔值
    • 这就是它的作用,布尔值在过滤器函数内部,用于确定动作是否在所有人之间通用。 return isCommontherefore 只返回布尔值进行过滤。
    • 它符合您的需求吗?也许我误解了什么。
    • masterPossible 操作正在过滤第二项的选择。它正在变为空。我希望它像 .. 如果第一行是 [1,2] 并且对于第二行 [1,2] ,我希望主可能的操作是 [1,2] .. 现在它返回 []。 . 如果 [1,3] 和 [1,2] 应该是 [1]
    • 它可以是任意数量的行,[1,2],[1,3],[1,4] 应该返回 [1]
    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 2018-10-21
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2017-05-08
    • 2018-12-21
    相关资源
    最近更新 更多