Vue 實(shí)現(xiàn)穿梭框功能的詳細(xì)代碼
Vue - 實(shí)現(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 }); // 過(guò)濾 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 }); // 過(guò)濾 undefined
that.new_info = info; // 更新原數(shù)據(jù)
},
},
mounted(){
let that = this;
// 給原始數(shù)據(jù)添加一個(gè) select 字段,判斷是否選中
that.info.map((val,key)=>{ that.$set(val,'select',false) });
}
********************************************************************************************************************************************************
這里使用splice刪除數(shù)據(jù)會(huì)有問(wèn)題 this.info.splice(index,1);當(dāng)選中多個(gè)元素時(shí),會(huì)發(fā)現(xiàn)只刪除掉其中一些元素,而還有一些選中的元素還存在因?yàn)楫?dāng)刪除掉了一個(gè)元素后,數(shù)組的索引發(fā)生的變化,造成了程序的異常。所以就使用了 delete清除數(shù)據(jù),然后再 filter過(guò)濾 undefined大概思路: 給數(shù)據(jù)添加一個(gè) select 字段,用多選框的 checked 綁定, click 的時(shí)候該字段實(shí)現(xiàn)取反轉(zhuǎn)移數(shù)據(jù)時(shí),只執(zhí)行 select 為 true 的數(shù)據(jù),添加到新數(shù)據(jù)框中,再把原先的刪除
到此這篇關(guān)于Vue 實(shí)現(xiàn)穿梭框功能的詳細(xì)代碼的文章就介紹到這了,更多相關(guān)Vue 穿梭框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Webstorm 新建.vue文件支持高亮vue語(yǔ)法和es6語(yǔ)法
這篇文章主要介紹了Webstorm 添加新建.vue文件功能并支持高亮vue語(yǔ)法和es6語(yǔ)法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
vue實(shí)現(xiàn)用戶(hù)動(dòng)態(tài)權(quán)限登錄的代碼示例
這篇文章主要介紹了vue如何實(shí)現(xiàn)用戶(hù)動(dòng)態(tài)權(quán)限登錄,文中的代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)vue有一定的幫助,需要的朋友可以參考閱讀2023-05-05
vue3不同語(yǔ)法格式對(duì)比的實(shí)例代碼
vue3發(fā)布已有一段時(shí)間了,文檔也大概看了一下,不過(guò)對(duì)于學(xué)一門(mén)技術(shù),最好的方法還是實(shí)戰(zhàn),這篇文章主要給大家介紹了關(guān)于vue3不同語(yǔ)法格式對(duì)比的相關(guān)資料,需要的朋友可以參考下2021-08-08
關(guān)于vue3默認(rèn)把所有onSomething當(dāng)作v-on事件綁定的思考
這篇文章主要介紹了關(guān)于vue3默認(rèn)把所有`onSomething`當(dāng)作`v-on`事件綁定的思考,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
vue與django(drf)實(shí)現(xiàn)文件上傳下載功能全過(guò)程
最近簡(jiǎn)單的學(xué)習(xí)了django和drf上傳文件(主要是圖片),做一個(gè)記錄,下面這篇文章主要給大家介紹了關(guān)于vue與django(drf)實(shí)現(xiàn)文件上傳下載功能的相關(guān)資料,需要的朋友可以參考下2023-02-02
Vue基礎(chǔ)之MVVM,模板語(yǔ)法和數(shù)據(jù)綁定
這篇文章主要為大家介紹了Vue之MVVM,模板語(yǔ)法和數(shù)據(jù)綁定,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
vue3中reactive和ref的實(shí)現(xiàn)與區(qū)別詳解
reactive和ref都是vue3實(shí)現(xiàn)響應(yīng)式系統(tǒng)的api,他們是如何實(shí)現(xiàn)響應(yīng)式的呢,reactive和ref又有什么區(qū)別呢,下面小編就來(lái)和大家詳細(xì)講講,希望對(duì)大家有所幫助2023-10-10
vue導(dǎo)出excel和echart圖形分別在不同工作表的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了如何使用vue實(shí)現(xiàn)導(dǎo)出excel和echart圖形分別在不同工作表,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能示例
這篇文章主要給大家介紹了關(guān)于vue動(dòng)態(tài)合并單元格并添加小計(jì)合計(jì)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

