current position:Home>Vue + Element tree form implement drag-and-drop sequence
Vue + Element tree form implement drag-and-drop sequence
2022-08-06 19:02:53【front-end anonymous worker】
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情 >>
Today, I will share with you the drag-and-drop sorting of the tree table,There are not many tutorials on sorting tree tables,There may still be problems,I will explain it to you in detail here,If you have such a need or find it useful,Please give a follow or favorite it,It is convenient for later review and use.
安装sortablejs
npm install sortablejs --save
复制代码
在需要的页面引入
import Sortable from 'sortablejs'
复制代码
form plusrow-key="id"
<el-table ref="table" row-key="id" :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
</el-table>
复制代码
Tree table sorting(树结构)
Tree table sorting implementation principle:Convert the tree structure to a list and then drag and drop,If not converted,The position of the drag is wrong,就出错了
data() {
return {
tableData: [
{
id: 1,
name: 'AAA',
level: 1,
children: [
{
id: 2,
name: 'A-1',
level: 2
}
]
},
{
id: 3,
name: 'BBB',
level: 1,
children: []
}
],
activeRows: [] // Data converted to a list
}
},
mounted () {
this.rowDrop()
},
methods: {
// Convert tree data to tile data
treeToTile (treeData, childKey = 'children') {
const arr = []
const expanded = data => {
if (data && data.length > 0) {
data.filter(d => d).forEach(e => {
arr.push(e)
expanded(e[childKey] || [])
})
}
}
expanded(treeData)
return arr
},
rowDrop() {
const tbody = this.$refs.table.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
Sortable.create(tbody , {
animation: 300,
onMove: () => {
this.activeRows = this.treeToTile(this.tableData) // Convert the tree structure to a list and then drag and drop
},
onEnd: e => {
//e.oldIndex为拖动一行原来的位置,e.newIndex为拖动后新的位置
if (e.oldIndex !== e.newIndex) { // Add conditions and restrictions according to your own project needs
const oldRow = this.activeRows[e.oldIndex] // The element to move
const newRow = this.activeRows[e.newIndex] // 新的元素
// Request interface ordering,Fill in the parameters according to the backend requirements
}
}
})
}
}
复制代码
这里就使用了2个方法,还有其它方法,根据自己需求来使用
方法介绍
onAdd: function (evt) { // 拖拽时候添加有新的节点的时候发生该事件
console.log('onAdd.foo:', [evt.item, evt.from])
},
onUpdate: function (evt) { // 拖拽更新节点位置发生该事件
console.log('onUpdate.foo:', [evt.item, evt.from])
},
onRemove: function (evt) { // 删除拖拽节点的时候促发该事件
console.log('onRemove.foo:', [evt.item, evt.from])
},
onStart: function (evt) { // 开始拖拽出发该函数
console.log('onStart.foo:', [evt.item, evt.from])
},
onSort: function (evt) { // 发生排序发生该事件
console.log('onUpdate.foo:', [evt.item, evt.from])
},
onEnd ({ newIndex, oldIndex }) { // 结束拖拽
let currRow = _this.tableData.splice(oldIndex, 1)[0]
_this.tableData.splice(newIndex, 0, currRow)
}
复制代码
注意点
1.如果你的onEnd
Methods are not arrow functions,如下面这样,It needs to be defined abovethis
指向,不然会报错
const _this = this
Sortable.create(tbody , {
onEnd ({ oldIndex, newIndex }) {
}
})
复制代码
2.Add drag and drop method,Need to wait for the table data to be obtained,Otherwise it may be emptytbody ,Drag and drop does not work. 可以在await
After the table data is obtained,在调用rowDrop
方法
3.If the table is refreshed,Will cause the drag to fail,The drag and drop method needs to be re-addedthis.rowDrop()
4.If the table is refreshed it will cause the page to refresh,The scroll bar is not where it was before,The page needs to be re-scrolled,体验效果不好.The solution is to record the scroll bar position,Refresh the page after dragging and scrolling to the current position automatically,The next article will explain recording scroll position,Please go to my homepage to view
结语
These are some of the problems bloggers have encountered in the project,If you have problems in use,Please say it in the comments section
copyright notice
author[front-end anonymous worker],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061858275580.html
The sidebar is recommended
- (3) write their own - YOLOv3 - loss function
- Sketch91: How to set an aligned reference object and align it according to the specified element tutorial
- Target tracking actual combat deepsort+yolov5 (on)
- Tensorflow - recurrent neural network (5) subword text classification
- Linear Algebra and Probability Theory
- Big data "killed": who am I, where am I, and what have I (was) done?
- Study together to build the react + webpack + ts framework!!!!!!(a)
- Java Data Structures and Algorithms Lesson 9 - Sorting
- Hardcore!The internal manual "MySQL Notes" written by the technical director of Ali is really strong
- How Spring solves the circular dependency problem
guess what you like
What is the goal of quantization coding?
What are the quantification methods?
I the first page of the scaffold - AST abstract syntax tree
What is the core idea of the first type of dictionary coding method, can you give an example?
iOSUIKit and Swift | Youth Training Notes
Vue about embedded iframe page?
Likou 93 - Restoring IP Address [Backtracking Algorithm]
Why is run-length encoding a better encoding method for computer desktop images?
What is the Cartesian product type in quantization coding?
Cache series: cache consistency problem solution
Random recommended
- Vue family bucket - Vue-CLI2 and Vue-CLI3 hands-on teaching
- [Tips] Mac uses commands to view the size of sub-files or sub-directories in a directory
- Why build an index?
- Descartes set type and what is the effect of quantization coding?
- [Written in the romantic moment of Qixi Lang] The solution for obtaining data when encountering http codes 206 and 302 in Go
- [Operating System] Process Creation and Destruction
- AQS synchronization component - CountDownLatch analysis and case
- Why is there an index in quantization coding?
- What is the linear combination type in quantization coding?
- Arduino Painless Development _LED8*8 Dot Matrix Experiment (Detailed)
- element ui table changes the default style, removes the border and changes the table background color
- Data Structure ----- Quick Sort
- Node.js test SMTP service
- Create Nginx docker container reverse proxy https
- Python batch get gitlab project code
- Do phrases created by the second-class dictionary method have to have a specific meaning?
- How do I select the quantitative method for the quantitative characteristics of the coding?
- What is the result after straight-sum quantization?
- What are the types of high-dimensional indexes?
- Back-end writing Swagger interface management documentation
- Windows use Telnet to test smtp
- Docker - way to modify folder mapping
- 10 easy-to-use software on mac
- SSL/TLS protocol operating mechanism in https protocol
- What is the certificate chain of trust for HTTPS?Can't you publish it yourself?
- Nginx error 413 Request Entity Too Large solution
- js data manipulation problem solving?
- After changing the scale of the screen and the ratio of the layout, the Vue project feels very slow to change the transparency of the image?
- Hand in hand with you to get started weback4.0 (1)
- How to pass the data obtained by nodejs to the front desk for use (keyword - system file)
- Chapter 24 How much do you know about proxy knowledge in Spring AOP
- The prize pool experience is bad, very dark
- C + + string container
- RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
- The vmware virtual machine is disconnected from the network (nat network) after a period of time
- RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9
- Installing vivado2019.1 is always showing "There is no valid Xilinx installation that this Update can be applied to"
- What are the characteristics of the run-length encoding algorithm?
- Question about pygame
- Self-learning crawler encounters a bottleneck, hoping to get some advice