ant design vue tree添加自定义新增、修改、删除节点图标

效果图

当前选择节点后新增自定义图标,取消选中默认没有自定义图标
在这里插入图片描述

核心代码处

 // nodeDate数据结构如下
 // [
 //   {
 //     codeLine: '电化厂new',
 //     id: '111',
 //     name: '电化厂new',
 //     parentId: '',
 //     children: [
 //       {
 //         codeLine: '电化厂new-test',
 //         id: '320214938155151360',
 //         name: 'test',
 //         parentId: '111',
 //         children: [],
 //       },
 //     ],
 //   },
 // ]

// 节点点击触发事件
async onSelect(params, e) {
  if (this.filterResource) {
    this.$set(this.filterResource, 'scopedSlots', {})
  }
  // 节点选中或取消选中
  if (e.selected) {
  // 此处codeLine为业务需要,判断是否是四级节点,添加相应的自定义icon
    let codeLine = e.selectedNodes[0].data.props.codeLine
    let arr = codeLine.split('-')
    // 树节点中查找到对应的当前节点,添加scopedSlots属性
    this.filterResource = parseArray(this.nodeData, 'id', params[0])
    let iconStr
    if (arr.length < 4) {
      iconStr = 'plus'
    } else {
      iconStr = 'edit'
    }
    this.$set(this.filterResource, 'scopedSlots', { icon: iconStr })
    this.selectedKeys = params
    this.codeLine = codeLine
  } else {
    this.selectedKeys = null
    this.codeLine = ''
  }
},

完整代码展示

<template>
  <div class="wrap">
    <a-tree
      ref="nodeTree"
      :tree-data="nodeData"
      :selectedKeys="selectedKeys"
      :replace-fields="replaceFields"
      default-expand-all
      show-icon
      @select="onSelect"
    >
    <!--   新增、修改、删除图标   -->
      <template slot="plus" slot-scope="record">
        <a-icon
          type="plus"
          class="plusType"
          style="padding-left: 10px"
          @click.stop="nodeHandle(record, true)"
        ></a-icon>
        <a-icon
          type="edit"
          class="editType"
          style="padding-left: 10px"
          @click.stop="nodeHandle(record, false)"
        ></a-icon>
        <a-icon
          type="delete"
          class="deleteType"
          style="padding-left: 10px"
          @click.stop="nodeDel(record)"
        ></a-icon>
      </template>
      <!--   修改、删除图标   -->
      <template slot="edit" slot-scope="record">
        <a-icon
          type="edit"
          class="editType"
          style="padding-left: 10px"
          @click.stop="nodeHandle(record, false)"
        ></a-icon>
        <a-icon
          type="delete"
          class="deleteType"
          style="padding-left: 10px"
          @click.stop="nodeDel(record)"
        ></a-icon>
      </template>
    </a-tree>
  </div>
</template>

<script>
import {
  DelLimsConfigNodeData,
  GetLimsConfigNodeData,
} from '@/services/LIMSApis/nodeConfig'
// 数组对象中查找符合目标的对象
const parseArray = function (objArray, key, value) {
  for (let i in objArray) {
    let element = objArray[i]
    if (typeof element === 'object') {
      let result = parseArray(element, key, value)
      if (result) return result
    } else {
      if (i === key) {
        if (element === value) return objArray
      }
    }
  }
}

export default {
  name: 'index1',
  data() {
    return {
      replaceFields: {
        title: 'name',
        key: 'id',
      },
      nodeData: [],
      codeLine: '',
      selectedKeys: null,
      filterResource: null,
    }
  },
  mounted() {
    this.GetLimsConfigNode()
  },
  methods: {
    // 获取全部节点树
    async GetLimsConfigNode() {
      this.nodeData = await GetLimsConfigNodeData()
    },
    // 新增、修改节点操作,此处为打开新增修改界面,调用实际后端接口进行节点的处理操作
    async nodeHandle(record, isAdd) {
      // this.visible = true
      // this.$nextTick(() => {
      //   this.$refs.addOrUpdate.init(record, isAdd, this.nodeData)
      // })
    },
    nodeDel(record) {
      // this.$confirm({
      //   title: `您确定要删除该节点:${record.name}?`,
      //   okText: '删除',
      //   cancelText: '取消',
      //   onOk: async () => {
      //     await DelLimsConfigNodeData({ id: record.id })
      //     this.$message.success('删除成功')
      //     // 刷新当前节点
      //     await this.GetLimsConfigNode()
      //     this.selectedKeys = null
      //     this.codeLine = ''
      //   },
      // })
    },
    async onSelect(params, e) {
      if (this.filterResource) {
        this.$set(this.filterResource, 'scopedSlots', {})
      }
      if (e.selected) {
        let codeLine = e.selectedNodes[0].data.props.codeLine
        let arr = codeLine.split('-')
        this.filterResource = parseArray(this.nodeData, 'id', params[0])
        let iconStr
        if (arr.length < 4) {
          iconStr = 'plus'
        } else {
          iconStr = 'edit'
        }
        this.$set(this.filterResource, 'scopedSlots', { icon: iconStr })
        this.selectedKeys = params
        this.codeLine = codeLine
      } else {
        this.selectedKeys = null
        this.codeLine = ''
      }
    },
  },
}
</script>

<style lang="less" scoped>
.wrap {
  position: relative;
  width: 350px;
  background: #fff;
}
/deep/.ant-tree li span.ant-tree-iconEle {
  width: 14px;
  height: 14px;
  position: absolute;
  right: 70px;
  .plusType,
  .editType,
  .deleteType {
    &:hover {
      color: @primary-color;
    }
  }
}
/deep/.ant-tree-node-content-wrapper {
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值