小白第三课 - 可操作的Tree

1、安装依赖

  • 命令:npm install —save @riophae/vue-treeselect

2、页面引入

  • import TreeSelect from ‘@riophae/vue-treeselect’
  • import ‘@riophae/vue-treeselect/dist/vue-treeselect.css’

3、HTML端

  • <el-tree :data="options" show-checkbox node-key="id" default-expand-all :expand-on-click-node="false" :render-content="renderContent"> </el-tree>
  • :data=”” :参数序列(数据源)
  • show-checkbox :是否显示复选框
  • node-key=”” :唯一字段标识(一般放主键)
  • :render-content=””:列表信息显示

4、引入组件

  • components: { TreeSelect }

5、获取已经递归好的后台参数

  • typeId:类型id
  • typeName:类型名称
  • parentId:父节点id
  • childrenMark:下一层节点数量,同时用做递归出口
  • typeChildrenList:子类型集合

6、前端递归拼接(已产品类型列表为例)

7、效果图

8、演示代码

<template>
  <div class="custom-tree-container">
    <div class="block">
      <p>使用 render-content</p>
      <el-tree
        :data="options"
        show-checkbox
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :render-content="renderContent">
      </el-tree>
    </div>
  </div>
</template>

<script>
  let id = 1000;
  import {
    addProductType,
    getProductTypeList
  } from "../../api/config";

  export default {
    data() {
      const data = [{
        id: 1,
        label: '一级 1',
        children: [{
          id: 4,
          label: '二级 1-1',
          children: [{
            id: 9,
            label: '三级 1-1-1'
          }, {
            id: 10,
            label: '三级 1-1-2'
          }]
        }]
      }, {
        id: 2,
        label: '一级 2',
        children: [{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 6,
          label: '二级 2-2'
        }]
      }, {
        id: 3,
        label: '一级 3',
        children: [{
          id: 7,
          label: '二级 3-1'
        }, {
          id: 8,
          label: '二级 3-2'
        }]
      }];
      return {
        data: JSON.parse(JSON.stringify(data)),
        data: JSON.parse(JSON.stringify(data)),

        options: [],
        typeChildrenList:[],
        typeName:"",
        typeId:"",
        childrenMark:"",
        parentId:""
      }
    },
    mounted() {
      this.queryCategoryList();
    },
    methods: {

      //产品类型列表
      queryCategoryList () {
        var that = this;
        getProductTypeList( that.typeName )
          .then((res) => {
            console.log("列表:",res.data)
            let result = []
            const children = this.getTree(res.data)
            let obj = {}
            obj.label = '请选择类型'
            obj.id = 0
            obj.children = children
            result.push(obj)
            this.options = result
          }).catch(error => {
          this.$Notice.error({
            title: '错误',
            desc: '网络连接错误'
          })
        })
      },
      //产品类型列表 - 递归拼接
      getTree (tree = []) {
        let arr = [];
        if (tree.length !== 0) {
          tree.forEach(item => {
            let obj = {};
            obj.label = item.typeName;
            obj.id = item.typeId;
            if(item.childrenMark !== "0") {
              obj.children = this.getTree(item.typeChildrenList);
            }
            arr.push(obj);
          });
        }
        return arr
      },

      append(data) {
        const newChild = { id: id++, label: 'testtest', children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      },

      remove(node, data) {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      },

      renderContent(h, { node, data, store }) {
        return (
          <span class="custom-tree-node">
          <span>{node.label}</span>
        <span>
        <el-button size="mini" type="text" on-click={ () => this.append(data) }>添加子项</el-button>
        <el-button size="mini" type="text" on-click={ () => this.remove(node, data) }>删除</el-button>
        </span>
        </span>);
      }
    }
  };
</script>

<style scoped>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>