Element中Tree树结构组件中实现Shift多选

Element中Tree树结构组件中实现Shift多选

无孩子的树

用到的属性和方法

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

直接上代码

<template>
  <div class="treeDiv">
    <el-tree
      ref="tree"
      highlight-current
      :data="treeData_"
      :default-expand-all="true"
      node-key="primaryKey"
      @node-click="treeSelectChange"
    ></el-tree>
  </div>
</template>
<script>
export default {
  props: {},
  data() {
    return {
      treeData_: [
        {
          primaryKey: 1,
          label: '1-1'
        },
        {
          primaryKey: 2,
          label: '1-2'
        },
        {
          primaryKey: 3,
          label: '1-3'
        },
        {
          primaryKey: 4,
          label: '1-3'
        },
        {
          primaryKey: 5,
          label: '1-3'
        },
        {
          primaryKey: 6,
          label: '1-3'
        },
        {
          primaryKey: 7,
          label: '1-3'
        }
      ],
      treeData: [], //存放多选的选中id
    }
  },

  mounted() {},

  methods: {
    // 点击树节点
    treeSelectChange(target, node) {

      let event_ = window.event || arguments.callee.caller.arguments[0]
      let shiftKeyDowned = event_.shiftKey

      if (shiftKeyDowned) {
        if (node.checked) {
          this.treeData.forEach((ele, index) => {
            if (ele == target.primaryKey) {
              this.$refs.tree.setChecked(target, false)
              this.$refs.tree.setCurrentKey()
              this.treeData.splice(index)
            }
          })
        } else {
          this.$refs.tree.setChecked(target, true)
          this.$refs.tree.setCurrentKey(target.primaryKey)
          this.treeData.push(target.primaryKey)
        }

      } else {
        this.treeData.forEach((ele) => {
          this.$refs.tree.setCurrentKey(ele)
          this.$refs.tree.setChecked(ele, false)
        })
        this.treeData = []

        this.$refs.tree.setCurrentKey(target.primaryKey)
        this.$refs.tree.setChecked(target, true)
        this.treeData.push(target.primaryKey)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.treeDiv {
  user-select: none;
}
/deep/
  .el-tree--highlight-current
  .el-tree-node.is-current
  > .el-tree-node__content {
  background-color: #fff;
  .el-tree-node__label {
    background-color: #d5e8fc;
  }
}

/deep/
  .el-tree--highlight-current
  .el-tree-node.is-checked
  > .el-tree-node__content {
  background-color: #fff;
  .el-tree-node__label {
    background-color: #d5e8fc;
  }
}
</style>

有孩子的

别人的原创博文
Element中Tree树结构组件中实现Ctrl和Shift多选

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值