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)文章
vue3.0?router路由跳轉(zhuǎn)傳參問(wèn)題(router.push)
這篇文章主要介紹了vue3.0?router路由跳轉(zhuǎn)傳參問(wèn)題(router.push),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02關(guān)于vue-router的使用及實(shí)現(xiàn)原理
這篇文章主要介紹了關(guān)于vue-router的使用及實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Vue3新特性之在Composition API中使用CSS Modules
這篇文章主要介紹了Vue3新特性之在Composition API中使用CSS Modules,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue實(shí)現(xiàn)簡(jiǎn)單無(wú)縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單無(wú)縫滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04使用vue-router完成簡(jiǎn)單導(dǎo)航功能【推薦】
vue-router是Vue.js官方提供的一套專(zhuān)用的路由工具庫(kù)。這篇文章主要介紹了使用vue-router完成簡(jiǎn)單導(dǎo)航功能,需要的朋友可以參考下2018-06-06Vue組件間的通信pubsub-js實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Vue組件間的通信pubsub-js實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03