微信小程序省市区选择器的详细阐述

一、微信小程序城市区县选择

微信小程序提供了城市区县选择组件,是对于用户选择地址时的一种简单方便的解决方案。在使用之前,需要在页面的.json文件中进行组件的注册。

"usingComponents":{
    "van-area":"../../components/vant/area/area"
}

而在对城市区县选择器的使用中,要在.wxml文件中创建一个组件标签,用于后续与JS代码进行交互。同时,也可以根据官方文档中提供的API方法,定制自己想要的功能,比如自定义模板,定制颜色。


在JS文件中,需要对组件进行初始化并且提供一些函数供用户实现交互效果。初始化时,需要提供相关参数并且声明每个参数所对应的含义,同时,还需要在函数中实现逻辑交互,对用户的选择进行处理,并对页面进行渲染。

data: {
    areaValue: [],
    show: false,
    title: "",
},
onConfirm(event) {
    console.log(event.detail.values);
    this.setData({
        areaValue: event.detail.values,
        show: false,
        title: "",
    });
},
onCancel() {
    this.setData({
        show: false,
        title: "",
    });
},
onChange(event) {
    console.log(event.detail);
},
onDetailToggle(event) {
    console.log(event.detail);
},
onHide() {
    console.log("onHide");
},
showAreaPopup() {
    this.setData({ show: true, title: "城市区县选择" });
},

二、微信小程序省市区街道选择

微信小程序省市区街道选择器是微信官方提供的一个组件,同样需要在页面.wxml文件中进行注册并创建组件标签。在使用之前,需要对组件提供一些参数的配置,比如显示方式,高亮颜色等等。


    
        
            {{region[value].label}}
            
        
    

对于使用该组件的开发人员而言,需要实现在页面中声明一个数组用于存储省市区信息,并且在需要使用到该组件的地方,调用相关函数对省市区信息进行处理并将信息渲染到页面中。

data: {
    value: [],
    region: [],
    mode: "region",
},
onChange(event) {
    console.log(event.detail);
    this.setData({ value: event.detail.value });
},
onLoad() {
    const that = this;
    wx.request({
        url: "http://japi.zto.cn/region",
        data: {
            messageType: "province",
            companyId: "3",
        },
        header: {
            "content-type": "application/json", // 默认值
        },
        success(res) {
            console.log(res.data);
            const tmp = [];
            for (let i = 0; i < res.data.result.length; i++) {
                tmp.push({
                    label: res.data.result[i].regionName,
                    value: res.data.result[i].regionCode,
                    children: [],
                });
            }
            console.log(tmp);
            that.setData({ region: tmp });
        },
    });
},

三、小程序省市区选择器

小程序省市区选择器是对于微信小程序城市区县选择器和微信小程序省市区街道选择器的完美结合,提供了更为便利和完善的地址选择方案。在使用该组件前,同样需要在页面.json文件中进行注册,同时,在.wxml文件中创建组件标签,并指定组件应该承担的功能。


    
        
            
                
                    
                        {{areaShow}}
                    
                    
                
            
        
    

对于该组件的使用,同样需要提供一些参数进行初始化,并在交互期间进行数据处理,对结果进行渲染。可以通过该组件,实现对于用户的地址选择方案的完善。

data: {
    areaShow: "省市区选择器",
    areaCodeArr: [0, 0, 0],
    areaList: [],
},
onLoad: function (options) {
    this.getProvinces()
        .then((res) => {
            this.areaList.push(res);
            return this.getCities(res[0].code);
        })
        .then((res) => {
            this.areaList.push(res);
            return this.getDistricts(res[0].code);
        })
        .then((res) => {
            this.areaList.push(res);
            this.getAreaShow();
        });
},
columnChange: function (e) {
    if (e.detail.column === 0) {
        let code = this.areaList[e.detail.column][e.detail.value].code;
        this.getCities(code).then(
            (res) => {
                this.areaList[1] = res;
                let code = this.areaList[e.detail.column][0].code;
                this.getDistricts(code).then((res) => {
                    this.areaList[2] = res;
                    this.getAreaShow(0);
                });
            },
            (err) => console.log(err)
        );
    } else if (e.detail.column === 1) {
        let code = this.areaList[e.detail.column][e.detail.value].code;
        this.getDistricts(code).then((res) => {
            this.areaList[2] = res;
            this.getAreaShow(0);
        });
    }
},
areaChange: function (e) {
    let value = e.detail.value;
    this.areaCodeArr = value;
    this.getAreaShow();
},
getProvinces: function () {
    let _this = this;
    return new Promise((resolve, reject) => {
        wx.request({
            url: "http://jiefangsudi.com/thanks/province/list",
            success: function (res) {
                let provinces = res.data;
                for (var i = 0; i  {
        wx.request({
            url: "http://jiefangsudi.com/thanks/city/list?provinceCode=" + parentCode,
            success: function (res) {
                let cities = res.data;
                for (var i = 0; i  {
        wx.request({
            url: "http://jiefangsudi.com/thanks/area/list?cityCode=" + parentCode,
            success: function (res) {
                let districts = res.data;
                for (var i = 0; i  {
        let areaCodeArr = _this.areaCodeArr;
        let provinces = _this.areaList[0].children;
        let cities = _this.areaList[1].children;
        let districts = _this.areaList[2].children;
        let areaShow = "";
        if (provinces[areaCodeArr[0]]) {
            areaShow += provinces[areaCodeArr[0]].name;
        }
        if (cities[areaCodeArr[1]]) {
            areaShow += "-" + cities[areaCodeArr[1]].name;
        }
        if (districts[areaCodeArr[2]]) {
            areaShow += "-" + districts[areaCodeArr[2]].name;
        }
        _this.areaShow = areaShow;
    }, delayTime);
},

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/238028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:09
下一篇 2024-12-12 12:09

相关推荐

  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29
  • 爬虫是一种程序

    爬虫是一种程序,用于自动获取互联网上的信息。本文将从如下多个方面对爬虫的意义、运行方式、应用场景和技术要点等进行详细的阐述。 一、爬虫的意义 1、获取信息:爬虫可以自动获取互联网上…

    编程 2025-04-29
  • Vb运行程序的三种方法

    VB是一种非常实用的编程工具,它可以被用于开发各种不同的应用程序,从简单的计算器到更复杂的商业软件。在VB中,有许多不同的方法可以运行程序,包括编译器、发布程序以及命令行。在本文中…

    编程 2025-04-29
  • Python一元二次方程求解程序

    本文将详细阐述Python一元二次方程求解程序的相关知识,为读者提供全面的程序设计思路和操作方法。 一、方程求解 首先,我们需要了解一元二次方程的求解方法。一元二次方程可以写作: …

    编程 2025-04-29
  • 如何使用GPU加速运行Python程序——以CSDN为中心

    GPU的强大性能是众所周知的。而随着深度学习和机器学习的发展,越来越多的Python开发者将GPU应用于深度学习模型的训练过程中,提高了模型训练效率。在本文中,我们将介绍如何使用G…

    编程 2025-04-29
  • Web程序和桌面程序的区别

    Web程序和桌面程序都是进行软件开发的方式,但是它们之间存在很大的区别。本文将从多角度进行阐述。 一、运行方式 Web程序运行于互联网上,用户可以通过使用浏览器来访问它。而桌面程序…

    编程 2025-04-29
  • 改善Python程序的90个建议pdf网盘

    本文将从多个方面对改善Python程序的90个建议pdf网盘进行详细阐述,帮助Python开发者提高程序的性能和效率。 一、代码优化 1、使用map函数或列表推导式代替for循环。…

    编程 2025-04-29

发表回复

登录后才能评论