基于Vue框架怎么写一个简单的穿梭框功能
Admin 2022-06-29 群英技术资讯 858 次浏览
Vue - 实现穿梭框功能,效果图如下所示:
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{ // 原数据,左框数据 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: [],// 新数据,右框数据 } }, methods:{// 添加数据 push(){ let that = this; let info = JSON.parse(JSON.stringify(that.info)); // 拷贝原数据, 深拷贝 info.forEach((item, index )=>{ // 执行 select 为true 的数据 if (item.select){ that.new_info = that.new_info.concat(item).sort((a,b)=>{ return a.id - b.id }); // 添加到新数据框, 排序 delete info[index]; // 删除数据 item.select = false; } }) info = info.filter(function (val) { return val }); // 过滤 undefined that.info = info; // 更新原数据\ }, // 移除数据 del(){ let that = this; let info = JSON.parse(JSON.stringify(that.new_info)); // 拷贝原数据, 深拷贝 info.forEach((item, index )=>{ // 执行 select 为true 的数据 if (item.select){ that.info = that.info.concat(item).sort((a,b)=>{ return a.id - b.id }); // 添加到新数据框, 排序 delete info[index]; // 删除数据 item.select = false; } }) info = info.filter(function (val) { return val }); // 过滤 undefined that.new_info = info; // 更新原数据 }, }, mounted(){ let that = this; // 给原始数据添加一个 select 字段,判断是否选中 that.info.map((val,key)=>{ that.$set(val,'select',false) }); }
********************************************************************************************************************************************************
这里使用splice删除数据会有问题 this.info.splice(index,1);当选中多个元素时,会发现只删除掉其中一些元素,而还有一些选中的元素还存在因为当删除掉了一个元素后,数组的索引发生的变化,造成了程序的异常。所以就使用了 delete清除数据,然后再 filter过滤 undefined大概思路: 给数据添加一个 select 字段,用多选框的 checked 绑定, click 的时候该字段实现取反转移数据时,只执行 select 为 true 的数据,添加到新数据框中,再把原先的删除
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了vue实现简易的计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文给大家介绍vue插槽,对于vue插槽我们之前也有了解过,本文就给大家来介绍一下vue插槽的好处和vue插槽的使用,感兴趣的朋友可以了解看看,那么接下来就跟随小编来学习一下吧。
在JavaScript中,可以利用reverse()方法来实现元素倒序排列,reverse()方法的作用就是颠倒数组中元素的顺序,语法为“需要进行倒序排列的数组元素.reverse()”。
本文可以让你了解所有有关JavaScript箭头函数的信息。我们将告诉你如何使用ES6的箭头语法,以及在代码中使用箭头函数时需要注意的一些常见错误。你会看到很多例子来说明它们是如何工作的。
目录vue判断安卓还是IOS最近工作上遇到这样一个需求所以我们需要进行一个判断H5端判断安卓跟ios显示不同的背景图vue判断安卓还是IOS最近工作上遇到这样一个需求vue写的页面,需要同时跟安卓和ios进行交互;若是安卓,执行代码:android.finishActivity();若是IOS,执行代码:try {
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008