Vue 實現(xiàn)穿梭框功能的詳細代碼
Vue - 實現(xiàn)穿梭框功能,效果圖如下所示:

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" >
<!-- 左框 -->
<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>
<!-- 添加/刪除 -->
<div class="content">
<p class="push" @click='push' >>>></p>
<p class="del" @click='del' ><<<</p>
</div>
<!-- 右框 -->
<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{
// 原數(shù)據(jù),左框數(shù)據(jù)
info:[
{id:'1',name:'小明'},
{id:'2',name:'小紅'},
{id:'3',name:'小雞'},
{id:'4',name:'哈哈哈哈'},
{id:'5',name:'啊啊啊啊'},
{id:'6',name:'dddd'},
{id:'7',name:'qwert'},
],
new_info: [],// 新數(shù)據(jù),右框數(shù)據(jù)
}
},
methods:{// 添加數(shù)據(jù)
push(){
let that = this;
let info = JSON.parse(JSON.stringify(that.info)); // 拷貝原數(shù)據(jù), 深拷貝
info.forEach((item, index )=>{
// 執(zhí)行 select 為true 的數(shù)據(jù)
if (item.select){
that.new_info = that.new_info.concat(item).sort((a,b)=>{ return a.id - b.id }); // 添加到新數(shù)據(jù)框, 排序
delete info[index]; // 刪除數(shù)據(jù)
item.select = false;
}
})
info = info.filter(function (val) { return val }); // 過濾 undefined
that.info = info; // 更新原數(shù)據(jù)\
},
// 移除數(shù)據(jù)
del(){
let that = this;
let info = JSON.parse(JSON.stringify(that.new_info)); // 拷貝原數(shù)據(jù), 深拷貝
info.forEach((item, index )=>{
// 執(zhí)行 select 為true 的數(shù)據(jù)
if (item.select){
that.info = that.info.concat(item).sort((a,b)=>{ return a.id - b.id }); // 添加到新數(shù)據(jù)框, 排序
delete info[index]; // 刪除數(shù)據(jù)
item.select = false;
}
})
info = info.filter(function (val) { return val }); // 過濾 undefined
that.new_info = info; // 更新原數(shù)據(jù)
},
},
mounted(){
let that = this;
// 給原始數(shù)據(jù)添加一個 select 字段,判斷是否選中
that.info.map((val,key)=>{ that.$set(val,'select',false) });
}
********************************************************************************************************************************************************
這里使用splice刪除數(shù)據(jù)會有問題 this.info.splice(index,1);當(dāng)選中多個元素時,會發(fā)現(xiàn)只刪除掉其中一些元素,而還有一些選中的元素還存在因為當(dāng)刪除掉了一個元素后,數(shù)組的索引發(fā)生的變化,造成了程序的異常。所以就使用了 delete清除數(shù)據(jù),然后再 filter過濾 undefined大概思路: 給數(shù)據(jù)添加一個 select 字段,用多選框的 checked 綁定, click 的時候該字段實現(xiàn)取反轉(zhuǎn)移數(shù)據(jù)時,只執(zhí)行 select 為 true 的數(shù)據(jù),添加到新數(shù)據(jù)框中,再把原先的刪除
到此這篇關(guān)于Vue 實現(xiàn)穿梭框功能的詳細代碼的文章就介紹到這了,更多相關(guān)Vue 穿梭框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.0?router路由跳轉(zhuǎn)傳參問題(router.push)
這篇文章主要介紹了vue3.0?router路由跳轉(zhuǎn)傳參問題(router.push),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
關(guān)于vue-router的使用及實現(xiàn)原理
這篇文章主要介紹了關(guān)于vue-router的使用及實現(xiàn)原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue3新特性之在Composition API中使用CSS Modules
這篇文章主要介紹了Vue3新特性之在Composition API中使用CSS Modules,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Vue組件間的通信pubsub-js實現(xiàn)步驟解析
這篇文章主要介紹了Vue組件間的通信pubsub-js實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

