vue中使用ztree实现列表tree, 支持新增节点,编辑, 删除

由于官方网址访问,不了,东拼西凑终于搞出了的demo
创建一个子组件, 复制即可用

<template>
  <div>
    <div>
      <!-- 操作按钮 -->
      <Button @click="addNode" type="primary">添加</Button>
      <Button @click="editNode" type="success">编辑</Button>
      <Button @click="removeNode" type="error">删除</Button>
    </div>
    <table class="table ztree-table">
      <thead class="thead">
        <tr>
          <th >名称</th>
          <th >编号</th>
          <th >更新时间</th>
          <th >来源</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td colspan="4">
            <ul id="ztree-table" class="ztree"></ul>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import '@ztree/ztree_v3/js/jquery-1.4.4.min'
import '@ztree/ztree_v3/js/jquery.ztree.all.min'
import '@ztree/ztree_v3/css/zTreeStyle/zTreeStyle.css'
// import '../tree/css/metroStyle.css'

export default {
  name: 'tree-table',
  props: {
    tabIndex: {
      type: Number,
      default () {
        return 0
      }
    },
    ztreeData: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      treeObj: null,
      currentSelectedNode: null
    }
  },
  mounted () {
  },
  watch: {
    ztreeData (nVal, oVal) {
      if (nVal) {
        this.initZTreeTable()
      }
    }
  },
  methods: {
    initZTreeTable () {
      const setting = {
        async: {
          enable: true,
          url: '/app/courseParameter/getFirstAtage',
          autoParam: ['id=cpId'],
          otherParam: ['cardType', `${this.tabIndex}`],
          dataFilter: (treeId, parentNode, childNodes) => {
            console.log(treeId)
            console.log(parentNode)
            console.log(childNodes)
            if (childNodes.data.length > 0) {
              return childNodes
            }
          },
          type: 'get'
        },
        view: {
          showLine: false,
          showIcon: false,
          addDiyDom: this.addCustomDom
        },
        data: {
          simpleData: {
            enable: true
          }
        },
        callback: {
          onClick: this.onNodeClick
        }
      }
      // 示例静态数据
      const zNodes = [...this.ztreeData]

      this.treeObj = $.fn.zTree.init($('#ztree-table'), setting, zNodes)
    },
    addCustomDom (treeId, treeNode) {
      const aObj = $(`#${treeNode.tId}_a`)
      aObj.css('width', '100%')
      // <span class="custom-td" style="display:inline-block">${treeNode.name}</span>
      const tableContent = `
        <span class="custom-td" >${treeNode.cpCode || '无编号'}</span>
        <span class="custom-td" >${treeNode.cpUpdateTime || '未知时间'}</span>
        <span class="custom-td" >${treeNode.cpSource === 0 ? '平台数据' : '系统数据'}</span>
      `
      aObj.append(tableContent)
    },
    onNodeClick (event, treeId, treeNode) {
      this.currentSelectedNode = treeNode
    },
    addNode () {
      if (this.currentSelectedNode) {
        const newNode = {
          id: new Date().getTime(),
          pId: this.currentSelectedNode.id, // 将新节点作为选中节点的子节点
          name: '新节点',
          code: '新编号',
          updateTime: '2024-09-03',
          source: '系统D'
        }
        this.treeObj.addNodes(this.currentSelectedNode, newNode)
      } else {
        alert('请选择一个节点进行添加子节点')
      }
    },
    editNode () {
      if (this.currentSelectedNode) {
        this.currentSelectedNode.name = '已编辑节点'
        this.currentSelectedNode.code = '已编辑编号'
        this.currentSelectedNode.updateTime = '2024-09-03'
        this.currentSelectedNode.source = '已编辑来源'
        this.treeObj.updateNode(this.currentSelectedNode)
      } else {
        alert('请选择一个节点进行编辑')
      }
    },
    removeNode () {
      if (this.currentSelectedNode) {
        this.treeObj.removeNode(this.currentSelectedNode)
        this.currentSelectedNode = null
      } else {
        alert('请选择一个节点进行删除')
      }
    }
  }
}
</script>

<style scoped lang="less">
.ztree-table {
  width: 60%;
  margin-top: 20px;
  border-collapse: collapse;
}
.thead {
  background: #eee;
  height: 50px;
}
.ztree-table th,
.ztree-table td {
  width: 100px;
  padding: 8px;
  text-align: left;
}

.ztree-table .ztree {
  width: 100%;
}

.ztree-table .ztree li {
  margin: 0;
  padding: 0;
  list-style: none;
}

/deep/.ztree li {
  padding: 5px 0;
}

.level0 {
  /deep/ a {
    width: 100%;
  }
}
/deep/.node_name, /deep/.custom-td {
  width: 250px !important;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>

最后再来张效果图,样式有需要自己优化
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值