1.
import Sortable from 'sortablejs'
<el-table
ref="dragTable"
v-loading="listLoading"
:data="list"
row-key="id"
border
fit
highlight-current-row
style="width: 100%"
>
<el-table-column
type="index"
width="50"
/>
<el-table-column label="单品内容" min-width="180px">
<template slot-scope="{row}">
<div style="display: flex;">
<img :src="row.cover" style="width: 100px;height: 50px;margin-right: 10px;">
<div style="display: flex;flex-direction: column;justify-content: space-between;">
<span>{{ row.title }}</span>
<span style="color: red;">¥{{ row.price }}</span>
</div>
</div>
</template>
</el-table-column>
</el-table>
// js
setSort() {
const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
this.sortable = Sortable.create(el, {
ghostClass: 'sortable-ghost',
setData: function(dataTransfer) {
console.log('dataTransfer', dataTransfer)
dataTransfer.setData('Text', '')
},
onEnd: evt => {
const targetRow = this.list.splice(evt.oldIndex, 1)[0] // 切割上一步数组
this.list.splice(evt.newIndex, 0, targetRow) // 下一步新数组后面添加上一步
const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
this.newList.splice(evt.newIndex, 0, tempIndex)
}
})
}
// css
.sortable-ghost{
opacity: .8;
color: #fff!important;
background: #42b983!important;
}