Achieve resultsI originally wanted to look online to see if there were any based on antdesign, but I found that there were really few! Without further ado, here are the pictures: Introduction to sortablejs First, let’s get to know this plugin: sortablejs Here I will focus on the API I used.
put : put is used to define the settings for placing list cells into this list container, true/false/['foo','bar']/function;
2. Specific implementation 1. The first step is to initialize <s-table ref="table" size="default" class="left-table" rowKey="key" :columns="columns" :data="loadData"> </s-table> <s-table class="sort-table" ref="table2" size="default" class="left-table" rowKey="key" :columns="columns" :data="loadData"> </s-table> Specific columns and There is no need to elaborate on loadData. JS code import Sortable from 'sortablejs' methods:{ // Initialize sortable to implement drag initSortable () { var that = this var el = this.$el.querySelector('.sort-table tbody') Sortable.create(el, { handle: '.ant-table-row', animation: 150, group: { name: 'name', pull: true, put: true }, onUpdate: function (evt) { }, // When dragging starts onStart: function (evt) { }, onAdd: function (evt) { }, onRemove: function (evt) { } }) }, initSortable1 () { var that = this var el = this.$el.querySelector('.left-table tbody') Sortable.create(el, { handle: '.ant-table-row', animation: 150, group: { name: 'name', pull: true, put: true }, onUpdate: function (evt) { }, // When dragging starts onStart: function (evt) { }, onAdd: function (evt) { }, onRemove: function (evt) { } }) }, } About So far two The drag effect can be achieved between tables, but it is only a drag effect . The sorting is unique to the table on my right, but the table here does not need this sorting. And if the dragging is successful, why does it still show that there is no data ? Finally,
Considering the performance consumption, I chose the second one: data(){ return { unMatchedList: [], // unmatched data on the left dataList: [], // matched data on the right pullIndex: '', // the index of the original array drag element} } 2) Update the data source every time // When dragging starts onStart: function (evt) { that.pullIndex = evt.oldIndex }, onAdd: function (evt) { //evt.newIndex moves to the index of the new array //pullIndex the index of the dragged element in the original array that.dataList.splice(evt.newIndex, 0, that.unMatchedList[that.pullIndex]) that.dataList.forEach((item, index) => { item.sort = index + 1 }) //Notify the table view to update that.$nextTick(() => { that.$refs.table2 && this.$refs.table2.refresh(true) that.$refs.table && this.$refs.table.refresh(true) }) }, onRemove: function (evt) { that.dataList.splice(evt.oldIndex, 1) that.dataList.forEach((item, index) => { item.sort = index + 1 }) that.$nextTick(() => { that.$refs.table2 && this.$refs.table2.refresh(true) that.$refs.table && this.$refs.table.refresh(true) }) } }) 3) Implement drag and drop sorting in the same table initSortable () { var that = this var el = this.$el.querySelector('.sort-table tbody') Sortable.create(el, { handle: '.ant-table-row', animation: 150, group: { name: 'name', pull: true, put: true }, //Don't use the onEnd method here onUpdate: function (evt) { var o = evt.oldIndex var n = evt.newIndex if (o === n) { return } that.sortListAndUpdate(that.dataList, o, n) }, }) }, // Sort the data, requiring o (oldIndex) and n (newIndex) to start from 0 sortList (list, o, n) { var newTableData = JSON.parse(JSON.stringify(list)) var data = newTableData.splice(o, 1, null) newTableData.splice(o < n ? n + 1 : n, 0, data[0]) newTableData.splice(o > n ? o + 1 : o, 1) return newTableData }, /** * Sort the data and update the table, requiring o (oldIndex) and n (newIndex) to start from 0*/ sortListAndUpdate (list, o, n) { var newTableData = this.sortList(list, o, n) newTableData.forEach((item, index) => { item.sort = index + 1 }) this.$nextTick(() => { this.dataList = newTableData that.$refs.table2 && this.$refs.table2.refresh(true) }) }, Here we use the This is the end of this article about antdesign-vue combined with sortablejs to realize the function of dragging and sorting two tables. For more relevant content about antdesign-vue to realize drag and sort, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Research on the effect of page sidebar realized by JS
Solution to Ubuntu dual system stuck when startin...
Table of contents Preface Solution: Step 1 Step 2...
Install axios and implement communication Here we...
register The front-end uses axios in vue to pass ...
Preface As we all know, JavaScript is single-thre...
First, the HTML code to embed the video in the pag...
Table of contents 2. Comma operator 3. JavaScript...
Here are 30 best practices for HTML beginners. 1....
This article example shares the specific code for...
Table of contents 1. Software and system image 2....
Table of contents 1. What is a transaction? 2. Th...
I searched the entire web and found all kinds of ...
Preface Through my previous Tomcat series of arti...
01. Command Overview The paste command will merge...
Hello everyone, today I want to share with you ho...