vue element el-transfer adds drag function

vue element el-transfer adds drag function

The Core Asset Management Project requires el-transfer to add drag-and-drop sorting and drag-and-drop functions to the left, right, up, and down sides;
The original component does not support dragging, so we need to use a third-party component sortablejs

First install

 sudo npm i sortablejs --save-dev

HTML code

  <template>
  <el-transfer ref="transfer" id="transfer" v-model="value" :data="data">
   <span slot-scope="{ option }" :draggable="!option.disabled" @dragstart="drag($event,option)">{{ option.key }} - {{ option.label }}</span>
  </el-transfer>
</template>```

create

  <script>

   import Sortable from 'sortablejs'

   export default {
      data() {
        const generateData = _ => {
          const data = []; for (let i = 1; i <= 15; i++) {
            data.push({
              key: i,
              label: `option ${i}`,
              disabled: i % 4 === 0 });
          } return data;
        }; return {
          data: generateData(),
          value: [1, 4],
          draggingKey : null }
      },
      mounted() {
        const transfer = this.$refs.transfer.$el
        const leftPanel = transfer.getElementsByClassName("el-transfer-panel")[0].getElementsByClassName("el-transfer-panel__body")[0];
        const rightPanel = transfer.getElementsByClassName("el-transfer-panel")[1].getElementsByClassName("el-transfer-panel__body")[0];
        const rightEl = rightPanel.getElementsByClassName("el-transfer-panel__list")[0]
        Sortable.create(rightEl,{
          onEnd: (evt) => {
            const {oldIndex,newIndex} = evt;
            const temp = this.value[oldIndex] 
            if (!temp || temp === 'undefined') {
              return
            }//Solve the problem of undefined when dragging the last item on the right from the right to the left this.$set(this.value,oldIndex,this.value[newIndex])  
            this.$set(this.value,newIndex,temp)
          }
        })
        const leftEl = leftPanel.getElementsByClassName("el-transfer-panel__list")[0]
        Sortable.create(leftEl,{
          onEnd: (evt) => {
            const {oldIndex,newIndex} = evt;
            const temp = this.data[oldIndex] 
            if (!temp || temp === 'undefined') {
              return
            } //Solve the problem of undefined when dragging the last item on the right from the right to the left this.$set(this.data,oldIndex,this.data[newIndex]) 
            this.$set(this.data,newIndex,temp)
          }
        })
        leftPanel.ondragover = (ev) => {
          ev.preventDefault()
        }
        leftPanel.ondrop = (ev) => {
          ev.preventDefault();
          const index = this.value.indexOf(this.draggingKey) if(index !== -1){ this.value.splice(index,1)
          }
        }
        rightPanel.ondragover = (ev) => {
          ev.preventDefault()
        }
        rightPanel.ondrop = (ev) => {
          ev.preventDefault(); if(this.value.indexOf(this.draggingKey) === -1){ this.value.push(this.draggingKey)
          }
        }
      },
      methods: {
        drag(ev,option) { this.draggingKey = option.key
        }
      }

    } 
</script>

This is the end of this article about adding drag-and-drop function to vue element el-transfer. For more related element el-transfer drag-and-drop 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+abp WeChat scan code login implementation code example
  • How to encapsulate the table component of Vue Element
  • Example code for implementing dynamic column filtering in vue+element table
  • Sharing tips on using vue element and nuxt
  • Implementation of vue+elementui universal pop-up window (add+edit)
  • Vue+element UI realizes tree table
  • Vue+elementUI dynamically adds form items and adds verification code details
  • Vue uses element-ui to implement form validation
  • Vue Element front-end application development integrates ABP framework front-end login

<<:  About uniApp editor WeChat sliding problem

>>:  React-native sample code to implement the shopping cart sliding deletion effect

Recommend

Detailed explanation of the processing of the three Docker Nginx Logs

Because colleagues in the company need Nginx log ...

Using iframe techniques to obtain visitor QQ implementation ideas and sample code

Today at work, a friend I added temporarily asked ...

The hottest trends in web design UI in 2013 The most popular UI designs

Time flies, and in just six days, 2013 will becom...

A brief analysis of how to use border and display attributes in CSS

Introduction to border properties border property...

Gearman + MySQL to achieve persistence operation example

This article uses the gearman+mysql method to imp...

A brief introduction to VUE uni-app core knowledge

Table of contents specification a. The page file ...

If I change a property randomly in Vue data, will the view be updated?

Interviewer: Have you read the source code of Vue...

js code that associates the button with the enter key

Copy code The code is as follows: <html> &l...

Detailed steps to install CentOS7 system on VMWare virtual machine

Pre-installation work: Make sure vmware workstati...

The past two years with user experience

<br />It has been no more than two years sin...

Add unlimited fonts to your website with Google Web Fonts

For a long time, website development was hampered...

MySql index improves query speed common methods code examples

Use indexes to speed up queries 1. Introduction I...

Simple Implementation of HTML to Create Personal Resume

Resume Code: XML/HTML CodeCopy content to clipboa...

HTML dl, dt, dd tags to create a table vs. Table creation table

Not only does it reduce the cost of website develo...