微信小程序开发 逐级选择地区

1.需求

  微信小程序开发,逐级选择地区(市、区县、街道、社区、网格),选择每一级然后展示下一级数据。

微信小程序逐级选择

2. 完整代码

2.1. 选择界面

在这里插入图片描述

2.1.1. selectArea.wxml

<text bindtap="selectGrid">{{gridName}}</text>
<!-- 选择地区位置 -->
<view class="area_selection" hidden="{{area_selection}}">
  <view class="communitypop_con">
    <selectionbox id="selectionbox" class="communitypop" bind:cliclCancel="cliclCancel" bind:cliclConfirm="cliclConfirm">
    </selectionbox>
  </view>
</view>

2.1.2. selectArea.wxss

.area_selection {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 800;
}

.communitypop_con {
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
}

.communitypop {
  width: 90%;
  height: 80%;
  overflow-y: auto;
  background: white;
  border-radius: 8rpx;
}

2.1.3. selectArea.json

{
  "usingComponents": {
    "selectionbox":"../selectionBox/selectionBox"
  }
}

2.1.4. selectArea.js

// pages/selectArea/selectArea.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    area_selection: true,
    gridName:"请选择"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.selectionbox = that.selectComponent("#selectionbox");

  },

  /**
   * 获取网格
   */
  selectGrid: function () {
    let that = this
    that.selectionbox.getCityData()
    that.setData({
      area_selection: false
    })
  },
  // 地区选择框确定
  cliclConfirm: function (e) {
    let that = this
    var gridObj = e.detail
    that.setData({
      gridName: gridObj.name,
      area_selection: true
    })
  },
  // 地区选择框取消
  cliclCancel: function (e) {
    let that = this
    that.setData({
      area_selection: true
    })
  },
})

2.2. 逐级选择地区弹出框

2.2.1. selectionBox.wxml

<view class="page-body">
  <view class="header-layout">
    <text class="header-title">所在网格:</text>
    <view class="header-content">
      <text >{{cityStr}}</text>
      <text hidden="{{isCountyHidden}}">>{{countyStr}}</text>
      <text hidden="{{isStreetHidden}}">>{{streetStr}}</text>
      <text hidden="{{isCommunityHidden}}">>{{communityStr}}</text>
      <text hidden="{{isGridHidden}}">>{{gridStr}}</text>
    </view>
  </view>
  <!-- 所属市 -->
  <view>
    <view class="item-header-top">所属市: </view>
    <view class="item-layout">
      <view class="item-title {{index+1== cityIndex?'item-title-active':''}}" bindtap="cityClick" wx:for="{{cityList}}" data-index="{{index}}" data-item-id="{{item.id}}" data-value="{{item.name}}" wx:for-index="index" wx:key="index">{{item.name || '暂无'}}</view>
    </view>
  </view>
  <!-- 所属区县 -->
  <view hidden="{{isCountyHidden}}">
    <view class="item-header">所属区县:</view>
    <view class="item-layout">
      <view class="item-title {{index+1== countyIndex?'item-title-active':''}}" bindtap="countyClick" wx:for="{{countyList}}" data-index="{{index}}" data-item-id="{{item.id}}" data-value="{{item.name}}" wx:for-index="index" wx:key="index">{{item.name || '暂无'}}</view>
    </view>
  </view>
  <!-- 所属街镇 -->
  <view hidden="{{isStreetHidden}}">
    <view class="item-header">所属街镇:</view>
    <view class="item-layout">
      <view class="item-title {{index+1== streetIndex?'item-title-active':''}}" bindtap="streetClick" wx:for="{{streetList}}" data-index="{{index}}" data-item-id="{{item.id}}" data-value="{{item.name}}" wx:for-index="index" wx:key="index">{{item.name || '暂无'}}</view>
    </view>
  </view>
  <!-- 所属社区 -->
  <view hidden="{{isCommunityHidden}}">
    <view class="item-header">所属社区:</view>
    <view class="item-layout">
      <view class="item-title {{index+1== communityIndex?'item-title-active':''}}" bindtap="communityClick" wx:for="{{communityList}}" data-index="{{index}}" data-item-id="{{item.id}}" data-value="{{item.name}}" wx:for-index="index" wx:key="index">{{item.name || '暂无'}}</view>
    </view>
  </view>
  <!-- 所属网格 -->
  <view hidden="{{isGridHidden}}">
    <view class="item-header">所属网格:</view>
    <view class="item-layout">
      <view class="item-title {{index+1== gridIndex?'item-title-active':''}}" bindtap="gridClick" wx:for="{{gridList}}" data-index="{{index}}" data-item-id="{{item.id}}" data-value="{{item.name}}" wx:for-index="index" wx:key="index">{{item.name || '暂无'}}</view>
    </view>
  </view>
  <view class="item-interval"></view>
  <view class="opera-layout">
    <text class="opera-item-cancel" bindtap="cliclCancel">取消</text>
    <text class="opera-item-confirm" bindtap="cliclConfirm">确定</text>
  </view>
</view>

2.2.2. selectionBox.wxss

@import "../../public/wxss/base.wxss";

.page-body{
  width: 100%;
  height: 100%;
  border: 1rpx solid white;
  border-radius: 10rpx;
  box-sizing: border-box;
  background-color: white;
}

.header-layout {
  background-color: white;
  display: flex;
  flex-direction: column;
  margin-bottom: 20rpx;
  position: absolute;
  width: 90%;
  padding: 15rpx 10rpx;
}
.header-title {
  color: #000;
  font-weight: 800;
  font-size: 32rpx;
  margin-top: 10rpx;
}
.header-content {
  display: flex;
  flex-direction: row;
  flex: 1;
  color: #333;
  font-size: 30rpx;
}
.item-title-top {
  margin: 80rpx 20rpx 10rpx;
  color: #333;
  font-weight: 700;
  font-size: 30rpx;
}
.item-header-top {
  margin: 120rpx 20rpx 10rpx;
  color: #0B8DF8;
  font-weight: 700;
  font-size: 30rpx;
}
.item-header {
  margin: 0 20rpx 10rpx;
  color: #0B8DF8;
  font-weight: 700;
 font-size: 30rpx;
}
.item-layout {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  padding: 10rpx 0 10rpx;
  flex-wrap: wrap;
  padding-bottom: 30rpx;
  background-color: white;
}

.item-title{
  width: 21%;
  color: #000;
  font-size: 30rpx;
  text-align: center;
  padding:10rpx 10rpx;
  box-shadow: 0 0 5rpx #bbb;
  border-radius: 10rpx;
  background: white;
  margin:10rpx 2.0% 10rpx;
}
.item-title-active{
  color: white;
  background: #0B8DF8;
}

.item-interval {
  height: 20rpx;
 }
 .opera-layout{
  position: absolute;
  left: 5%;
  bottom: 60rpx;
  width: 90%;
  background: #fff;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 10rpx 80rpx 30rpx;
  border-radius: 10rpx;
  font-size: 34rpx;
}
 .opera-item-cancel{
  display: inline-block;
  color: white;
  background:#ccc;
  border-radius: 70rpx;
  padding: 10rpx 50rpx;
}
.opera-item-confirm{
  display: inline-block;
  color: white;
  background:#0B8DF8;
  border-radius: 70rpx;
  padding: 10rpx 50rpx;
}

2.2.3. selectionBox.js

import areaJson from '../../public/json/areaJson';
Page({
  data: {
    comId: null,
    ciryList: [],
    countyList: [],
    streetList: [],
    communityList: [],
    gridList: [],
    isCountyHidden: true,
    isStreetHidden: true,
    isCommunityHidden: true,
    isGridHidden: true,
    cityIndex: null,
    countyIndex: null,
    streetIndex: null,
    communityIndex: null,
    gridIndex: null,
  },
  /**
   * 获取市级数据
   */
  getCityData: function () {
    let that = this
    that.setData({
      cityList: areaJson.data.areaList
    })
  },
  /**
   * 获取区县数据
   */
  getCountyData: function (itemid) {
    let that = this
    that.setData({
      countyList: areaJson.data.countyList
    })
  },
  /**
   * 获取街道数据
   */
  getStreetData: function (itemid) {
    let that = this
    that.setData({
      streetList: areaJson.data.streetList
    })
  },
  /**
   * 获取社区数据
   */
  getCommunityData: function (itemid) {
    let that = this
    that.setData({
      communityList: areaJson.data.communityList
    })
  },
  /**
   * 获取网格数据
   */
  getGridData: function (itemid) {
    let that = this
    that.setData({
      gridList: areaJson.data.gridList
    })
  },
  /**
   * 市级点击
   */
  cityClick: function (e) {
    var that = this
    var itemIndex = parseInt(e.currentTarget.dataset.index) + 1
    var itemValue = e.currentTarget.dataset.value
    var itemId = e.currentTarget.dataset.itemId
    that.setData({
      countyList: [],
      streetList: [],
      communityList: [],
      gridList: [],
      isCountyHidden: false,
      isStreetHidden: true,
      isCommunityHidden: true,
      isGridHidden: true,
      cityIndex: itemIndex,
      countyIndex: null,
      streetIndex: null,
      communityIndex: null,
      gridIndex: null,
      cityStr: itemValue,
      countyStr: '',
      streetStr: '',
      communityStr: '',
      gridStr: ''
    })
    //获取区县数据
    that.getCountyData(itemId)
  },
  /**
   * 区县点击
   */
  countyClick: function (e) {
    var that = this
    var itemIndex = parseInt(e.currentTarget.dataset.index) + 1
    var itemValue = e.currentTarget.dataset.value
    var itemId = e.currentTarget.dataset.itemId
    that.setData({
      streetList: [],
      communityList: [],
      gridList: [],
      isStreetHidden: false,
      isCommunityHidden: true,
      isGridHidden: true,
      countyIndex: itemIndex,
      streetIndex: null,
      communityIndex: null,
      gridIndex: null,
      countyIndex: itemIndex,
      streetIndex: null,
      communityIndex: null,
      gridIndex: null,
      countyStr: itemValue,
      streetStr: '',
      communityStr: '',
      gridStr: ''
    })
    //获取街道数据
    that.getStreetData(itemId)
  },
  /**
   * 街道点击
   */
  streetClick: function (e) {
    var that = this
    var itemIndex = parseInt(e.currentTarget.dataset.index) + 1
    var itemValue = e.currentTarget.dataset.value
    var itemId = e.currentTarget.dataset.itemId
    that.setData({
      communityList: [],
      gridList: [],
      isCommunityHidden: false,
      isGridHidden: true,
      streetIndex: itemIndex,
      communityIndex: null,
      gridIndex: null,
      streetStr: itemValue,
      communityStr: '',
      gridStr: ''
    })
    //获取社区数据
    that.getCommunityData(itemId)
  },
  /**
   * 社区点击
   */
  communityClick: function (e) {
    var that = this
    var itemIndex = parseInt(e.currentTarget.dataset.index) + 1
    var itemValue = e.currentTarget.dataset.value
    var itemId = e.currentTarget.dataset.itemId
    that.setData({
      gridList: [],
      isGridHidden: false,
      communityIndex: itemIndex,
      gridIndex: null,
      communityStr: itemValue,
      gridStr: ''
    })
    //获取网格数据
    that.getGridData(itemId)
  },
  /**
   * 网格点击
   */
  gridClick: function (e) {
    var that = this
    var itemIndex = parseInt(e.currentTarget.dataset.index) + 1
    var itemValue = e.currentTarget.dataset.value
    that.setData({
      gridIndex: itemIndex,
      deptId: itemIndex-1,
      gridStr: itemValue,
    })
  },
  // 取消选择
  cliclCancel: function () {
    var that = this
    //清除数据
    that.clearData()
    that.triggerEvent('cliclCancel')
  },
  /**
   * 清除数据
   */
  clearData: function () {
    var that = this
    that.setData({
      ciryList: [],
      countyList: [],
      streetList: [],
      communityList: [],
      gridList: [],
      isCountyHidden: true,
      isStreetHidden: true,
      isCommunityHidden: true,
      isGridHidden: true,
      cityIndex: null,
      countyIndex: null,
      streetIndex: null,
      communityIndex: null,
      gridIndex: null,
      cityStr: '',
      countyStr: '',
      streetStr: '',
      communityStr: '',
      gridStr: ''
    })
  },
  // 确定选择
  cliclConfirm: function () {
    let that = this
    var cityStr = that.data.cityStr
    var countyStr = that.data.countyStr
    var streetStr = that.data.streetStr
    var communityStr = that.data.communityStr
    var gridStr = that.data.gridStr
    if (!cityStr) {
      wx.showToast({
        icon: 'none',
        title: '请选择市',
      })
      return
    } else if (!countyStr) {
      wx.showToast({
        icon: 'none',
        title: '请选择区县',
      })
      return
    } else if (!streetStr) {
      wx.showToast({
        icon: 'none',
        title: '请选择街镇',
      })
      return
    } else if (!communityStr) {
      wx.showToast({
        icon: 'none',
        title: '请选择社区',
      })
      return
    } else if (!gridStr) {
      wx.showToast({
        icon: 'none',
        title: '请选择网格',
      })
      return
    } 
    //清除数据
    that.clearData()
    var obj = {}
    obj.id = that.data.deptId
    obj.name = cityStr+countyStr+streetStr+communityStr+gridStr
    if (obj) {
      that.triggerEvent('cliclConfirm', obj)
    } else {
      wx.showToast({
        icon: 'none',
        title: '缺少参数',
      })
    }
  },
})

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/355029.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Java技术栈 —— Hadoop入门(二)

Java技术栈 —— Hadoop入门&#xff08;二&#xff09; 一、用MapReduce对统计单词个数1.1 项目流程1.2 可能遇到的问题1.3 代码勘误1.4 总结 一、用MapReduce对统计单词个数 1.1 项目流程 (1) 上传jar包。 (2) 上传words.txt文件。 (3) 用hadoop执行jar包的代码&#xff0c;…

go语言基础之泛型

1.泛型 泛型是一种独立于所使用的特定类型的编写代码的方法。使用泛型可以编写出适用于一组类型中的任何一种的函数和类型。 1.1 为什么需要泛型 func reverse(s []int) []int {l : len(s)r : make([]int, l)for i, e : range s {r[l-i-1] e}return r }fmt.Println(reverse…

ACL、VLAN、NAT笔记

一、ACL ---访问控制列表 1.ACL的作用 1&#xff0c;访问控制&#xff1a;在路由器流量流入或流出的接口上&#xff0c;匹配流量&#xff0c;然后 执行设定好的动作。 ---- permit 允许 , deny 拒绝 2&#xff0c;抓取感兴趣流&#xff1a;ACL可以和其他服务结合使用。ACL只…

Mac安装nvm,安装多个不同版本node,指定node版本

一.安装nvm brew install nvm二。配置文件 touch ~/.zshrc echo export NVM_DIR~/.nvm >> ~/.zshrc echo source $(brew --prefix nvm)/nvm.sh >> ~/.zshrc三.查看安装版本 nvm -vnvm常用命令如下&#xff1a;nvm ls &#xff1a;列出所有已安装的 node 版本nvm…

CMake 完整入门教程(五)

CMake 使用实例 13.1 例子一 一个经典的 C 程序&#xff0c;如何用 cmake 来进行构建程序呢&#xff1f; //main.c #include <stdio.h> int main() { printf("Hello World!/n"); return 0; } 编写一个 CMakeList.txt 文件 ( 可看做 cmake 的…

导航页配置服务Dashy本地部署并实现公网远程访问

文章目录 简介1. 安装Dashy2. 安装cpolar3.配置公网访问地址4. 固定域名访问 简介 Dashy 是一个开源的自托管的导航页配置服务&#xff0c;具有易于使用的可视化编辑器、状态检查、小工具和主题等功能。你可以将自己常用的一些网站聚合起来放在一起&#xff0c;形成自己的导航…

Unity 中介者模式 (实例详解)

文章目录 简介实例1&#xff1a;玩家与UI交互实例2&#xff1a;战斗模块中的攻击事件协调实例3&#xff1a;游戏场景中的事件广播实例4&#xff1a;模块间通信 - 地图导航与角色移动实例5&#xff1a;UI模块间同步 - 菜单切换与选项状态 简介 在Unity游戏开发中&#xff0c;中…

SpringBoot内置工具类

Collections java.util包下的Collections类&#xff0c;该类主要用于操作集合或者返回集合 一、排序 List<Integer> list new ArrayList<>();list.add(2);list.add(1);list.add(3);Collections.sort(list);//升序System.out.println(list);Collections.reverse(…

【机器学习】欠拟合与过拟合

过拟合&#xff1a;模型在训练数据上表现良好对不可见数据的泛化能力差。 欠拟合&#xff1a;模型在训练数据和不可见数据上泛化能力都很差。 欠拟合常见解决办法&#xff1a; &#xff08;1&#xff09;增加新特征&#xff0c;可以考虑加入特征组合、高次特征&#xff0c;以…

MySql45讲-08.事务到底是隔离的还是不隔离的?(结合MVCC视频)

命令的启动时机 begin/start transaction 命令并不是一个事务的起点&#xff0c;在执行到它们之后的第一个操作InnoDB表的语句&#xff0c;事务才真正启动。如果你想要马上启动一个事务&#xff0c;可以使用start transaction with consistent snapshot 这个命令。 事务的版本…

数据结构—基础知识(14):森林、树与二叉树的转换

数据结构—基础知识&#xff08;14&#xff09;&#xff1a;森林、树与二叉树的转换 将树转换为二叉树进行处理&#xff0c;利用二叉树的算法来实现对树的操作。由于树和二叉树都可以用二叉链表作存储结构&#xff0c;则以二叉链表作媒介可以导出树与二叉树之间的一个对应关系…

Nginx负载均衡下的webshell连接

一、上传AntSword-Labs-master搭建负载均衡实验环境 搭建好docker环境&#xff0c;并且配置好docker-compose 我的Redhat的docker版本&#xff1a; 查看当前环境下的文件是否正确&#xff1a; 接着执行docker compose up -d 拉取环境 访问成功页面&#xff1a; 进入docker容器…

RabbitMQ 死信交换机的详述➕应用

&#x1f973;&#x1f973;Welcome 的Huihuis Code World ! !&#x1f973;&#x1f973; 接下来看看由辉辉所写的关于RabbitMQ的相关操作吧 目录 &#x1f973;&#x1f973;Welcome 的Huihuis Code World ! !&#x1f973;&#x1f973; 一.什么是死信交换机 二. 死信队列…

C++1.0

思维导图 提示输入一个字符串&#xff0c;统计该字符中大写&#xff0c;小写字母个数&#xff0c;数字个数&#xff0c;空格个数以及特殊字符个数&#xff0c;要求使用C风格字符串完成 #include <iostream>using namespace std;int main() {cout << "请输入一…

路由懒加载(React和Vue)

1、为了提升性能&#xff0c;将懒加载的文件单独打包 在webpack.config.js配置打包成chunks // 打包到不同的chunks optimization: {// 将动态加载(懒加载)的文件(imort())单独打包splitChunks: {chunks: "all",},// 避免分割缓存失效runtimeChunk: {name: (entrypo…

数据结构(C语言版)代码实现(五)——双向循环链表的部分实现

目录 参考材料与格式 线性表的有关知识 头文件 库、宏定义、数据类型声明 线性表的双向链表存储结构 构造空链表 销毁链表 链表长度 按位查找 插入元素 删除元素 打印链表 完整头文件DuLinkList.h 测试函数&#xff08;主函数&#xff09; 测试结果 收获 参考材…

电磁兼容(EMC):产品如何做到可靠的防静电设计

工业产品所应用的电磁环境之恶劣。要想产品在如此恶劣的电磁环境下正常工作&#xff0c;需要具备强大的抗干扰能力方能胜任。其中以静电干扰最为常见且棘手。本文将手把手教你如何将工业产品做到可靠的防静电设计。 1 了解静电 你想要打倒对手&#xff0c;必须先深入地了解他…

Redis 学习笔记 2:Java 客户端

Redis 学习笔记 2&#xff1a;Java 客户端 常见的 Redis Java 客户端有三种&#xff1a; Jedis&#xff0c;优点是API 风格与 Redis 命令命名保持一致&#xff0c;容易上手&#xff0c;缺点是连接实例是线程不安全的&#xff0c;多线程场景需要用线程池来管理连接。Redisson&…

预训练语言模型transformer

预训练语言模型的学习方法有三类&#xff1a;自编码&#xff08;auto-encode, AE)、自回归&#xff08;auto regressive, AR&#xff09;&#xff0c;Encoder-Decoder结构。 决定PTM模型表现的真正原因主要有以下几点&#xff1a; 更高质量、更多数量的预训练数据增加模型容量…

双非本科准备秋招(8.2)——JVM1

第一天系统学习JVM&#xff01;今天学了JVM是什么&#xff0c;学习JVM的作用&#xff0c;运行时的数据区域&#xff08;重点&#xff09;&#xff0c;内存溢出。明天学GC。 运行时数据区域 整体认识 JDK1.7 JDK1.8 先写一下每个线程私有的三个数据区&#xff0c;分别是程序计…
最新文章