基于Vue框架怎么写一个简单的穿梭框功能
Admin 2022-06-29 群英技术资讯 1050 次浏览
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
JS怎样实现在数组特定索引插入元素?其实我们开发程序对很多功能都有接触的,而使用数组实现插入元素其实并没有那么难
yield关键字只能用于生成器函数,它表示暂停函数内部代码的执行,并返回当前迭代数据。如果没有下一个yield,next返回对象的done将被放置为true。
本文实例为大家分享了cropperjs实现裁剪图片功能的具体代码,供大家参考,具体内容如下
这篇文章主要分享vue指令的内容,很多新手可能对vue指令不太了解,下文就给大家介绍一些常用vue指令、自定义指令、全局指令、局部指令等等,感兴趣的朋友可以参考下,希望大家阅读完这篇文章能有所收获
1、利用a标签的href属性添加文件URL,语法“下载”。2、使用url跳转下载,语法“window.open(文件url)”。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008