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:
|
<<: A brief discussion on the CSS overflow mechanism
>>: Detailed Analysis of the Differences between break and last in Nginx
Physical Structure of an InnoDB Index All InnoDB ...
1. Function : Allows the parent component to inse...
1. Go to the official website to download the jdk...
Table of contents Prerequisites useEffect commitB...
Table of contents summary Environment and tool pr...
Table of contents Preface Static scope vs. dynami...
question: When I was doing project statistics rec...
Table of contents 1. MySQL data backup 1.1, mysql...
Table of contents infer Case: Deepen your underst...
Usage of time difference functions TIMESTAMPDIFF ...
Here is a text scrolling effect implemented with ...
drop table Drop directly deletes table informatio...
Preface The simple understanding of MySQL permiss...
Nginx, pronounced "engine x," is an ope...
Make an animation of the eight planets in the sol...