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
Table of contents Use two-way binding data in v-m...
Table of contents 1. innodb_buffer_pool_size 2. i...
This article uses examples to describe the basic ...
Five delay methods for MySQL time blind injection...
This article shares with you how to import Excel ...
1. Set up HOST on the host Macbook The previous d...
Recently, I encountered many problems when instal...
HTML-centric front-end development is almost what ...
In the migration of Oracle database to MySQL data...
Introduction This article records how to mount a ...
Table of contents 1. What is DOM 2. Select elemen...
If your web application runs on only one machine,...
1. Unzip the file to the current directory Comman...
Table of contents 1. Add packaging command 2. Run...
Preface When we write web pages, we will definite...