uView 2.X 的 picker 组件实现省市区县联动选择功能

news/2024/5/17 15:39:18

uView 2.X 的 picker 组件实现省市区县联动选择功能

<template>
  <view class="container">
    <view class="p-card">
      <view class="p-title">
        <text>收货地址信息</text>
      </view>
      <view class="" style="margin-top: 22rpx;">
        <view
          class="u-flex u-flex-between"
          style="padding: 26rpx 0;border-bottom: 2rpx solid #ECECEC;"
        >
          <view class="">
            <text style="font-size: 30rpx;font-weight: 400;color: #424242;"
              >地区</text
            >
          </view>
          <view
            class="u-flex u-flex-between"
            style="width: 75%;"
            @click="handAddrClick"
          >
            <view class="" style="width: 90%;">
              <u--input
                placeholder="请选择省市区县"
                border="none"
                readonly
                v-model="pcaAddr"
              ></u--input>
              <u-picker
                :show="addrPickerShow"
                ref="uPicker"
                :columns="addrList"
                keyName="text"
                @confirm="confirm"
                @cancel="cancel"
                @change="changeHandler"
              ></u-picker>
            </view>
            <u-icon name="arrow-right" size="15"></u-icon>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        pcaAddr: "",
        treeList: [
          {
            text: "北京市",
            value: "110000",
            children: [
              {
                text: "北京市",
                value: "110100",
                children: [
                  {
                    text: "东城区",
                    value: "110101",
                  },
                  {
                    text: "西城区",
                    value: "110102",
                  },
                ],
              },
            ],
          },
          {
            text: "陕西省",
            value: "610000",
            children: [
              {
                text: "西安市",
                value: "610100",
                children: [
                  {
                    text: "新城区",
                    value: "610102",
                  },
                  {
                    text: "碑林区",
                    value: "610103",
                  },
                ],
              },
            ],
          },
        ],
        addrPickerShow: true,
        addrList: [],
      };
    },
    onLoad() {
      this.addrList = [
        this.treeList,
        this.treeList[0].children,
        this.treeList[0].children[0].children,
      ];
    },
    methods: {
      handAddrClick() {
        this.addrPickerShow = true;
      },
      changeHandler(e) {
        const {
          columnIndex,
          value,
          values, // values为当前变化列的数组内容
          index,
          // 微信小程序无法将picker实例传出来,只能通过ref操作
          picker = this.$refs.uPicker,
        } = e;
        // 当第一列值发生变化时,变化第二列(后一列)对应的选项
        if (columnIndex === 0) {
          // picker为选择器this实例,变化第二列对应的选项
          picker.setColumnValues(1, value[0].children);
          // picker为选择器this实例,变化第三列对应的选项
          picker.setColumnValues(2, value[0].children[0].children);
        }

        if (columnIndex === 1) {
          // picker为选择器this实例,变化第三列对应的选项
          picker.setColumnValues(2, value[0].children[index].children);
        }
      },
      // 回调参数为包含columnIndex、value、values
      confirm(e) {
        console.log("confirm", e);
        this.addrPickerShow = false;
      },
      cancel() {
        this.addrPickerShow = false;
      },
    },
  };
</script>

<style scoped>
  .container {
    width: 100vw;
    padding-top: 16rpx;
    min-height: calc(100vh - 16rpx);
    background: #eff0f5;
  }

  .p-card {
    width: 710rpx;
    box-sizing: border-box;
    min-height: 100rpx;
    background: #ffffff;
    border-radius: 16rpx;
    margin: auto;
    padding: 30rpx 24rpx;
  }

  .p-card + .p-card {
    margin-top: 16rpx;
  }

  .p-title {
    font-size: 34rpx;
    font-weight: 500;
    color: #232323;
  }
</style>

http://www.niftyadmin.cn/n/409322.html

相关文章

Rocky Linux9安装教程

序言 Centos废了&#xff0c;最近在考虑将服务器迁移至Rockylinux系统&#xff0c;在这里记录下安装过程 当前安装版本RockyLinux9.2&#xff08;minimal版本&#xff09;&#xff0c;VMware Fusion专业版13.0.2 创建虚拟机 第一步&#xff1a; 先下载好ISO文件&#xff0c…

final finally 和 finalize的区别

final、finally和finalize都是Java中的关键字&#xff0c;但它们的含义和用途却不同。 final 表示不可变&#xff0c;用于修饰类、方法和变量。 finally 表示无论如何都会执行的代码块&#xff0c;用于清理资源和恢复现场。 finalize 是Object类的一个方法&#xff0c;用于在…

【日志解析】【启发式】Drain:一种用于日志解析的深度解析树

Drain&#xff1a;An Online Log Parsing Approach with Fixed Depth Tree 文章目录 Drain&#xff1a;An Online Log Parsing Approach with Fixed Depth Tree1 论文出处2 背景2.1 背景介绍2.2 针对问题2.3 创新点 3 主要设计思路3.1 Drain整体结构3.2 具体步骤 4 实验设计5 个…

chatgpt赋能python:Python平铺图片的SEO优化

Python平铺图片的SEO优化 在网站设计中&#xff0c;图片是一个重要的元素。但是&#xff0c;使用大量的图片会导致网站加载速度变慢&#xff0c;从而影响用户的体验。为了解决这个问题&#xff0c;我们可以使用平铺图片&#xff0c;这样可以减小图片的大小&#xff0c;提高网站…

一、人像分割

开源PP-HumanSegl模型,提供了三个高质量的人像分割模型:有应用于服务端GPU部署的PP-HumanSegl模型,有适用于移动端的轻量PP-HumanSeg模型,还有能够在浏览器部署的超轻量模型PP-HumanSegs模型。 近期百度视频会议 上线了虚拟背景功能,支持在网页端视频会议时进行背景切…

Keras-3-实例2-多分类问题

1. 多分类问题&#xff1a; 1.1 路透社数据集加载 路透社数据集由路透社在1986年发布&#xff0c;包含46个不同的主题&#xff1a;某些主题样本较多&#xff0c;某些较少&#xff0c;但是训练集中每个主题都至少含有10个样本。 from keras.datasets import reuters(train_da…

【集群】LVS负载均衡群集(一)

文章目录 前言一、企业群集应用概述1. 群集的含义1.1 群集的特点1.2 扩展服务器的方式 2. 群集的类型2.1 负载均衡群集&#xff08;Load Balance Cluster&#xff09;2.2 高可用群集&#xff08;High Availability Cluster&#xff09;2.3 高性能运算群集&#xff08;High Perf…

总结893

学习目标&#xff1a; 月目标&#xff1a;6月&#xff08;线性代数强化9讲&#xff0c;背诵15篇短文&#xff0c;考研核心词过三遍&#xff09; 周目标&#xff1a;线性代数强化3讲&#xff0c;英语背3篇文章并回诵&#xff0c;检测 每日必复习&#xff08;5分钟&#xff09;…