element的tabs标签拖拽
当前网上已经存在有相当多的案例,比如当前链接添加链接描述
按照他的使用,导致拖拽后排序错乱,排查问题后发现,newIndex, oldIndex不是下标信息,是从1开始的,而tab标签数据下标是从0开始的,因此导致错乱,以下是修改后的代码
<el-tabs class="tabSign">
<el-tab-pane
v-for="item in tabOptionList"
:key="item.name"
:label="item.name"
:name="item.name"
>
{{ item.label }}
</el-tab-pane>
</el-tabs>
import Sortable from 'sortablejs' // 插件引入
data() {
return {
tabOptionList: [
{ name: '1', label: '111' },
{ name: '2', label: '222' },
{ name: '3', label: '333' },
{ name: '4', label: '444' },
{ name: '5', label: '555' },
{ name: '6', label: '666' }
],
pageData: {
pageNum: 1,
pageSize: 10,
total: 0
}
}
},
mounted() {
this.rowDrop() // 初始化拖拽方法
},
methods: {
// 拖拽方法
rowDrop() {
const el = document.querySelector('.tabSign .el-tabs__nav') // 找到想要拖拽的那一列
const _this = this
Sortable.create(el, {
onEnd({ newIndex, oldIndex }) {
// 有时候位置会变为0,这时候默认改为1
const newIndex2 = newIndex || 1
const oldIndex2 = oldIndex || 1
const currRow = JSON.parse(JSON.stringify(_this.layout[oldIndex2 - 1]))
_this.layout.splice(oldIndex2 - 1, 1) // 删除旧的
_this.layout.splice(newIndex2 - 1, 0, currRow) // 存入新的
// 下面写自己的逻辑
}
})
}
},
此方法经过试验可行,希望对各位有所帮助