el-table tree-propsを使用する場合、すべてのチェックボックスをサポート

1477 ワード


//   
data() {
  return {
    isCheckedAll : false
  }
}
/*   tree    --   */
handleSelectionChange(selection) {
      // this.$refs.vuetree.setCheckedNodes(this.vuetree);
      this.ids = selection.map((obj) => obj.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelectAllChange(selection) {
      this.toggleAllSelection(selection)
    },
    toggleAllSelection(selectedData) {
      this.isCheckedAll = !this.isCheckedAll
      if(this.isCheckedAll) {
        selectedData.forEach((subRow) => {
          this.toggleSelection(subRow['children'])
        })
      } else {
        this.$refs['table'].clearSelection()
      }
    },
    toggleSelection(rows) {
      if(!rows.length) {
        return
      }
      rows.forEach((row) => {
        if(!this.rowHasSelected(row)) {
          this.$refs['table'].toggleRowSelection(row)
        }
        if(row['children']) {
          this.toggleSelection(row['children'])
        }
      })
    },
    rowHasSelected(row) {
      let index = this.ids.indexOf(row.id)
      return ~index
    },
    /*   tree    --   */

注意:tableデータをロードするたびにisCheckedAllをfalseにリセットする必要があります