自定义多级联动选择器指南(uni-app)

  • 多端支持:可以运行在H5、APP、微信小程序还是支付宝小程序,都可以轻松使用改组件。
  • 自定义配置:您可以根据需要配置选择器的级数,使其适应不同的数据结构和用例。
  • 无限级联:此组件支持无限级联选择,使您能够创建具有复杂数据结构的选择器。
参数类型描述默认值必选
titlestring标题‘’
layernumber控制几级联动1
dataarray数据:[{name: ‘’, id: ‘’, children: [{name: ‘’, id: ‘’}]}][]

在这里插入图片描述

接口返回数据结构:

[{id: 47, name: "工厂1", parentId: 0, type: 0},{id: 48, name: "区域1", parentId: 47, type: 0},{id: 19, name: "设备1", parentId: 48, type: 1}
]

处理后数据结构:

[{ id: 47, name: "工厂1", parentId: 0, type: 0,children: [{ id: 48, name: "区域1", parentId: 47, type: 0,children: [{ id: 19, name: "设备1", parentId: 48, type: 1 }] }] 
}]

引用示例:

<template><view class="container"><view @click="bindDevice">选择设备</view><cascade-picker ref="picker" :title="cascadePicker.title" :layer="cascadePicker.layer" :data="cascadePicker.data" @callback="pickerCallback"></cascade-picker></view>
</template>
<script>
import cascadePicker from '@/components/cascade-picker/cascade-picker.vue';
import {handleTree} from "@/utils/tree";
export default {components: {cascadePicker},data() {return {deviceArr: [],deviceId: '',cascadePicker: {title: 'picker标题',layer: null,data: []}}},onLoad() {this.getDeviceSimpleList()},methods: {// 获取设备列表getDeviceSimpleList() {getDeviceSimpleList().then(res => {this.deviceArr = handleTree(res.data)})},// 打开设备选择器bindDevice() {const _this = this;_this.cascadePicker.data = _this.deviceArr;_this.$refs.picker.open().then(function() {console.log('打开成功');});},// picker多级联动回调pickerCallback(e) {const {data} = e;const lastItem = data[data.length - 1];this.deviceId = lastItem.id;}}
}
</script>

@/utils/tree文件中的handleTree方法

/*** 构造树型结构数据* @param {*} data 数据源* @param {*} id id字段 默认 'id'* @param {*} parentId 父节点字段 默认 'parentId'* @param {*} children 孩子节点字段 默认 'children'* @param {*} rootId 根Id 默认 0*/
export function handleTree(data, id = 'id', parentId = 'parentId', children = 'children', rootId = 0) {rootId = rootId || Math.min.apply(Math, data.map(item => {return item[parentId]})) || 0//对源数据深度克隆const cloneData = JSON.parse(JSON.stringify(data))//循环所有项const treeData = cloneData.filter(father => {let branchArr = cloneData.filter(child => {//返回每一项的子级数组return father[id] === child[parentId]});branchArr.length > 0 ? father.children = branchArr : '';//返回第一层return father[parentId] === rootId;});return treeData !== '' ? treeData : data;
}

cascade-picker组件完整代码:

components文件夹下创建cascade-picker文件夹,然后在此文件夹下创建cascade-picker.vue - 多级联动组件

<template name="cascade-picker"><view class="aui-picker" v-if="SHOW" :class="{'aui-picker-in': FADE==1,'aui-picker-out': FADE==0}"><view class="aui-mask" @click.stop="close"></view><view class="aui-picker-main"><view class="aui-picker-header"><view class="aui-picker-header-icon" @click.stop="close">取消</view><view class="aui-picker-title" v-if="title">{{ title }}</view><view class="aui-picker-header-icon aui-picker-confirm" @click.stop="_confirm">确认</view></view><view class="aui-picker-nav"><view class="aui-picker-navitem"v-if="nav.length>0"v-for="(item, index) in nav":key="index":data-index="index":class="[index==navCurrentIndex ? 'active' : '', 'aui-picker-navitem-'+index]":style="{margin: nav.length>2 ? '0 10px 0 0' : '0 30px 0 0'}"@click.stop="_changeNav($event)">{{ item.name }}</view><view class="aui-picker-navitem":key="nav.length":data-index="nav.length":class="[nav.length==navCurrentIndex ? 'active' : '', 'aui-picker-navitem-'+nav.length]":style="{margin: nav.length>2 ? '0 10px 0 0' : '0 30px 0 0'}"@click.stop="_changeNav($event)">请选择</view><view class="aui-picker-navborder" :style="{left: navBorderLeft+'px'}"></view></view><view class="aui-picker-content"><view class="aui-picker-lists"><view class="aui-picker-list"v-for="(list, index) in queryItems.length + 1":key="index":data-index="index":class="[index==navCurrentIndex ? 'active' : '']"><view class="aui-picker-list-warp" v-if="index == 0"><view class="aui-picker-item"v-for="(item, key) in items"v-if="item.pid=='0'":key="key":data-pindex="index":data-index="key":data-id="item.id":data-pid="item.pid":data-name="item.name":data-type="item.type":class="{'active': result.length>index && result[index].id==item.id}":style="{'background': touchConfig.index==key && touchConfig.pindex==index ? touchConfig.style.background : ''}"@click.stop="_chooseItem($event)"@touchstart="_btnTouchStart($event)"@touchmove="_btnTouchEnd($event)"@touchend="_btnTouchEnd($event)">{{ item.name }}</view></view><view class="aui-picker-list-warp" v-else><view class="aui-picker-item"v-for="(item, key) in queryItems[index-1]":key="key":data-pindex="index":data-index="key":data-id="item.id":data-pid="item.pid":data-name="item.name":data-type="item.type":class="{'active': result.length>index && result[index].id==item.id}":style="{'background': touchConfig.index==key && touchConfig.pindex==index ? touchConfig.style.background : ''}"@click.stop="_chooseItem($event)"@touchstart="_btnTouchStart($event)"@touchmove="_btnTouchEnd($event)"@touchend="_btnTouchEnd($event)">{{ item.name }}</view></view></view></view></view></view></view>
</template><script>
export default {name: 'cascade-picker',props: {title: { //标题type: String,default: ''},layer: { //控制几级联动,默认无限级(跟随数据有无下级)type: Number,default: null},data: { //数据 如:[{id: '', name: '', children: [{id: '', name: ''}]}]type: Array,default() {return [// [{id: '', name: '', children: [{id: '', name: ''}]}]]}}},data() {return {SHOW: false,FADE: -1,nav: [],items: [], // 第一级数据列表queryItems: [], // 各级数据列表navCurrentIndex: 0, // 当前选中的导航项索引navBorderLeft: 35, // 导航栏的边框左侧位置result: [],touchConfig: {index: -1,pindex: -1,style: {color: '#214579',background: '#EFEFEF'}},selectedData: [] // 用于回显数据的属性}},watch: {data() {const _this = this;const data = _this.data;_this.items = _this._flatten(data, '0')}},methods: {// 打开open() {const _this = this;_this.reset(); //打开时重置pickerreturn new Promise(function (resolve, reject) {_this.SHOW = true;_this.FADE = 1;resolve();});},// 关闭close() {const _this = this;return new Promise(function (resolve, reject) {_this.FADE = 0;const _hidetimer = setTimeout(() => {_this.SHOW = false;_this.FADE = -1;clearTimeout(_hidetimer);resolve();}, 100)});},//重置reset() {const _this = this;_this.queryItems = [];_this.nav = [];_this.navBorderLeft = 35;_this.navCurrentIndex = 0;_this.result = [];},//导航栏切换_changeNav(e) {const _this = this;const index = Number(e.currentTarget.dataset.index);_this.navCurrentIndex = index;const _el = uni.createSelectorQuery().in(this).select(".aui-picker-navitem-" + index);_el.boundingClientRect(data => {_this.navBorderLeft = data.left + 20;}).exec();},//数据选择_chooseItem(e) {const _this = this;const id = e.currentTarget.dataset.id;const name = e.currentTarget.dataset.name;const pid = e.currentTarget.dataset.pid;const type = e.currentTarget.dataset.type;const _arr = [];_this.result[_this.navCurrentIndex] = {id: id, name: name, pid: pid, type: type};if ((!_this._isDefine(_this.layer) && _this._isDefine(_this._deepQuery(_this.data, id).children))||(_this.navCurrentIndex < (Number(_this.layer) - 1) && _this._isDefine(_this._deepQuery(_this.data, id).children))) { //有下级数据_this._deepQuery(_this.data, id).children.forEach(function (item, index) {_arr.push({id: item.id, name: item.name, pid: id, type: item.type});});if (_this.navCurrentIndex == _this.queryItems.length) { //选择数据_this.queryItems.push(_arr);_this.nav.push({name: name});} else { //重新选择数据_this.queryItems.splice(_this.navCurrentIndex + 1, 1);_this.nav.splice(_this.navCurrentIndex + 1, 1);_this.queryItems.splice(_this.navCurrentIndex, 1, _arr);_this.nav.splice(_this.navCurrentIndex, 1, {name: name});}_this.navCurrentIndex = _this.navCurrentIndex + 1;const _el = uni.createSelectorQuery().in(this).select(".aui-picker-navitem-" + _this.navCurrentIndex);setTimeout(() => {_el.boundingClientRect(data => {_this.navBorderLeft = data.left + 20;}).exec();}, 100)} else { //无下级数据且最后一级数据的type为1时,则可以确认关闭_this._confirm();}},_confirm() {const _this = this;const lastItem = _this.result[_this.result.length - 1];if (lastItem && lastItem.type === 1) {_this.close().then(() => {_this.$emit("callback", {status: 0, data: _this.result});});} else {uni.$u.toast('请选择设备')}},//递归遍历——将树形结构数据转化为数组格式_flatten(tree, pid) {return tree.reduce((arr, {id, name, type, children = []}) =>arr.concat([{id, name, pid, type}], this._flatten(children, id)), [])},//根据id查询对应的数据(如查询id=10100对应的对象)_deepQuery(tree, id) {let isGet = false;let retNode = null;function deepSearch(tree, id) {for (let i = 0; i < tree.length; i++) {if (tree[i].children && tree[i].children.length > 0) {deepSearch(tree[i].children, id);}if (id === tree[i].id || isGet) {isGet || (retNode = tree[i]);isGet = true;break;}}}deepSearch(tree, id);return retNode;},/***判断字符串是否为空@param {string} str 变量@example: aui.isDefine("变量");*/_isDefine(str) {if (str == null || str == "" || str == "undefined" || str == undefined || str == "null" || str == "(null)" || str == 'NULL' || typeof (str) == 'undefined') {return false;} else {str = str + "";str = str.replace(/\s/g, "");if (str == "") {return false;}return true;}},_btnTouchStart(e) {const _this = this,index = Number(e.currentTarget.dataset.index),pindex = Number(e.currentTarget.dataset.pindex);_this.touchConfig.index = index;_this.touchConfig.pindex = pindex;},_btnTouchEnd(e) {const _this = this,index = Number(e.currentTarget.dataset.index),pindex = Number(e.currentTarget.dataset.pindex);_this.touchConfig.index = -1;_this.touchConfig.pindex = -1;},}
}
</script><style lang="scss" scoped>
/* ====================多级联动弹窗=====================*/
.aui-picker {width: 100vw;height: 100vh;//opacity: 0;position: fixed;top: 0;left: 0;z-index: 999;/* display: none; */
}// 遮罩层
.aui-mask {width: 100%;height: 100%;background: rgba(0, 0, 0, .3);position: absolute;left: 0;top: 0;z-index: 999;
}.aui-picker.aui-picker-in {-moz-animation: aui-fade-in .1s ease-out forwards;-ms-animation: aui-fade-in .1s ease-out forwards;-webkit-animation: aui-fade-in .1s ease-out forwards;animation: aui-fade-in .1s ease-out forwards;
}.aui-picker.aui-picker-out {-moz-animation: aui-fade-out .1s ease-out forwards;-ms-animation: aui-fade-out .1s ease-out forwards;-webkit-animation: aui-fade-out .1s ease-out forwards;animation: aui-fade-out .1s ease-out forwards;
}.aui-picker-main {width: 100vw;height: 50vh;background: #FFF;//border-radius: 15px 15px 0 0;position: absolute;left: 0px;bottom: 0;z-index: 999;
}.aui-picker.aui-picker-in .aui-picker-main {-moz-animation: aui-slide-up-screen .2s ease-out forwards;-ms-animation: aui-slide-up-screen .2s ease-out forwards;-webkit-animation: aui-slide-up-screen .2s ease-out forwards;animation: aui-slide-up-screen .2s ease-out forwards;
}.aui-picker.aui-picker-out .aui-picker-main {-moz-animation: aui-slide-down-screen .2s ease-out forwards;-ms-animation: aui-slide-down-screen .2s ease-out forwards;-webkit-animation: aui-slide-down-screen .2s ease-out forwards;animation: aui-slide-down-screen .2s ease-out forwards;
}.aui-picker-header {width: 100%;min-height: 50px;position: relative;z-index: 999;display: flex;justify-content: space-between;align-items: center;&-icon {font-size: 15px;color: #aaa;padding: 0 15px;}.aui-picker-confirm {height: 50px;line-height: 50px;text-align: center;font-size: 15px;color: $custom-content-color;padding: 0 15px;}
}.aui-picker-header::after {content: '';width: 100%;height: 1px;background: rgba(100, 100, 100, .3);-moz-transform: scaleY(.3);-ms-transform: scaleY(.3);-webkit-transform: scaleY(.3);transform: scaleY(.3);position: absolute;left: 0;bottom: 0;z-index: 999;
}.aui-picker-title {line-height: 20px;text-align: center;font-size: 17px;color: #333;padding: 15px;box-sizing: border-box;position: absolute;left: 50px;right: 50px;top: 0;
}.aui-picker-content {width: 100%;height: -webkit-calc(100% - 100px);height: calc(100% - 100px);
}.aui-picker-nav {width: 100%;height: 50px;text-align: left;padding: 0 15px;margin: 0 0 1px 0;justify-content: flex-start;white-space: nowrap;box-sizing: border-box;position: relative;overflow-x: scroll;overflow-y: hidden;
}.aui-picker-nav::after {content: '';width: 100%;height: 1px;background: rgba(100, 100, 100, .3);-moz-transform: scaleY(.3);-ms-transform: scaleY(.3);-webkit-transform: scaleY(.3);transform: scaleY(.3);position: absolute;left: 0;bottom: 0;z-index: 999;
}.aui-picker-navitem {width: 80px;line-height: 50px;font-size: 16px;margin: 0 30px 0 0;text-align: center;display: inline-block;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;
}.aui-picker-navitem.active {color: $custom-content-color;
}.aui-picker-navborder {width: 40px;height: 3px;background: $custom-content-color;border-radius: 5px;transition: left .15s;position: absolute;left: 40px;bottom: 0;
}.aui-picker-lists {width: 100%;height: 100%;justify-content: space-around;white-space: nowrap;
}.aui-picker-list {width: 100%;height: 100%;overflow: hidden;overflow-y: scroll;display: none;vertical-align: top;
}.aui-picker-list.active {display: inline-block;
}.aui-picker-list-warp {width: 100%;height: auto;box-sizing: border-box;padding: 15px 0;display: inline-block;
}.aui-picker-item {width: 100%;height: 50px;line-height: 50px;padding: 0 15px;box-sizing: border-box;font-size: 15px;color: #333;position: relative;
}.aui-picker-item.active {color: $custom-content-color;
}.aui-picker-item.active::after {content: '✔';font-size: 15px;color: $custom-content-color;position: absolute;top: 0px;right: 10px;
}</style>
总结

自定义多级联动选择器组件将有助于您在uni-app中创建灵活的选择器,以满足各种不同平台和级数的需求。如果有任何问题、反馈或需要进一步的帮助,请不要犹豫,在下面的评论中提出。期待听到您的声音,以便改进和完善这个组件。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/455016.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

最好的ppt模板网站是哪个?做PPT不可错过的18个网站!

现在有很多PPT模板网站&#xff0c;但真正免费且高质量的不多&#xff0c;今天我就分享主流的国内外PPT模板下载网站&#xff0c;并且会详细分析这些网站的优缺点&#xff0c;这些网站都是基于个人实际使用经验的&#xff0c;免费站点会特别标注&#xff0c;让你可以放心下载&a…

信息安全工程师(64)其他恶意代码分析与防护

前言 恶意代码是指那些能够损害系统用户和系统所有者利益的软件&#xff0c;是故意在计算机系统上执行恶意任务的恶意代码的集合。 一、恶意代码分析 病毒&#xff08;Virus&#xff09; 定义&#xff1a;病毒是一种人为制造的、能够进行自我复制的、具有对计算机资源的破坏作用…

国家信息安全水平考试(NISP一级)最新题库-第十七章

目录 另外免费为大家准备了刷题小程序和docx文档&#xff0c;有需要的可以私信获取 1 受到了ARP欺骗的计算机&#xff0c;发出的数据包&#xff0c;     地址是错误的&#xff08;&#xff09; A.源IP&#xff1b;B.目的IP&#xff1b;C.源MAC&#xff1b;D.目的MAC 正…

rust入门基础总结

文章目录 前言1、输出格式规范一、占位符相关&#xff08;一&#xff09;{}与{:?} 二、参数替换方式&#xff08;一&#xff09;位置参数&#xff08;二&#xff09;具名参数 三、格式化参数&#xff08;一&#xff09;宽度&#xff08;二&#xff09;对齐&#xff08;三&…

基于K8S的StatefulSet部署mysql主从

StatefulSet特性 StatefulSet的网络状态 拓扑状态&#xff1a;应用的多个实例必须按照某种顺序启动&#xff0c;并且必须成组存在&#xff0c;例如一个应用中必须存在一个A Pod和两个B Pod&#xff0c;且A Pod必须先于B Pod启动的场景 存储状态&#xff1a;应用存在多个实例&…

Golang | Leetcode Golang题解之第500题键盘行

题目&#xff1a; 题解&#xff1a; func findWords(words []string) (ans []string) {const rowIdx "12210111011122000010020202" next:for _, word : range words {idx : rowIdx[unicode.ToLower(rune(word[0]))-a]for _, ch : range word[1:] {if rowIdx[unico…

【Redis】数据结构(下)

文章目录 QuickList概念QuickList结构QuickList的特点控制ZipList的大小对节点的ZipList进行压缩 总结 SkipList概念源码中结构分析总结 QuickList 概念 问题1:ZipList虽然节省内存,但是申请的内存必须是连续空间,如果内存占用过多,申请内存效率低,怎么办? 为了缓解这个问题,…

可编辑38页PPT | 柔性制造企业数字化转型与智能工厂建设方案

荐言分享&#xff1a;在数字化技术快速发展的今天&#xff0c;传统的大规模生产模式已难以满足市场对个性化、定制化产品的需求。柔性制造以其灵活多变、快速响应的特点&#xff0c;成为制造业转型升级的关键。通过数字化转型与智能工厂建设&#xff0c;企业可以构建高效、灵活…

Llama Tutor:开源 AI 个性化学习平台,根据主题自动制定学习计划

❤️ 如果你也关注大模型与 AI 的发展现状&#xff0c;且对大模型应用开发非常感兴趣&#xff0c;我会快速跟你分享最新的感兴趣的 AI 应用和热点信息&#xff0c;也会不定期分享自己的想法和开源实例&#xff0c;欢迎关注我哦&#xff01; &#x1f966; 微信公众号&#xff…

Centos7 将man手册内容转换为txt,pdf访问,并汉化

转换为txt格式 直接将man的输出导入到txt文档即可&#xff0c;使用col -b 删除掉特殊字符方便阅读 # man ps | col -b > ps.txt转换为pdf格式 使用 groff 将 man 页转换为 PostScript 格式&#xff0c;然后使用 ps2pdf 转换为 PDF&#xff1a; 下载ps2pdf 命令工具包&am…

8阻塞队列

阻塞队列能是⼀种 线程安全的数据结构 , 并且具有以下特性: • 当队列满的时候, 继续⼊队列就会阻塞, 直到有其他线程从队列中 取⾛ 元素. • 当队列空的时候, 继续出队列也会阻塞, 直到有其他线程往队列中 插⼊ 元素 标准库中的阻塞队列 在 Java 标准库中内置了阻塞队列. …

与ai一起作诗(《校园清廉韵》)

与ai对话犹如拷问自己的灵魂&#xff0c;与其说ai助力还不如说在和自己对话。 (笔记模板由python脚本于2024年10月19日 19:18:33创建&#xff0c;本篇笔记适合喜欢python和诗歌的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&#xff1a;https://www.python.org/ Free&…

NGINX 保护 Web 应用安全之基于 IP 地址的访问

根据客户端的 IP 地址控制访问 使用 HTTP 或 stream 访问模块控制对受保护资源的访问&#xff1a; location /admin/ { deny 10.0.0.1; allow 10.0.0.0/20; allow 2001:0db8::/32; deny all; } } 给定的 location 代码块允许来自 10.0.0.0/20 中的任何 IPv4 地址访问&#xf…

sql注入 --二次注入堆叠注入文件读取getshell

二次注入 二次注入原理&#xff1a; SQL二次注入&#xff0c;指的是在有些应用场景下&#xff0c;我们先把SQL注入的payload写入到目标站点数据库中&#xff0c;然后再在某些实际将该数据取出&#xff0c;使得我们写入的payload执行。 堆叠注入 条件&#xff1a; 因为堆叠注…

客户端工具xshell/finalshell连接Vagrant创建的虚拟机并上传本地文件

vagrant up #启动虚拟机 vagrant ssh #进入到虚拟机中 su root #切换超级管理员 vi /etc/ssh/sshd_config #修改PasswordAuthentication为yes; systemctl restart sshd #重启sshd服务修改之后 就可以使用xshell正常连接了

算法笔记day05

目录 1.最小公倍数 2.最长连续的子序列 3.字母收集 1.最小公倍数 求最小公倍数_牛客题霸_牛客网 算法思路&#xff1a; 这就是一道数学题&#xff0c;a,b的最小公倍数 a * b / 最大公约数。 使用辗转相除法&#xff0c;求a&#xff0c;b的最大公约数。 #include <iostre…

【Next.js 项目实战系列】05-删除 Issue

原文链接 CSDN 的排版/样式可能有问题&#xff0c;去我的博客查看原文系列吧&#xff0c;觉得有用的话&#xff0c;给我的库点个star&#xff0c;关注一下吧 上一篇【Next.js 项目实战系列】04-修改 Issue 删除 Issue 添加删除 Button​ 本节代码链接 这里我们主要关注布局…

ANSYS Workbench纤维混凝土3D

在ANSYS Workbench建立三维纤维混凝土模型可采用CAD随机几何3D插件建模后导入&#xff0c;模型包含球体粗骨料、圆柱体长纤维、水泥砂浆基体等不同组分。 在CAD随机几何3D插件内设置模型参数后运行&#xff0c;即可在AutoCAD内建立三维纤维混凝土模型&#xff0c;插件支持任意…

uniapp,获取头部高度

头部自定义时候&#xff0c;设置获取安全区域&#xff0c;可以用 uni.getSystemInfoSync();接口。 <view class"statusBar" :style"{height:statusBarHeightpx}"> let SYSuni.getSystemInfoSync(); let statusBarHeightref(SYS.statusBarHeight) …