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>
相关推荐
yqcoder1 天前
uni-app 之 页面路由
uni-app
小离a_a1 天前
uniapp小程序添加路由全局限制,增加路由白名单,登录后接口跳转参数正常传递
小程序·uni-app
游九尘1 天前
uniapp获取定位uni.getLocation报错getLocation:fail maybe not obtain GPS Permission.
uni-app
雪芽蓝域zzs2 天前
uniapp 该应用与此设备的CPU不兼容
uni-app
CHB2 天前
uni-task - 轻量级团队任务管理系统
uni-app
行思理2 天前
UniApp 打包配置 iOS的UniversalLinks
uni-app·universal link
雪芽蓝域zzs2 天前
uni-app x 使用 UTS 语言使用 mixins
开发语言·javascript·uni-app
雪芽蓝域zzs2 天前
uni-app x 中使用 UTS 语言实现兼容鸿蒙的加密
华为·uni-app·harmonyos
2501_915909062 天前
苹果App Store上架全流程指南从注册到上线
android·ios·小程序·https·uni-app·iphone·webview
anyup3 天前
uni-app 全能日历组件,支持农历、酒店预订、打卡签到、价格日历多种场景
前端·前端框架·uni-app