您的位置:首页 > 其它

Sortable实现table拖拽功能

2019-04-30 11:54 225 查看

1.安装插件

[code]npm install sortablejs --save

2.使用插件 

[code]<template>
<el-table :data="tableData" id="table" row-key="id" border style="width: 300px;">
<el-table-column type="index" fixed width="50" label="序号"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column label="操作" width="70">
<template slot-scope="scope">
<i style="font-size: 24px; color: blue;" class="el-icon-menu"></i>
</template>
</el-table-column>
</el-table>
</template>

<script>
import Sortable from 'sortablejs'
export default{
name: 'tableEle',
data () {
return {
tableData: [
{
name: '张三',
id: 1
},
{
name: '李四',
id: 2
},
{
name: '王五',
id: 3
},
{
name: '赵六',
id: 4
}
]
}
},
created () {

},
mounted () {
// 调用拖拽方法
this.rowDrop()
},
methods: {
// 行拖拽
rowDrop () {
const tbody = document.querySelector('#table tbody')
const _this = this
Sortable.create(tbody, {
onEnd ({ newIndex, oldIndex }) {
const currRow = _this.tableData.splice(oldIndex, 1)[0]
_this.tableData.splice(newIndex, 0, currRow)
}
})
}
}
}
</script>

<style lang="less" scoped>

</style>

注意:1.一定要指定行的唯一标识 row-key="id"

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: