..._el-tree穿梭框">

当前位置:   article > 正文

vue el-tree树形三级穿梭框 上下位移删除子节点获取元素idJSON格式_el-tree穿梭框

el-tree穿梭框

基于element-ui的tree组件合成的穿梭框,包含上下位移删除子节点
在这里插入图片描述

<template>
  <div class="container">
     <div class="trees">
       <div class="treeP">
           <p class="title">
              <label class="la">
                 <span>
                    <el-checkbox v-model="checked" @change="checkedAll()">全选</el-checkbox>
                 </span>
              </label>
           </p>
           <div class="tree1">
              <el-tree
                :data="data"
                show-checkbox
                node-key="id"
                default-expand-all
                ref="tree"
                >
              </el-tree>
          </div>
       </div>
         
          <div class="btn">
             <el-button type="primary" @click="btn" icon="el-icon-arrow-right" circle></el-button>
          </div>
          <div class="treeP">
            <p class="title">
              <label class="la">
                 <span class="quit" @click="quit">
                    清空
                 </span>
              </label>
            </p>
             <div class="tree1">
               <el-tree
                 :data="data1"
                 node-key="id"
                 draggable
                 default-expand-all
                 :expand-on-click-node="true">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                  <span>{{ node.label }}</span>
                  <span>
                    <el-button
                      type="text"
                      size="mini"
                      @click="() => remove(node, data)">
                      删除
                    </el-button>
                  </span>
                </span>
               </el-tree>
             </div>
          </div>
          
     </div>
     <div style="width:50px;margin:30px auto;">
        <el-button type="primary" @click="ti">提交</el-button>
     </div>
     
  </div>
</template>


<script>
  export default {
    data() {
      return {
        checked:false,
        checked2:false,
        data1:[],
        data: [{
          id: 1,
          label: '一级 1',
          children: [
            {
              id: 4,
              label: '二级 1-1',
               children:[
                 {
                   id:11,
                   label: '三级 1-1-1'
                 },
                 {
                   id:12,
                   label: '三级 1-1-2'
                 },
               ]
            },
            {
              id: 5,
              label: '二级 1-2',
               children:[
                 {
                   id:13,
                   label: '三级 1-2-1'
                 },
                 {
                   id:14,
                   label: '三级 1-2-2'
                 },
               ]
            },
          ]
        }, {
          id: 2,
          label: '一级 2',
          children: [
            {
            id: 6,
            label: '二级 2-1',
             children:[
                 {
                   id:15,
                   label: '三级 2-1-1'
                 },
                 {
                   id:16,
                   label: '三级 2-1-2'
                 },
               ]
            },
            {
            id: 7,
            label: '二级 2-2',
             children:[
                 {
                   id:17,
                   label: '三级 2-2-1'
                 },
                 {
                   id:18,
                   label: '三级 2-2-2'
                 },
               ]
            }
          ]
        }, {
          id: 3,
          label: '一级 3',
          children: [{
            id: 8,
            label: '二级 3-1',
             children:[
                 {
                   id:19,
                   label: '三级 3-1-1'
                 },
                 {
                   id:20,
                   label: '三级 3-1-2'
                 },
               ]
            },
            {
            id: 9,
            label: '二级 3-2',
             children:[
                 {
                   id:21,
                   label: '三级 3-2-1'
                 },
                 {
                   id:22,
                   label: '三级 3-2-2'
                 },
               ]
            },
           {
            id: 10,
            label: '二级 3-3',
             children:[
                 {
                   id:23,
                   label: '三级 3-3-1'
                 },
                 {
                   id:24,
                   label: '三级 3-3-2'
                 },
               ]
          },]
        }],
        vuetree: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
      };
    },
    methods: {
      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);
      },
      checkedAll(){
        this.checked ?  this.$refs.tree.setCheckedNodes(this.data) : this.$refs.tree.setCheckedKeys([]);
      },
      quit(){
        this.data1 = []
      },
      btn(){
         var arrs = JSON.parse(JSON.stringify(this.getSimpleCheckedNodes(this.$refs.tree.store)))
        //  console.log(arrs)
         for(let i=arrs.length - 1; i>0;i--) {
            if(arrs[i].id === arrs[i-1].id){
              arrs[i-1].children.push(...arrs[i].children)
              arrs.splice(i,1)
            }
          }
         this.data1 = arrs
      },
      getSimpleCheckedNodes(store) {
        const checkedNodes = [];
        const traverse = function(node) {
          const childNodes = node.root ? node.root.childNodes : node.childNodes;
          childNodes.forEach(child => {
            if (child.checked) {
              // console.log(child)
              if(child.childNodes.length>0){
              //  console.log(child.data)
                if(child.parent.parent == null){
                   checkedNodes.push(child.data);
                }else{
                     var arr = JSON.parse(JSON.stringify(child.parent.data))
                   var obj = child.data
                   arr.children.length = 0
                   arr.children.push(obj)
                   checkedNodes.push(arr);
                }
              }else{
                // 判断第二级菜单!!!
                if(!child.data.children){
                  var arrP = JSON.parse(JSON.stringify(child.parent.parent.data))
                  var arr = JSON.parse(JSON.stringify(child.parent.data))
                  var obj = child.data
                  arrP.children.length = 0
                  arr.children.length = 0
                  arr.children.push(obj)
                  arrP.children.push(arr)
                  checkedNodes.push(arrP);
                }
              }   
            }
            if (child.indeterminate) {
              traverse(child);
            }
          });
        };
        traverse(store)
        return checkedNodes;
      },
      ti(){
        var as = JSON.parse(JSON.stringify(this.data1))
        for(var item of as){
          delete item.label
           for(var list of item.children){
             delete list.label
             for(var i of list.children){
               delete i.label
             }
           }
        }
        console.log(JSON.stringify(as))
      }
    }
  };
</script>

<style>
.container{
    width: 1200px;
    height: 700px;
    margin:50px auto;
}
  .trees{
    width: 80%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
  }
   .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
  .tree1{
    height: 500px;
    padding: 5px;
    overflow: scroll;
    overflow-x:hidden ;
  }
  .treeP{
    width:40%;border: 1px solid #ebeef5;border-radius:5px;
  }
  .title{
    height: 40px;
    background: #f5f7fa;
  }
  .la{
   line-height: 40px;padding:0 10px 0 10px;
  }
 .quit{
   font-size:14px;margin-left:83%;cursor: pointer;
 }
 .quit:hover{
   opacity: .5;
 }
 .btn{
     margin: auto; 
  }
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/223973
推荐阅读
相关标签