vue3+typeScript穿梭框的实现示例
编程学习 2021-07-04 14:07www.dzhlxh.cn编程入门
这篇文章主要介绍了vue3+typeScript穿梭框的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
前言
实现功能:模仿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穿梭框内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- ThinkPad 笔记本如何调节LCD屏幕亮度
- Dreamweaver制作网页打开特效教程
- Win10 Mobile 10586升级后无限重启怎么办 硬重启帮您
- Win8系统提示音频设备有问题有一个或多个音频服
- Xbox One版Win10首个预览版9月份发布
- 如何在textarea文本输入区内实现换行
- Win10 Build 9901系统更新 预览版新版本下载
- McAfee Framework存在远程格式串处理漏洞
- Win10家庭版今日(7月30)正式在中国官方商城开卖
- Win10 Mobile预览版更新完10536.1000后才收到10536.100
- Win10 RS2更新了什么-Win10 RS2最终版本号1704首曝
- Windows7如何查看回收站对应的文件夹有哪些方法
- Win10让Charms栏回归桌面的方法教程
- 取消Windows XP系统开机启动画面的小技巧
- win8系统怎么下载安装USB百兆网卡?
- XP系统下磁盘空间变少了怎么办?XP系统磁盘空间