微信小程序省市區選擇器的詳細闡述

一、微信小程序城市區縣選擇

微信小程序提供了城市區縣選擇組件,是對於用戶選擇地址時的一種簡單方便的解決方案。在使用之前,需要在頁面的.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/zh-hant/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

發表回復

登錄後才能評論