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

MySQL Database Basics: A Summary of Basic Commands

Table of contents 1. Use help information 2. Crea...

CSS 3.0 text hover jump special effects code

Here is a text hovering and jumping effect implem...

JavaScript drag time drag case detailed explanation

Table of contents DragEvent Interface DataTransfe...

Implementation of the Pycharm installation tutorial on Ubuntu 18.04

Method 1: Download Pycharm and install Download a...

MySQL optimization tutorial: large paging query

Table of contents background LIMIT Optimization O...

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

Brief analysis of mysql scheduled backup tasks

Introduction In a production environment, in orde...

PyTorch development environment installation tutorial under Windows

Anaconda Installation Anaconda is a software pack...

How to install MySQL 8.0.13 in Alibaba Cloud CentOS 7

1. Download the MySQL installation package (there...

Example of Vue uploading files using formData format type

In Vue, we generally have front-end and back-end ...

How to set mysql to case insensitive

mysql set to case insensitive Windows Go to the d...

Solution to the paging error problem of MySQL one-to-many association query

The query data in the xml price inquiry contains ...