uni-app 多列picker切换列显示对应内容

html部分:

复制代码
	<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						选择用户
					</view>
					<view class="uni-list-cell-db">
						<picker mode="multiSelector" @columnchange="bindMultiPickerColumnChange" :value="multiIndex"
							:range="multiArray">
							<view class="uni-input">
								{{multiArray[0][multiIndex[0]]}},{{multiArray[1][multiIndex[1]]}}
							</view>
						</picker>
					</view>
				</view>
			</view>

要先将后端传回的数据做个处理 :

复制代码
generateMultiArray() {
				const branchNames = [...new Set(this.userList.map(user => user.branchName))]; // 获取所有不同的 branchName
				this.userNameByBranch = []; // 根据 branchName 分组 userName

				branchNames.forEach(branch => {
					this.userNameByBranch[branch] = this.userList
						.filter(user => user.branchName === branch)
						.map(user => user.userName);
				});
				console.log('userNameByBranch', this.userNameByBranch)
				// 构建 multiArray
				const multiArray = [
					branchNames,
					this.userNameByBranch
				];
				
				this.multiArray = multiArray;
				
				this.bindMultiPickerColumnChange({ detail: { column: 0, value: 0 } }); // 设置默认选择
			},

后端返回的数据格式如下 "list": [
            {
                "userId": 1,
                "userName": "王三",
                "branchName": "软件部"
            },
            {
                "userId": 2,
                "userName": "李四",
                "branchName": "软件部"
            },
            {
                "userId": 3,
                "userName": "赵六",
                "branchName": "软件部"
            },
            {
                "userId": 4,
                "userName": "王齐",
                "branchName": "软件部"
            },
            {
                "userId": 4,
                "userName": "发寒热",
                "branchName": "项目部"
            },
            {
                "userId": 4,
                "userName": "王总",
                "branchName": "总经理"
            }
        ]

然后写切换逻辑:

复制代码
			bindMultiPickerColumnChange(e) {
				console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value)
				this.multiIndex[e.detail.column] = e.detail.value

				// 如果修改的是第一列
				if (e.detail.column === 0) {
					// 获取用户选择的分支名称
					const selectedBranch = this.multiArray[0][e.detail.value];

					// 根据选择的分支名称更新第二列的值
					const userNameArray = this.userNameByBranch[selectedBranch] || []; // 获取对应分支名称的用户名数组

					// 更新第二列的值为对应的用户名数组
					this.multiArray[1] = userNameArray;

					// 重置第二列的选择索引为0
					this.multiIndex[1] = 0;
				}

				// 强制更新视图
				this.$forceUpdate();
			},

css部分:

复制代码
.uni-list {
		background-color: #f6f6f6;
		position: relative;
		width: 96%;
		margin: 0 auto;
		display: flex;
		flex-direction: column;
	}

	.uni-list:after {
		position: absolute;
		z-index: 10;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: white;
	}

	.uni-list-cell {
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.uni-list-cell-left {
		white-space: nowrap;
		font-size: 28rpx;
		padding: 0 30rpx;
	}

	.uni-list-cell-db {
		flex: 1;
	}

	.uni-input {
		height: 50rpx;
		padding: 15rpx 25rpx;
		line-height: 50rpx;
		font-size: 28rpx;
		background: #f6f6f6;
		flex: 1;
	}
相关推荐
2501_9159184128 分钟前
iOS 26 App 性能测试|性能评测|iOS 26 性能对比:实战策略
android·macos·ios·小程序·uni-app·cocoa·iphone
用户9047066835711 小时前
uniapp Vue3版本,用pinia存储持久化插件pinia-plugin-persistedstate对微信小程序的配置
前端·uni-app
乔冠宇12 小时前
uniapp创建ts项目tsconfig.json报错的问题
uni-app
细节控菜鸡12 小时前
【2025最新】uniapp 中基于 request 封装实现多文件上传完整指南
uni-app
fakaifa12 小时前
【全开源】企业微信SCRM社群营销高级版系统+uniapp前端
uni-app·开源·企业微信·scrm·源码下载·企业微信scrm
棋子一名15 小时前
跑马灯组件 Vue2/Vue3/uni-app/微信小程序
微信小程序·小程序·uni-app·vue·js
游戏开发爱好者816 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088916 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9151063218 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214319 小时前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式