antd vue tree table 左侧树 右侧表格 根据树选择筛选

 

 

<template>
  <div>
    <div
      style="
        display: flex;
        justify-content: space-between;
        height: 20rem;
        background: white;
        margin-bottom: 2rem;
      "
    >
      <a-tree
        row-key="index"
        :tree-data="treeData"
        default-expand-all
        style="margin-right: 1rem"
        @select="handleSelect"
      />
      <a-table
        :columns="columns"
        :data-source="tableData"
        size="small"
        :scroll="{ y: 280 }"
        :pagination="false"
      >
        <template v-slot:action>
          <a href="javascript:">Delete</a>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: '首页',
      columns: [
        {
          title: '序号',
          customRender: (t, r, index) => {
            return index + 1
          },
          width: 200,
        },
        {
          title: '用户名',
          dataIndex: 'name',
          width: 100,
        },
        {
          title: '真实姓名',
          dataIndex: 'content',
          width: 100,
        },
        {
          title: 'Action',
          key: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      list: [
        {
          title: '词典分类1',
          key: 'dictionary1',
          scopedSlots: { title: 'custom' },
          children: [],
        },
        {
          title: '词典分类2',
          key: 'dictionary2',
          scopedSlots: { title: 'custom' },
          children: [],
        },
        {
          title: '词典分类3',
          key: 'dictionary3',
          scopedSlots: { title: 'custom' },
          children: [
            {
              title: '子类1',
              key: 'sub1',
              children: [
                {
                  name: '安静',
                  content: '宁静 平静 清静',
                  creater: 'aaa',
                },
                {
                  name: '著名',
                  content: '闻名 知名 有名 盛名',
                  creater: 'aaa',
                },
              ],
            },
            {
              title: '子类2',
              key: 'sub2',
              children: [
                {
                  name: '优雅',
                  content: '典雅 高雅  优雅  淡雅',
                  creater: 'aaa',
                },
                {
                  name: '采购合同',
                  content: '买卖合同 购买合同 订购合同 销售合同',
                  creater: 'aaa',
                },
              ],
            },
          ],
        },
      ],
      tableData: [],
      treeSelected: '',
    }
  },
  computed: {
    // 过滤掉第三层显示tree数据
    treeData() {
      let list = JSON.parse(JSON.stringify(this.list))
      list = list.map((item) => {
        item.children.map((leaf) => {
          delete leaf.children
          return leaf
        })
        return item
      })
      console.log(list)
      return list
    },
  },
  watch: {
    treeSelected(val) {
      this.handleTreeSelected()
    },
  },
  mounted() {
    // 初始化treeSelected 值
    this.treeSelected = this.list[0].key
    this.handleTreeSelected()
  },
  methods: {
    handleSelect(select) {
      this.treeSelected = select[0]
    },
    // 过滤table数据
    handleTreeSelected() {
      const { treeSelected, list } = this
      const _list = JSON.parse(JSON.stringify(list))
      let tableData = []
      let isSelected = false // 是否查询到选中数据
      for (let i = 0; i < _list.length; i++) {
        const { children, key } = _list[i]
        if (key === treeSelected) {
          isSelected = true
          children.forEach((item) => {
            const { children: _children = [] } = item
            if (_children.length > 0) tableData = tableData.concat(_children)
          })
          break
        } else
          children.forEach((item) => {
            const { children = [], key } = item
            console.log(key, treeSelected, children)
            if (key === treeSelected) {
              isSelected = true
              tableData = children
            }
          })
        if (isSelected) break // 查询到 跳出循环
      }
      this.tableData = tableData
    },
  },
}
</script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少十步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值