当前位置:   article > 正文

vue+element ui+vuedraggable实现表格内不同格子间标签的拖拽_element ui如何使用draggable

element ui如何使用draggable


一、效果视频

最近有个需求是实现在表格内上下不同格子间标签的拖拽,然而element ui并没有提供此类api,后面我导入vuedraggable包实现了此需求,效果见视频。

demo视频:

element ui表格内标签拖拽demo

二、代码实现

首先要去下载vuedraggable包

npm i vuedraggable
  • 1

package.json文件里看包是否下载成功
在这里插入图片描述
下载完成后新建页面

页面完整代码如下:

ps:若要实现表格横向的移动,将代码中的:group="jndex + 1"改为group="a"即可

<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="tableData"
      :max-height="maxHeight"
      class="small-table"
      border
      style="width: 100%"
    >
      <el-table-column align="center" prop="week_date" min-width="160">
      </el-table-column>
      <el-table-column
        align="center"
        prop="start_date"
        label="日期"
        min-width="160"
      />
      <template v-for="(jtem, jndex) in list">
        <el-table-column align="center" :label="jtem.name" min-width="160">
          <template slot-scope="scope">
            <template v-for="(item, index) in jtem.taskList">
              <template v-if="scope.$index === index">
                <draggable
                  :list="item.ground"
                  class="list-group"
                  animation="500"
                  handle=".el-tag"
                  :group="jndex + 1"
                  @start="dragStart($event, jndex)"
                  @end="dragEnd($event, jndex)"
                >
                  <template v-if="item.ground.length > 0">
                    <div
                      class="list-group-item"
                      v-for="(item2, index2) in item.ground"
                      :key="item2.groundId"
                    >
                      <el-tag style="margin: 5px 0">{{ item2.name }}</el-tag>
                    </div>
                  </template>
                </draggable>
              </template>
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>

<script>
import draggable from "vuedraggable";
export default {
  components: {
    draggable
  },
  data() {
    return {
      loading: false,
      maxHeight: window.innerHeight - 250,
      tableData: [
        {
          start_date: "2019-12-23",
          week_date: "星期一"
        },
        {
          start_date: "2019-12-24",
          week_date: "星期二"
        },
        {
          start_date: "2019-12-25",
          week_date: "星期三"
        },
        {
          start_date: "2019-12-26",
          week_date: "星期四"
        },
        {
          start_date: "2019-12-27",
          week_date: "星期五"
        },
        {
          start_date: "2019-12-28",
          week_date: "星期六"
        },
        {
          start_date: "2019-12-29",
          week_date: "星期日"
        }
      ],
      departmentList: [],
      list: [
        {
          name: "小王",
          taskList: [
            {
              ground: [
                {
                  groundId: 1,
                  name: "John1",
                  no: "1"
                },
                {
                  groundId: 2,
                  name: "heo1",
                  no: "2"
                },
                {
                  groundId: 3,
                  name: "mary1",
                  no: "3"
                }
              ]
            },
            {
              ground: [
                {
                  groundId: 11,
                  name: "John2",
                  no: "4"
                },
                {
                  groundId: 12,
                  name: "heo2",
                  no: "5"
                }
              ]
            },
            {
              ground: [
                {
                  groundId: 21,
                  name: "John3",
                  no: "6"
                }
              ]
            },
            {
              ground: []
            },
            {
              ground: []
            },
            {
              ground: []
            },
            {
              ground: []
            }
          ]
        },
        {
          name: "小lv",
          taskList: [
            {
              ground: [
                {
                  groundId: 150,
                  name: "John1",
                  no: "45"
                },
                {
                  groundId: 250,
                  name: "heo1",
                  no: "55"
                },
                {
                  groundId: 350,
                  name: "mary1",
                  no: "65"
                }
              ]
            },
            {
              ground: [
                {
                  groundId: 1150,
                  name: "John2",
                  no: "75"
                },
                {
                  groundId: 1250,
                  name: "heo2",
                  no: "85"
                }
              ]
            },
            {
              ground: [
                {
                  groundId: 2150,
                  name: "John3",
                  no: "95"
                }
              ]
            },
            {
              ground: []
            },
            {
              ground: []
            },
            {
              ground: []
            },
            {
              ground: []
            }
          ]
        }
      ],
      listNo: []
    };
  },
  methods: {
    dragStart(e, listIndex) {
      this.listNo = [];
      this.list[listIndex].taskList.forEach(item => {
        let array = item.ground.map(item2 => {
          return item2.no;
        });
        this.listNo = [...this.listNo, ...array];
      });
      console.log(this.listNo);
    },
    dragEnd(e, listIndex) {
      this.list[listIndex].taskList.forEach(item => {
        item.ground.forEach(item2 => {
          item2.no = this.listNo.shift();
        });
      });
      console.log(this.list[listIndex].taskList);
    }
  }
};
</script>

<style scoped>
.el-tag {
  cursor: move;
}
</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

其中dragStart函数是拖拽点击开始时所触发,dragEnd函数是拖拽完后触发

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/471357
推荐阅读
相关标签
  

闽ICP备14008679号