Vue's detailed code for implementing the shuttle box function

Vue's detailed code for implementing the shuttle box function

Vue - implement the shuttle box function, the effect diagram is as follows:

CSS

.transfer{
    display: flex;
    justify-content: center;
    align-items: center;
}
.transfer>.list {
    width: 200px;
    height: 300px;
    border: 1px solid #000;
    list-style: none;
}
.content{
    font-size: 30px;
    margin: 0 20px;
}
.list>li{
    padding: 5px;
    box-sizing: border-box;
}

HTML

<div class="transfer" >
    <!-- Left frame -->
    <ul class="list left">
        <template v-for="(item, index) in info">
            <li :key="index">
                <input type="checkbox" :id=`checkbox${item.id}` name="checkbox" :checked="item.select" @click="item.select=!item.select" />
                <label :for=`checkbox${item.id}` >{{ item.name }} </label>
            </li>
        </template>
    </ul>

    <!-- Add/Remove -->
    <div class="content">
        <p class="push" @click='push' >>>></p>
        <p class="del" @click='del' ><<<</p>
    </div>

    <!-- Right frame -->
    <ul class="list right">
        <template v-for="(item, index) in new_info">
            <li :key="index" >
                <input type="checkbox" :id=`newcheckbox${item.id}` name="newcheckbox" :checked="item.select" @click="item.select=!item.select" />
                <label :for=`newcheckbox${item.id}`>{{ item.name }} </label>
            </li>
        </template>
    </ul>
</div>

js

data(){
    return {
        // Original data, left frame data info:[
            {id:'1',name:'Xiao Ming'},
            {id:'2',name:'Xiaohong'},
            {id:'3',name:'Chicken'},
            {id:'4',name:'Hahahaha'},
            {id:'5',name:'Ahhhhh'},
            {id:'6',name:'dddd'},
            {id:'7',name:'qwert'},
        ],
        new_info: [], // New data, right frame data}
},
methods:{// add data push(){
        let that = this;
        let info = JSON.parse(JSON.stringify(that.info)); // Copy the original data, deep copy info.forEach((item, index )=>{
            // Execute data where select is true if (item.select){
                that.new_info = that.new_info.concat(item).sort((a,b)=>{ return a.id - b.id }); // Add to new data frame, sort delete info[index]; // Delete data item.select = false; 
            }
        })
        info = info.filter(function (val) { return val }); // Filter undefined 
        that.info = info; // Update original data\
    },
    // Remove data del(){
        let that = this;
        let info = JSON.parse(JSON.stringify(that.new_info)); // Copy the original data, deep copy info.forEach((item, index )=>{
            // Execute data where select is true if (item.select){
                that.info = that.info.concat(item).sort((a,b)=>{ return a.id - b.id }); // Add to new data frame, sort delete info[index]; // Delete data item.select = false;
            }
        })
        info = info.filter(function (val) { return val }); // Filter undefined 
        that.new_info = info; // Update original data},
},

mounted(){
    let that = this;
    // Add a select field to the original data to determine whether it is selected that.info.map((val,key)=>{ that.$set(val,'select',false) });
}

************************************************************************************************************************************************************

There will be problems with using splice to delete data here this.info.splice(index,1); When multiple elements are selected, you will find that only some of the elements are deleted, and some of the selected elements still exist because when an element is deleted, the index of the array changes, causing the program to abnormal. So delete is used to clear the data, and then filter undefined. The general idea is: add a select field to the data, bind it with the checked field of the multiple-choice box, and when clicked, the field is inverted. When transferring data, only the data with select as true is executed, added to the new data frame, and then the original one is deleted.

This is the end of this article about the detailed code of Vue to implement the shuttle box function. For more related Vue shuttle box 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 shuttle box realizes up and down movement
  • Implementation example of vue3+typeScript shuttle box
  • Detailed explanation of how to use the shuttle frame in VUE Element-ui
  • Vue realizes the shuttle box effect
  • Vue introduces element Transfer shuttle frame on demand

<<:  A brief discussion on the CSS overflow mechanism

>>:  Detailed Analysis of the Differences between break and last in Nginx

Recommend

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...

Detailed analysis of MySQL 8.0 memory consumption

Table of contents 1. innodb_buffer_pool_size 2. i...

Five delay methods for MySQL time blind injection

Five delay methods for MySQL time blind injection...

How to import Excel files into MySQL database

This article shares with you how to import Excel ...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

Problems with installing mysql and mysql.sock under linux

Recently, I encountered many problems when instal...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

The implementation process of extracting oracle data to mysql database

In the migration of Oracle database to MySQL data...

Mount the disk in a directory under Ubuntu 18.04

Introduction This article records how to mount a ...

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

Some suggestions for improving Nginx performance

If your web application runs on only one machine,...

Detailed process of Vue front-end packaging

Table of contents 1. Add packaging command 2. Run...

Three solutions for sub-functions accessing external variables in JavaScript

Preface When we write web pages, we will definite...