vue+element tree ----上下移动

2020-03-12  本文已影响0人  若水亦城空

default-expand-all 是否默认展开所有节点

影响 上下 移动

<template>
  <div>
    <div class="question_info_lists">
      <el-tree
        ref="tree"
        :key="tree_key"
        :data="treeData"
        node-key="id"
        :render-content="renderContent"
        :default-expanded-keys="defaultExpand"
      ></el-tree>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      treeData: [
        {
          id: 1,

          label: "一级 1",

          children: [
            {
              id: 4,

              label: "二级 1-1",

              children: [
                { id: 9, label: "三级 1-1-1", children: [] },
                {
                  id: 10,
                  label: "三级 1-1-2",
                  children: []
                },
                {
                  id: 11,
                  label: "三级 1-1-3",
                  children: []
                }
              ]
            },

            {
              id: 12,

              label: "二级 1-2",

              children: []
            },

            {
              id: 13,

              label: "二级 1-3",

              children: []
            }
          ]
        },

        {
          id: 2,

          label: "一级 2",

          children: [
            { id: 5, label: "二级 2-1", children: [] },
            {
              id: 6,
              label: "二级 2-2",
              children: []
            }
          ]
        },

        {
          id: 3,

          label: "一级 3",

          children: [
            { id: 7, label: "二级 3-1", children: [] },
            {
              id: 8,
              label: "二级 3-2",

              children: []
            }
          ]
        }
      ],

      tree_key: 0,

      defaultExpand: []
    };
  },
  methods: {
    // 节点上移

    nodeUp(node, data) {
      const parent = node.parent;

      const children = parent.data.children || parent.data;

      const cIndex = children.findIndex(d => d.id === data.id);

      if (parent.level === 0 && cIndex === 0) {
        return;
      } else if (parent.level !== 0 && cIndex === 0) {
        //不同父节点中移动
        alert("不同父节点中移动");

        // const parent2 = parent.parent

        // const children2 = parent2.data.children || parent2.data

        // const pIndex2 = parseInt(children2.findIndex(p => p.id === parent.data.id), 10) - 1

        // if (pIndex2 < 0) return

        // children2[pIndex2].children.push(data)

        // children.splice(cIndex, 1)

        // this.defaultExpand[0] = children2[pIndex2].id
      } else if (
        (parent.level === 0 && cIndex !== 0) ||
        (parent.level !== 0 && cIndex !== 0)
      ) {
        const tempChildrenNodex1 = children[cIndex - 1];

        const tempChildrenNodex2 = children[cIndex];

        this.$set(children, cIndex - 1, tempChildrenNodex2);

        this.$set(children, cIndex, tempChildrenNodex1);

        this.defaultExpand[0] = data.id;
      }

      this.tree_key++;
    },

    // 节点下移

    nodeDown(store, node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const cIndex = children.findIndex(d => d.id === data.id);
      const cLength = children.length - 1; // 最边上的节点
      const allLevel = store.data.length - 1; // 树的深度

      if (parent.level === allLevel && cIndex === cLength) {
        // 最最末的节点

        return;
      } else if (parent.level !== allLevel && cIndex === cLength) {
        //父节点不同
        alert("不能移动");
        // const parent2 = parent.parent

        // const children2 = parent2.data.children || parent2.data

        // const pIndex2 = parseInt((children2.findIndex(p => p.id === parent.data.id)), 10)

        // if (pIndex2 === allLevel) return

        // children2[pIndex2 + 1].children.push(data)

        // children.splice(cIndex, 1)

        // this.defaultExpand[0] = children2[pIndex2 + 1].id
      } else if (
        (parent.level === allLevel && cIndex !== cLength) ||
        (parent.level !== allLevel && cIndex !== cLength)
      ) {
        // 父节点相同

        const tempChildrenNodex1 = children[cIndex + 1];

        const tempChildrenNodex2 = children[cIndex];

        this.$set(children, cIndex + 1, tempChildrenNodex2);

        this.$set(children, cIndex, tempChildrenNodex1);

        this.defaultExpand[0] = data.id;
      }

      this.tree_key++;
    },

    // 结构树操作group node,

    renderContent(h, { node, data, store }) {
      return (
        <span>
          <span className="node_labe">{data.label}</span>

          <div class="tree_node_op" style="  float: right">
            <i
              class="el-icon-upload2"
              on-click={() => this.nodeUp(node, data)}
            ></i>

            <i
              class="el-icon-download"
              on-click={() => this.nodeDown(store, node, data)}
            ></i>
          </div>
        </span>
      );
    }
  }
};
</script>

<style scoped></style>

上一篇下一篇

猜你喜欢

热点阅读