uniapp的多列选择器

直接粘贴~

javascript 复制代码
<template>
  <view class="container">
    <form>
      <picker mode="multiSelector" :range="multiArray" @change="onMultiChange" @columnchange="onMultiColumnChange">
        <view class="picker">
          <text>{{ multiArray[0][selectedIndex[0]] }}</text>
          <text class="iconfont icon-down"></text>
          <text>{{ multiArray[1][selectedIndex[1]] }}</text>
          <text class="iconfont icon-down"></text>
        </view>
      </picker>
    </form>
  </view>
</template>

<script>
export default {
  data() {
    return {
      multiArray: [],     // 两列联动数据
      selectedIndex: [0, 0], // 当前选中的索引,市、区
      cityList: [        // 市、区数据,仅保留广州市和深圳市
        {
          name: "广州市",
          districtList: ["荔湾区", "越秀区", "海珠区", "天河区", "白云区", "黄埔区", "番禺区", "花都区", "南沙区", "增城区", "从化区"]
        },
        {
          name: "深圳市",
          districtList: ["罗湖区", "福田区", "南山区", "宝安区", "龙岗区", "盐田区", "龙华区", "坪山区", "光明区", "大鹏新区"]
        },
		{
		  name: "xx市",
		  districtList: ["xx区", "福田区", "南山区", "宝安区", "龙岗区", "盐田区", "龙华区", "坪山区", "光明区", "大鹏新区"]
		}
      ]
    }
  },
  created() {
    // 初始化两列联动数据
    this.multiArray = [
      this.cityList.map(city => city.name), // 市列表,广州市和深圳市
      this.cityList[0].districtList // 区列表,初始化为广州市的区
    ];
  },
  methods: {
    onMultiChange(e) {
      this.selectedIndex = e.detail.value;
      // 当用户选择市时,更新区数据
      const selectedCity = this.cityList[this.selectedIndex[0]];
      this.multiArray[1] = selectedCity.districtList;
      this.selectedIndex[1] = 0;
    },
    onMultiColumnChange(e) {
      const column = e.detail.column;
      const value = e.detail.value;
      if (column === 0) {
        // 当用户改变市时,更新区数据
        const selectedCity = this.cityList[value];
        this.multiArray[1] = selectedCity.districtList;
        this.selectedIndex[1] = 0;
      }
    }
  }
}
</script>

<style>
.container {
  padding: 20px;
}
.picker {
  padding: 10px;
  background-color: #f0f0f0;
  border-radius: 5px;
  text-align: center;
  margin-bottom: 20px;
}
.iconfont {
  margin-left: 10px;
}
</style>
相关推荐
予你@。10 小时前
uni-app(Vue3)实现自定义 Tab 切换滑块效果(微信小程序)
vue.js·微信小程序·uni-app
HashTang1 天前
【AI 编程实战】第 11 篇:让小程序飞起来 - 性能优化实战指南
前端·uni-app·ai编程
lruri1 天前
记录一个修复nvue文件在vscode里面提示ts-plugin报错
uni-app
蓝帆傲亦1 天前
Web前端Mock数据实战指南:正确使用Mock.js提升开发效率
微信小程序·小程序·uni-app
00后程序员张1 天前
iOS 应用代码混淆,对已编译 IPA 进行类与方法混淆
android·ios·小程序·https·uni-app·iphone·webview
木子啊2 天前
Uni-app社会化功能:登录支付分享全攻略
uni-app
笨笨狗吞噬者2 天前
【2025】加入 uniapp 的一年
前端·uni-app
2501_915918412 天前
mobileprovision 描述文件内容查看,查看 Bundle ID、证书指纹等来定位安装与签名问题
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
HTTPS 端口解析,除了 443 端口还有哪些
android·ios·小程序·https·uni-app·iphone·webview
三十_2 天前
uniApp WebView 动态配置加载状态监控与容错方案
前端·uni-app