Skip to content

Commit cb3578e

Browse files
committed
perf[drag-table]:refine the effect && fixed row-key bug
1 parent fe25c4d commit cb3578e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/views/example/table/dragTable.vue

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="app-container calendar-list-container">
3-
4-
<el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
3+
<!-- Note that row-key is necessary to get a correct row order. -->
4+
<el-table :data="list" row-key="id" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
55

66
<el-table-column align="center" label="序号" width="65">
77
<template slot-scope="scope">
@@ -27,9 +27,9 @@
2727
</template>
2828
</el-table-column>
2929

30-
<el-table-column width="80px" label="重要性">
30+
<el-table-column width="100px" label="重要性">
3131
<template slot-scope="scope">
32-
<svg-icon v-for="n in +scope.row.importance" icon-class="star" class="meta-item__icon" :key="n"></svg-icon>
32+
<svg-icon v-for="n in +scope.row.importance" icon-class="star" class="icon-star" :key="n"></svg-icon>
3333
</template>
3434
</el-table-column>
3535

@@ -45,15 +45,15 @@
4545
</template>
4646
</el-table-column>
4747

48-
<el-table-column align="center" label="拖拽" width="95">
48+
<el-table-column align="center" label="拖拽" width="80">
4949
<template slot-scope="scope">
5050
<svg-icon class='drag-handler' icon-class="drag"></svg-icon>
5151
</template>
5252
</el-table-column>
5353

5454
</el-table>
5555

56-
<div class='show-d'>默认顺序 &nbsp; {{ olderList}}</div>
56+
<div class='show-d'>默认顺序 &nbsp; {{ oldList}}</div>
5757
<div class='show-d'>拖拽后顺序{{newList}}</div>
5858

5959
</div>
@@ -75,7 +75,7 @@ export default {
7575
limit: 10
7676
},
7777
sortable: null,
78-
olderList: [],
78+
oldList: [],
7979
newList: []
8080
}
8181
},
@@ -99,8 +99,8 @@ export default {
9999
this.list = response.data.items
100100
this.total = response.data.total
101101
this.listLoading = false
102-
this.olderList = this.list.map(v => v.id)
103-
this.newList = this.olderList.slice()
102+
this.oldList = this.list.map(v => v.id)
103+
this.newList = this.oldList.slice()
104104
this.$nextTick(() => {
105105
this.setSort()
106106
})
@@ -109,7 +109,12 @@ export default {
109109
setSort() {
110110
const el = document.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
111111
this.sortable = Sortable.create(el, {
112+
ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
112113
onEnd: evt => {
114+
const targetRow = this.list.splice(evt.oldIndex, 1)[0]
115+
this.list.splice(evt.newIndex, 0, targetRow)
116+
117+
// for show the changes, you can delete in you code
113118
const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
114119
this.newList.splice(evt.newIndex, 0, tempIndex)
115120
}
@@ -119,10 +124,21 @@ export default {
119124
}
120125
</script>
121126

127+
<style>
128+
.sortable-ghost{
129+
opacity: .8;
130+
color: #fff!important;
131+
background: #42b983!important;
132+
}
133+
</style>
134+
122135
<style scoped>
136+
.icon-star{
137+
margin-right:2px;
138+
}
123139
.drag-handler{
124-
width: 30px;
125-
height: 30px;
140+
width: 20px;
141+
height: 20px;
126142
cursor: pointer;
127143
}
128144
.show-d{

0 commit comments

Comments
 (0)