自定义组件之三级联动

简介:  小程序的联动已经出了来,但是还是想手动写个,说干就干pickers.axml{{province}}-{{city}}-{{county}}取消确定{{item}}{{item}}{{item}}pickers.

 小程序的联动已经出了来,但是还是想手动写个,说干就干

pickers.axml


  
  
    
   
   
        
    
    
     {{province}}-{{city}}-{{county}}
    
    
    
   
   

  
  

  
  
    
   
   
        
    
    
            
     
     
                
      
      
        取消 
      
                
      
      
        确定 
      
            
     
     
            
     
     
                
      
       
       
         {{item}} 
        
      
                
      
       
       
         {{item}} 
        
      
                
      
       
       
         {{item}} 
        
      
            
     
     
        
    
    
    
   
   

  
  
pickers.js

Component({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    cityData: "",
    isShow: false
  },
  didMount() {
    var that = this;
    that.setData({
      'cityData': this.props.value
    });
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    console.log('初始化完成');

  },
  methods: {
    onChange(e) {
      console.log(e)
      let val = e.detail.value
      let t = this.data.values;
      let cityData = this.data.cityData;

      if (val[0] != t[0]) {
        console.log('province no ');
        const citys = [];
        const countys = [];
        for (let i = 0; i < cityData[val[0]].sub.length; i++) {
          citys.push(cityData[val[0]].sub[i].name)
        }
        for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
          countys.push(cityData[val[0]].sub[0].sub[i].name)
        }
        this.setData({
          province: this.data.provinces[val[0]],
          city: cityData[val[0]].sub[0].name,
          citys: citys,
          county: cityData[val[0]].sub[0].sub[0].name,
          countys: countys,
          values: val,
          value: [val[0], 0, 0]
        })
        return;
      }
      if (val[1] != t[1]) {
        console.log('city no');
        const countys = [];
        for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
          countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
        }
        this.setData({
          city: this.data.citys[val[1]],
          county: cityData[val[0]].sub[val[1]].sub[0].name,
          countys: countys,
          values: val,
          value: [val[0], val[1], 0]
        })
        return;
      }
      if (val[2] != t[2]) {
        console.log('county no');
        this.setData({
          county: this.data.countys[val[2]],
          values: val
        })
        return;
      }

    },
    open() {
      this.setData({
        isShow: true
      })
    },
    handleOk() {
      let address=[this.data.province,this.data.city,this.data.county];
      
      this.props.onChange(address)
      this.setData({
        isShow: false
      })
    },
    handleNo() {
     
    var that = this;
    that.setData({
      'cityData': this.props.value
    });
    var cityData = that.data.cityData;
    console.log("onLoad", cityData);
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[ i ].name);
    }
    console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[ i ].name)
    }
    console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name,
        isShow: false,
        value: [0, 0, 0],
        values: [0, 0, 0],
    })
    console.log('初始化完成');
     let address=[this.data.province,this.data.city,this.data.county]
     
     this.props.onChange(address)
    }
    
  }

});
pickers.acss



.input-box{
 display: flex;
 align-items: center;
 height: 300rpx;
 padding: 20rpx;

}
.input-box .input{
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 32rpx;
  color: #646464;
  padding: 0 10rpx;
  border: 1px solid #000;
}
  .city-picker-mask{
  
   left: 0;
  bottom:0; 
  position: fixed;
  height: 10%;
  width: 100%;
  min-height: 100%; 
  background-color: rgba(0,0,0,.5);
  z-index: 160;
  
  
} 
.city-picker-mask .city-picker-inner{
  position: absolute;
  left: 0;
  bottom:0;
  background-color: #fff;
  width: 100%;
}
.city-picker-inner .citybody {
   width: 100%;
}
.city-picker-inner .city-header {
  box-sizing: border-box;
  width: 100%;
  height: 30px;
  line-height: 30px;
  z-index: 4;
  padding: 0 20rpx;
  background-color: #fff;
}
.city-header .city-cancel {
  float: left;
  color: #818181;
}
.city-header .city-true {
  float: right;
  color: #2FB42E
}
.city-picker-inner .picker-column-item{
   width: 100%;
   color: #646464;
   font-size: 28rpx;
  } 
  .swiper-item{
    height: 90px;
  }
.bc_red{
  background-color: red;
}
.bc_green{
  background-color: green;
}
.bc_yellow{
  background-color: yellow;
}
.font{
  font-size: 40px;
  font-family:"STHupo" !important;
}
picker.json

{
    "component":true
}

注意:由于[i]会被论坛过滤掉,我这里把会过滤掉的截图贴出来  

  注意总共是有九个[i]

这样组件就开发出来了,接下来就是使用了,下面以在index页面中使用为例

index.axml


  
  
index.js

import tcity from '/util/citys.js';

Page({
  data: {
    cityData:"",
  },

  onChange(val,code) {
  console.log("com",val)
  console.log("comcode",code)
  },
  onLoad() {
    tcity.init(this);//这个是省市县的数据数据比较多就写在citys.js里了
  },
});
index.json

{
    "usingComponents":{
        "pickers":"/pages/test/test"  //这里写上自己的路径
    }
}

顺便把citys的也贴出来

citys.js

let cityData= [ 
       ...    //这里面就是省市县的数据 太多了,我就以附件形式传一下
  ]



function init(that){
  that.setData( { 
        'cityData': cityData
    });
}

module.exports={
    init:init
}

 

这样一个地区三级联动就完成。

目录
相关文章
|
存储 关系型数据库 数据库
深入了解 PostgreSQL:功能、特性和部署
PostgreSQL,通常简称为Postgres,是一款强大且开源的关系型数据库管理系统(RDBMS),它在数据存储和处理方面提供了广泛的功能和灵活性。本文将详细介绍 PostgreSQL 的功能、特性以及如何部署和使用它。
1514 1
深入了解 PostgreSQL:功能、特性和部署
|
安全 机器人 数据安全/隐私保护
steam注册教程,8个步骤拥有属于自己的steam账户
steam注册教程,从小白到高手,只差这篇教程!
2602 4
steam注册教程,8个步骤拥有属于自己的steam账户
Vue3级联选择(Cascader)
这是一个可定制的级联选择器组件,支持多级下拉选项。主要属性包括:数据源、文本字段、值字段、后代字段、占位符文本、选择行为、间距、宽度、高度、禁用状态、清除功能、搜索功能及过滤函数等。组件可根据需求灵活配置,并支持动态更新与事件回调。在线预览和详细示例可见[这里](https://themusecatcher.github.io/vue-amazing-ui/guide/components/cascader.html)。
1261 2
Vue3级联选择(Cascader)
|
JavaScript
Vue2级联选择(Cascader)
这是一个基于 Vue 3 的级联选择组件,支持多种自定义属性,如数据源、选中项、文本字段等。提供了丰富的配置项,如层级间隙、宽度、高度、禁用状态和占位符等,便于灵活使用。组件通过监听选择变化并触发回调事件,实现了动态更新与交互。
1058 1
Vue2级联选择(Cascader)
|
开发框架 前端开发 JavaScript
循序渐进VUE+Element 前端应用开发(21)--- 省市区县联动处理的组件使用
循序渐进VUE+Element 前端应用开发(21)--- 省市区县联动处理的组件使用
|
Java API 开发者
如何在Spring Boot中设计API返回码?
如何在Spring Boot中设计API返回码?
448 0
如何在Spring Boot中设计API返回码?
|
传感器 机器学习/深度学习 编解码
领域最全!多传感器融合方法综述!(Camera/Lidar/Radar等多源异构数据)(下)
自动驾驶正成为影响未来行业的关键技术,传感器是自动驾驶系统中感知外部世界的关键,其协作性能直接决定自动驾驶车辆的安全性。本文主要讨论了近年来自动驾驶中多传感器融合的不同策略。分析了常规传感器的性能和多传感器融合的必要性,包括radar、激光雷达、摄像机、超声波、GPS、IMU和V2X。
领域最全!多传感器融合方法综述!(Camera/Lidar/Radar等多源异构数据)(下)
|
搜索推荐 小程序 NoSQL
如何设计 API 返回码(错误码)?
如何设计 API 返回码(错误码)?
824 0
如何设计 API 返回码(错误码)?
|
10天前
|
缓存 测试技术 API
Qwen 3.7 Plus 与 Max 实测:性价比与多模态能力差异解析(2026)
2026 年 6 月 1 日,阿里悄无声息地发布了 Qwen 3.7 Plus,距 Qwen 3.7 Max 上线刚好 11 天。同样的 1M 上下文,同样的 35 小时自治上限。但价格才是头条:Plus 是 0.40/M输入,Max是 2.50/M——便宜约 6 倍——并且还能看图、看视频。Vision Arena 上 Plus 已经排到 #16。所以这周真正值得讨论的问题不是”要不要为视觉能力买单”,而是”Max 凭什么用 6 倍价格换来 2 个百分点的 benchmark 领先”。