1.在<el-table>中增加一列
<el-table-column label="操作" >
<template slot-scope="scope">
<el-button size="mini" type='text' :disabled="scope.$index == 0" @click.stop="sortUp(scope.$index, scope.row)">向上↑ </el-button>
<el-button size="mini" type='text' :disabled="(scope.$index + 1) == tableData.length" @click.stop="sortDown(scope.$index, scope.row)">向下↓</el-button>
</template>
</el-table-column>
2.函数如下:
// 上移按钮
sortUp (index, row) {
console.log(index)
if (index === 0) {
this.$message({
message: '已经是第一个了,爬不上去了',
type: 'warning'
})
} else {
let temp = this.tableData[index - 1]
this.$set(this.tableData, index - 1, this.tableData[index])
this.$set(this.tableData, index, temp)
}
},
// 下移按钮
sortDown (index, row) {
if (index === (this.tableData.length - 1)) {
this.$message({
message: '已经是最后一个了,没有下降空间了',
type: 'warning'
})
} else {
let i = this.tableData[index + 1]
this.$set(this.tableData, index + 1, this.tableData[index])
this.$set(this.tableData, index, i)
}
}
3.如果还需要顺序标识的话,可以用index赋值。
4.上移、下移,是否可用的判断由disabled或函数中的if逻辑实现
5.this.$set( target, key, value ),为对象添加一个新属性时调用,方法说明
target---操作对象
key---要改变的位置
value---赋值
this.$set(this.tableData, index, i)
即为,改变this.tableData的index元素为i
6.实现效果