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
In the many projects I have worked on, there is b...
body{font-size:12px; font-family:"宋体";}...
Data backup and restoration part 2, as follows Ba...
Table of contents 1. Troubleshooting and locating...
The DIV floating effect (fixed position) is imple...
The centos8 distribution is released through the ...
The computer system is: win7 This article is main...
CJK is the abbreviation of CJK Unified Ideographs...
I have read countless my.cnf configurations on th...
The Raspberry Pi model is 4b, 1G RAM. The system ...
1. Preparation 1.1 Download the Python installati...
Table of contents 1. Arithmetic operators 2. Comp...
Table of contents Introduction to WiFi Wireless T...
mysql storage engine: The MySQL server adopts a m...
Preface Since the types of the same fields in the...