实现功能:模仿element穿梭框的简单功能
每周分享一个vue3+typeScript的小组件,我只想分享下自己的实现思路,楼主是个菜鸡前端,记录下实现过程,说不定对你有帮助。
思路:用两个数组分别记录左右框框里面的值,根据复选框选中状态来实现删除增加即可
html部分
<div class="shuttle"> <!-- 左边列表 --> <div class="shuttle-box"> <div class="shuttle-box-title"> <div>列表一</div> <div class="index-num">{{itemLeft.length}}</div> </div> <div class="shuttle-box-list"> <div class="shuttle-box-item" v-for="(vo,inx) in itemLeft" :key="inx"> <input type="checkbox" :value="inx" v-model="checkLeft" :disabled="vo.disabled" /> {{vo.label}} </div> </div> </div> <!-- 左右操作按钮 --> <div class="shuttle-click"> <span @click="goLeft">←</span> <span @click="goRight">→</span> </div> <!-- 右边列表 --> <div class="shuttle-box"> <div class="shuttle-box-title"> <div>列表二</div> <div class="index-num">{{itemRight.length}}</div> </div> <div class="shuttle-box-list"> <div class="shuttle-box-item" v-for="(vo,inx) in itemRight" :key="inx"> <input type="checkbox" :value="inx" v-model="checkRight" :disabled="vo.disabled" /> {{vo.label}} </div> </div> </div> </div>
ts部分
<script lang="ts"> import { defineComponent, reactive, toRefs } from 'vue' export default defineComponent({ setup() { const data = reactive({ itemLeft: [{ label: '列表1的第一条数据', disabled: true, }, { label: '列表1的第二条数据', disabled: false, }], itemRight: [{ label: '列表2的第一条数据', disabled: false, }, { label: '列表2的第二条数据', disabled: false, }], checkLeft: [], checkRight: [], goLeft: () => { //数组排序 data.checkRight.sort(data.sortIndex); data.checkRight.forEach((item) => { //将itemRight对应索引的数据移动到左边去 data.itemLeft.push(data.itemRight[item]); //移除 data.itemRight.splice(item, 1); }); //清空 data.checkLeft = []; data.checkRight = []; }, goRight: () => { //数组排序 data.checkLeft.sort(data.sortIndex); data.checkLeft.forEach((item) => { //将itemLeft对应索引的数据移动到右边去 data.itemRight.push(data.itemLeft[item]); //移除 data.itemLeft.splice(item, 1); }); //清空 data.checkLeft = []; data.checkRight = []; }, //checkbox是绑定的是的数组的索引,所以checkbox的点击的顺序不同的话索引的顺序是不同的,这样删除有可能找不到会报错,排个序从大到小删除就可以 //这个是排序参数 sortIndex: (a, b) => { return b - a; } }) return { ...toRefs(data), } } }) </script>
css部分
.shuttle { width: 800px; padding: 50px 0; display: flex; justify-content: space-between; //整个穿梭框 .shuttle-box { width: 300px; height: 500px; border: 1px solid #ddd; //标题 .shuttle-box-title { background: #f5f7fa; padding: 0 20px; height: 40px; line-height: 40px; display: flex; justify-content: space-between; .index-num { color: #909399; font-size: 12px; font-weight: 400; } } //列表 .shuttle-box-list { padding: 20px; //一个列表item .shuttle-box-item { line-height: 2.0; } } } //左右穿梭按钮 .shuttle-click { padding-top: 60px; cursor: pointer; span { padding: 5px 10px; display: inline-block; background: #409eff; color: #ffffff; margin: 0 5px; text-align: center; } } }
到此这篇关于vue3+typeScript穿梭框的实现示例的文章就介绍到这了,更多相关vue3+typeScript穿梭框内容请搜索站长技术以前的文章或继续浏览下面的相关文章希望大家以后多多支持站长技术!
2月26日消息 众所周知,Windows 10 的安全更新和其他重要累计更新通常是在同一天...
继 Australis 和 Photon 之后,Mozilla 现又酝酿为 Firefox 带来名为Proton的全...
前言 aop即是面向切面编程,众多Aop框架里Castle是最为人所知的,另外还有死去的...
console.log ,作为一个前端开发者,可能每天都会用它来分析调试,但这个简单函...
不少Windows 10用户之前都抱怨一个问题,那就是系统的屏幕出现了渲染问题,而微...
一、Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候...
互联网业务往往使用MySQL数据库作为后台存储,存储引擎使用InnoDB。我们针对互联...
开发过程中,我们经常会遇到代码回滚的情况。正常人都知道,git 回滚有两大宝: ...
本文转载自微信公众号「Linux开发那些事儿」,作者 LinuxThings 。转载本文请联...
简介 “ 大家好我是帅哥欢迎来到帅哥的程序人生我会把经历分享出来助你了解圈内...