
vue Element UI 小应用:实现tab拖动应用。先看个图:

直接拖动TAB元素,然后进行排版,如何实现呢?往下看吧。
方法
使用Sortable.JS插件。对Element-UI中的el-table进行拖拽行排序。
new Sortable(example1, {
Animation: 150,
ghostclass: 'blue-background-class'
});基本使用
1、安装
npm install sortablejs --save
2、引用
import Sortable from 'sortablejs'
3、使用
<el-table id="table" :data="list" row-key="id" style="width: 500px" > <el-table-column prop="name" label="称" width="180" /> <el-table-column label="操作"> <template slot-scope="scope"> <el-button class="handle" size="mini" ><i class="el-icon-rank" /> 移动</el-button> </template> </el-table-column> </el-table>
说明:
orderNum:为排序号
handle: 使列表单元中符合选择器的元素成为拖动的手柄,只有按住拖动手柄才能使列表单元进行拖动
Array.splice() 方法有三个参数:
注意:
newarray = Array.splice(0): 表示将原数组赋给新数组,并将原数组清空。
要在el-table渲染后调用 this.rowDrop(); 方法
组件绑定是根据Id绑定的:document.querySelector('#ability-table .el-table__body-wrAPPer tbody'),要注意父组件Id和子组件Id不要重名,否则会优先绑定到父组件对应的Id元素。
element关于table拖拽排序问题
最近在使用element table的时候,经常会遇到排序的问题,如果只是简单的排序的话,element官方已经给出了指定的方法
//table的默认排序方式是按ID排序 顺序为递减 这里可以改成其它 比如 order
<el-table :data="tableData" :default-sort="{PRop: 'ID', order: 'descending'}">
//设置sortable属性时出现排序按钮
<el-table-column prop="ID" label="座号" sortable>
</el-table>但是,element官方组件并不支持拖拽排序,我在这里引入sortablejs实现拖拽排序的功能
sortablejs GitHub地址:
//sortablejs github地址 https://GITHUB.com/SortableJS/Sortable#readme
//安装sortable.js Install with NPM: $ npm install sortablejs --save //在组件内引入 import Sortable from 'sortablejs' //为需要拖拽排序的表格添加ref属性 <el-table ref="dragTable"> //在数据渲染完毕添加拖拽事件 created(){ this.getBanner() }, methods:{ async getBanner(val){ Await APIGetBanner().then((res)=>{ this.bannerTable = res.data.data; }) this.oldList = this.bannerTable.map(v => v.id); this.newList = this.oldList.slice(); this.$nextTick(() => { this.setSort() //数据渲染完毕执行方法 }) } setSort() { const el = this.$refs.dragTable.$el.querySelectorAll( '.el-table__body-wrapper > table > tbody' )[0]; this.sortable = Sortable.create(el, { // Class name for the drop placeholder, ghostClass: 'sortable-ghost', setData: function(dataTransfer) { dataTransfer.setData('text', '') }, //拖拽结束执行,evt执向拖拽的参数 onEnd: evt => { //判断是否重新排序 if(evt.oldIndex !== evt.newIndex){ let data = { id:this.bannerTable[evt.oldIndex].id, banner_order:evt.newIndex } //数据提交失败则恢复旧的排序 apiPutBanner(data).catch(()=>{ const targetRow = this.bannerTable.splice(evt.oldIndex, 1)[0]; this.bannerTable.splice(evt.newIndex, 0, targetRow); }) } } }) } }
如果需要列拖拽的话,可以参考下面的代码,和上面是一样的原理,在这里我就不赘述了。
//行拖拽
rowDrop() {
const tbody = document.queryselector('.el-table__body-wrApper tbody')
const _this = this
Sortable.create(tbody, {
onEnd({ newIndex, oldIndex }) {
const currRow = _this.tableData.splice(oldIndex, 1)[0]
_this.tableData.splice(newIndex, 0, currRow)
}
})
},
//列拖拽
columnDrop() {
const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
this.sortable = Sortable.create(wrapperTr, {
animation: 180,
delay: 0,
onEnd: evt => {
const oldItem = this.droPCol[evt.oldIndex]
this.dropcol.splice(evt.oldIndex, 1)
this.dropCol.splice(evt.newIndex, 0, oldItem)
}
})
} 


网友评论文明上网理性发言 已有0人参与
发表评论: