el-table组件结合sortable.js实现拖拽排序

603 阅读1分钟
const tbody = document.querySelector('.el-table__body-wrapper tbody')
Sortable.create(tbody, { 
 	onEnd({ newIndex, oldIndex }) { 
 	if (newIndex !== oldIndex) { 
 		const currRow = that.tableData.splice(oldIndex, 1)[0]; 
 		that.tableData.splice(newIndex, 0, currRow);//数据互换位置 
 		//发送换位置的请求
        	...
 	} 
 } 
});

使用ElementUI中的el-table组件结合sortable.js做拖拽排序时el-table组件需要设置row-key属性,避免更新渲染错误