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;
	}
相关推荐
游戏开发爱好者826 分钟前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息4 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”4 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
局外人LZ1 天前
Uniapp脚手架项目搭建,uniapp+vue3+uView pro+vite+pinia+sass
前端·uni-app·sass
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
前端呆头鹅1 天前
Websocket使用方案详解(uniapp版)
websocket·网络协议·uni-app
浮桥1 天前
uniapp+h5 公众号实现分享海报绘制
uni-app·notepad++
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview