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

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

How to configure jdk environment under Linux

1. Go to the official website to download the jdk...

The difference between useEffect and useLayoutEffect in React

Table of contents Prerequisites useEffect commitB...

VMware ESXI server virtualization cluster

Table of contents summary Environment and tool pr...

JavaScript static scope and dynamic scope explained with examples

Table of contents Preface Static scope vs. dynami...

How to fix the four sides of the table to scroll up, down, left and right

question: When I was doing project statistics rec...

MySQL scheduled full database backup

Table of contents 1. MySQL data backup 1.1, mysql...

In-depth understanding of the use of the infer keyword in typescript

Table of contents infer Case: Deepen your underst...

Usage of MySQL time difference functions TIMESTAMPDIFF and DATEDIFF

Usage of time difference functions TIMESTAMPDIFF ...

Native JS to achieve directory scrolling effects

Here is a text scrolling effect implemented with ...

Three ways to delete a table in MySQL (summary)

drop table Drop directly deletes table informatio...

Detailed explanation of MLSQL compile-time permission control example

Preface The simple understanding of MySQL permiss...

HTML+CSS3 code to realize the animation effect of the solar system planets

Make an animation of the eight planets in the sol...