A brief discussion on the issue of element dragging and sorting in table

A brief discussion on the issue of element dragging and sorting in table

Recently, when using element table, I often encounter sorting problems. If it is just a simple sorting, the element official has given a specific method

//The default sorting method of the table is to sort by ID in descending order. You can change it to other order, such as
    <el-table :data="tableData" :default-sort="{prop: 'ID', order: 'descending'}">
      //When setting the sortable attribute, the sort button appears <el-table-column prop="ID" label="Seat number" sortable>
    </el-table>

However, the official element component does not support drag and drop sorting. I introduce sortablejs here to implement the drag and drop sorting function.

sortablejs GitHub address

//Install sortable.js
Install with NPM:

$ npm install sortablejs --save

//Introduce into the component import Sortable from 'sortablejs'

//Add ref attribute to the table that needs to be dragged and sorted <el-table ref="dragTable">

//Add drag event after data rendering 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() //Execute method after data rendering})
    }
    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', '')
            },
           //Drag ends execution, evt executes the drag parameter onEnd: evt => {
              //Determine whether to re-sort if (evt.oldIndex !== evt.newIndex) {
                  let data = {
                     id:this.bannerTable[evt.oldIndex].id,
                     banner_order:evt.newIndex
                  }
                  //If data submission fails, restore the old sorting apiPutBanner(data).catch(()=>{
                     const targetRow = this.bannerTable.splice(evt.oldIndex, 1)[0];
                     this.bannerTable.splice(evt.newIndex, 0, targetRow);
                  })
              }
            }
        })
    }
}

If you need to drag the column, you can refer to the following code, which is the same principle as above, so I will not go into details here.

//Row drag 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)
        }
      })
    },
    //Column drag 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)
        }
      })
    }

This is the end of this article about the element table drag and drop sorting problem. For more relevant element table drag and drop sorting content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue uses vuedraggable to implement nested multi-layer drag and drop sorting function
  • Vue implements selection, dragging and sorting effects based on vuedraggable
  • vue drag component vuedraggable API options to achieve mutual drag and sorting between boxes
  • vuedraggable+element ui realizes the drag and drop sorting effect of page controls
  • Detailed explanation of how to use vuedraggable, a drag-and-drop sorting plugin for Vue
  • Elementui table component + sortablejs to implement row drag and drop sorting sample code
  • Detailed instructions for using the vue drag component vuedraggable
  • Use vuedraggable to implement drag function from left to right
  • Use element+vuedraggable to realize image upload and drag sorting

<<:  How does MySQL connect to the corresponding client process?

>>:  A simple way to achieve scrolling effect with HTML tag marquee (must read)

Recommend

Detailed explanation of Mysql communication protocol

1.Mysql connection method To understand the MySQL...

Detailed explanation of MySQL database paradigm

Preface: I have often heard about database paradi...

Detailed steps to install Anaconda on Linux (Ubuntu 18.04)

Anaconda is the most popular python data science ...

Code analysis of synchronous and asynchronous setState issues in React

React originated as an internal project at Facebo...

MySQL account password modification method (summary)

Preface: In the daily use of the database, it is ...

RGB color table collection

RGB color table color English name RGB 16 colors ...

Getting Started Tutorial on Animating SVG Path Strokes Using CSS3

Without relying on JavaScript, pure CSS is used t...

How to create a swap partition file in Linux

Introduction to Swap Swap (i.e. swap partition) i...

JavaScript to achieve lottery effect

This article shares the specific code of JavaScri...

How to use iostat to view Linux hard disk IO performance

TOP Observation: The percentage of CPU time occup...

Detailed explanation of JavaScript closure issues

Closures are one of the traditional features of p...

Vue SPA first screen optimization solution

Table of contents Preface optimization SSR Import...

JavaScript to implement login form

This article example shares the specific code of ...