美文网首页
vue+element tree ----增加修改删除上下移动

vue+element tree ----增加修改删除上下移动

作者: 若水亦城空 | 来源:发表于2020-03-12 14:33 被阅读0次

    vue+element tree ----增加修改删除上下移动

    参见

    https://blog.csdn.net/SQY_candy/article/details/83448329

    <template>
      <div>
        <div class="exam_structure">
          <el-input
            placeholder="输入关键字进行过滤"
            v-model="filterText">
          </el-input>
          <el-button type="primary" size="small" class="add_new_question" @click="add_new_question"><i></i>添加父节点</el-button>
        </div>
    
        <div class="question_info_lists">
    
          <el-tree ref="tree" :key="tree_key" :data="treeData" node-key="id" :render-content="renderContent"
                   :expand-on-click-node="false" :default-expanded-keys="defaultExpand" show-checkbox
                   :filter-node-method="filterNode"></el-tree>
          <el-row class="add_question" v-show="add_question_flag">
    
            <el-col :span="12">
    
              <el-input v-model="new_question_name" placeholder="请输入大题名称"></el-input>
    
            </el-col>
    
            <el-col :span="12">
    
              <el-button size="mini" class="btn_sure" @click.stop="add_question_sure">确定</el-button>
    
              <el-button size="mini" class="btn_cancel" @click.stop="add_question_cancel">取消</el-button>
    
            </el-col>
    
          </el-row>
    
        </div>
      </div>
    </template>
    
    <script>
      export default {
        name: "tree1",
         data() {
    
          return {
            filterText: '',
            treeData: [{
    
              id: 1,
    
              label: '一级 1',
    
              isEdit: false,
    
              children: [{
    
                id: 4,
    
                label: '二级 1-1',
    
                isEdit: false,
    
                children: [{id: 9, label: '三级 1-1-1', isEdit: false, children: []}, {
                  id: 10,
                  label: '三级 1-1-2',
                  isEdit: false,
                  children: []
                }, {
                  id: 11,
                  label: '三级 1-1-3',
                  isEdit: false,
            children: []
                }]
    
              },
    
                {
    
                  id: 12,
    
                  label: '二级 1-2',
    
                  isEdit: false,
    
                  children: []
    
                },
    
                {
    
                  id: 13,
    
                  label: '二级 1-3',
    
                  isEdit: false,
    
                  children: []
    
                }]
    
            },
    
              {
    
                id: 2,
    
                label: '一级 2',
    
                isEdit: false,
    
                children: [{id: 5, label: '二级 2-1', isEdit: false, children: []}, {
                  id: 6,
                  label: '二级 2-2',
                  isEdit: false,
                  children: []
                }]
    
              },
    
              {
    
                id: 3,
    
                label: '一级 3',
    
                isEdit: false,
    
                children: [
                  {id: 7, label: '二级 3-1', isEdit: false, children: []},
                  {
                  id: 8,
                  label: '二级 3-2',
                  isEdit: false,
                  children: []
                }]
    
              }],
    
            add_question_flag: false,
    
            new_question_name: '',
    
            tree_key: 0,
    
            defaultExpand: []
    
          }
    
        },
        watch: {
          filterText(val) {
            this.$refs.tree.filter(val);
          }
        },
    
    
        methods: {
          filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
          },
    // 添加新大题
    
          add_new_question() {
    
            this.add_question_flag = true
    
          },
    
          add_question_sure() {   //确定
    
            const nodeObj = {id: '', label: this.new_question_name, isEdit: false, children: []}
            this.treeData.push(nodeObj)
            this.add_question_flag = false
    
          },
    
          add_question_cancel() {    //取消
    
            this.add_question_flag = false
    
            this.new_question_name = ''
    
          },
    
          //      增加
          append(store, node, data) {
            var maxid = '20'
            //新增数据
            const nodeapp = {id: ++maxid, label: '增加节点', isEdit: false, children: []}
            data.children.push(nodeapp)
            if (!node.expanded) {
              node.expanded = true
            }
            const parent = node.parent
            const children =  parent.data
            const cIndex = children.findIndex(d => d.id === data.id)
            const tempChildrenNodex2 = children[cIndex]  //拿到被添加的上一级
            console.log(tempChildrenNodex2.children[cIndex - 1])
          },
    
    // 修改
    
          nodeEdit(ev, store, data) {
    
            data.isEdit = true
    
            this.$nextTick(() => {
    
              const $input = ev.target.parentNode.parentNode.querySelector('input') || ev.target.parentElement.parentElement.querySelector('input')
    
    
              !$input ? '' : $input.focus()
    
            })
    
          },
    
          edit_sure(ev, data) {
    
            const $input = ev.target.parentNode.parentNode.querySelector('input') || ev.target.parentElement.parentElement.querySelector('input')
    
    
            if (!$input) {
    
              return false
    
            } else {
    
              data.label = $input.value
    
              data.isEdit = false
    
            }
    
          },
    
    // 节点删除
    
          nodeDelete(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)
    
          },
    
    // 节点上移
    
          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++
    
          },
    
          showOrEdit(data) {
    
            if (data.isEdit) {
    
              return <input type="text" value={data.label} on-blur={ev => this.edit_sure(ev, data)}/>
    
            } else {
    
              return <span className="node_labe">{data.label}</span>
    
            }
    
          },
    
    // 结构树操作group node,
    
          renderContent(h, {node, data, store}) {
    
            return (
    
              <span>
    
    <span class="el-icon-document">
    
    {this.showOrEdit(data)}
    
    </span>
    
    <div class="tree_node_op" style="  float: right">
    
    <i class="el-icon-edit" on-click={(ev) => this.nodeEdit(ev, store, data)}></i>
    
    <i class="el-icon-delete" on-click={() => this.nodeDelete(node, data)}></i>
    
    <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>
    <i class="el-icon-plus" on-click={() => this.append(store, node, data)}></i>
    
    </div>
    
    </span>)
    
          }
    
        }
      }
    </script>
    
    <style scoped>
    
    </style>
    
    

    也开始是 render

    treeRender(h, { store, node, data }) {
          return h(
            "span",
            {
              style: {
                display: "inline-block",
                width: "99%"
              },
              on: {
                mouseenter: e => {
                  e.path[0].style.backgroundColor = "#EBF7FF";
                },
                mouseleave: e => {
                  e.path[0].style.backgroundColor = "#F5F7F9";
                }
              }
            },
            [
              h("span", [
                h("Icon", {
                  props: {
                    type: data.fmlbh ? "ios-albums" : "md-albums"
                  },
                  style: {
                    marginRight: "8px"
                  }
                }),
                h("span", data.mlxsmc)
              ]),
              h(
                "span",
                {
                  style: {
                    display: "inline-block",
                    float: "right"
                  }
                },
                [
                  h(
                    "span",
                    {
                      style: {
                        display: "inline-block",
                        width: "0",
                        marginRight: "232px"
                      }
                    },
                    data.remark
                  ),
                  h(
                    "span",
                    {
                      style: {
                        display: "inline-block",
                        width: "0",
                        marginRight: "243px"
                      }
                    },
                    data.mlsxh
                  ),
                  h(
                    "span",
                    {
                      style: {
                        display: "inline-block",
                        width: "0",
                        marginRight: "230px"
                      }
                    },
                    data.mllx == 1 ? "卷" : "目录"
                  ),
                  h(
                    "span",
                    {
                      style: {
                        display: "inline-block",
                        width: "0",
                        marginRight: "175px"
                      }
                    },
                    data.mlbh
                  ),
                  h(
                    "span",
                    {
                      style: {
                        display: "inline-block",
                        width: "0",
                        marginRight: "235px"
                      }
                    },
                    data.createTime
                  ),
                  h(
                    "span",
                    {
                      style: {
                        display: "inline-block",
                        width: "0",
                        marginRight: "250px"
                      }
                    },
                    data.lastModifiedTime
                  ),
                  h(
                    "Button",
                    {
                      props: Object.assign(
                        {},
                        {
                          type: "warning",
                          size: "small",
                          icon: "md-create"
                        }
                      ),
                      style: {
                        fontSize: "12px !important"
                      },
                      on: {
                        click: () => {
                          this.editCatalog(data);
                        }
                      }
                    },
                    "编辑"
                  ),
                  h(
                    "Button",
                    {
                      props: Object.assign(
                        {},
                        {
                          type: "primary",
                          size: "small",
                          icon: "ios-arrow-up"
                        }
                      ),
                      style: {
                        marginLeft: "8px",
                        fontSize: "12px !important"
                      },
                      on: {
                        click: () => {
                          this.upMove(node, data);
                        }
                      }
                    },
                    "上移"
                  ),
                  h(
                    "Button",
                    {
                      props: Object.assign(
                        {},
                        {
                          type: "primary",
                          size: "small",
                          icon: "ios-arrow-down"
                        }
                      ),
                      style: {
                        marginLeft: "8px",
                        fontSize: "12px !important"
                      },
                      on: {
                        click: () => {
                          this.downMove(store, node, data);
                        }
                      }
                    },
                    "下移"
                  )
                ]
              )
            ]
          );
        },
    ```````
    
    
    

    相关文章

      网友评论

          本文标题:vue+element tree ----增加修改删除上下移动

          本文链接:https://www.haomeiwen.com/subject/wobujhtx.html