Home WeChat Applet Mini Program Development How does the applet implement and call the region selector?

How does the applet implement and call the region selector?

Sep 15, 2018 am 10:47 AM
Applets

The content of this article is about how to implement and call the region selector in the applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

region-widget.js

var api = require('../../../utils/api/index.js');var constants = require('../../../utils/api/lib/constants');var raw = require('../../../utils/citys');

Component({
  options: {
    multipleSlots: true
  },
  data: {
    select:[0,0],
    region: [Object.keys(raw), raw[Object.keys(raw)[0]]],
    province:"",
    city:""
  },
  properties: {
    target: {
      type: Array,
      value: [],
      observer: 'update'
    }
  },
  methods: {
    update: function (newVal, oldVal) {
      if(!newVal || newVal.length < 2)return;      var province = newVal[0];      var city = newVal[1];      var region = this.data.region;      //update province
      var pro_index = region[0].indexOf(province);      if (pro_index < 0)return;
      region = [region[0], raw[province]];      //update city
      var city_index = region[1].indexOf(city);      if (city_index < 0) return;      this.setData({
        select: [pro_index,city_index],
        region: region,
        province: province,
        city: city
      })
    },
    bindChange: function (e) {
      if (!e.detail.value || e.detail.value.length <2)return;      var region = this.data.region;      var pro_index = e.detail.value[0];      var city_index = e.detail.value[1];      this.setData({
        province: region[0][pro_index],
        city: region[1][city_index]
      })      this.notify();
    },
    bindColumnChange: function (e) {
      if (e.detail.column === 0) {        var region = this.data.region;        var province = region[0][e.detail.value];        this.setData({ 
          region: [region[0], raw[province]] 
        });
      }
    },
    notify() {      this.triggerEvent(&#39;regionChange&#39;, { province: this.data.province, city: this.data.city }, {})
    }
  }
})
Copy after login

region-widget.wxml

<picker 
  mode="multiSelector" 
  value="{{select}}" 
  range="{{region}}"
  bindchange="bindChange" 
  bindcolumnchange="bindColumnChange">
  <slot></slot></picker>
Copy after login

city.js

var citys = {  &#39;北京市&#39;: [&#39;市辖区&#39;],  &#39;天津市&#39;: [&#39;市辖区&#39;],
  &#39;河北省&#39;: [&#39;石家庄市&#39;, &#39;唐山市&#39;, &#39;秦皇岛市&#39;, &#39;邯郸市&#39;, &#39;邢台市&#39;, &#39;保定市&#39;, &#39;张家口市&#39;, &#39;承德市&#39;, &#39;沧州市&#39;, 
          &#39;廊坊市&#39;, &#39;衡水市&#39;, &#39;省直辖县级行政区划&#39;], 
 &#39;山西省&#39;: [&#39;太原市&#39;, &#39;大同市&#39;, &#39;阳泉市&#39;, &#39;长治市&#39;, &#39;晋城市&#39;, &#39;朔州市&#39;, &#39;晋中市&#39;,
           &#39;运城市&#39;, &#39;忻州市&#39;, &#39;临汾市&#39;, &#39;吕梁市&#39;],
 &#39;内蒙古自治区&#39;: [&#39;呼和浩特市&#39;, &#39;包头市&#39;, &#39;乌海市&#39;, &#39;赤峰市&#39;, &#39;通辽市&#39;, &#39;鄂尔多斯市&#39;, &#39;呼伦贝尔市&#39;,
                &#39;巴彦淖尔市&#39;, &#39;乌兰察布市&#39;, &#39;兴安盟&#39;, &#39;锡林郭勒盟&#39;, &#39;阿拉善盟&#39;],  
 &#39;辽宁省&#39;: [&#39;沈阳市&#39;, &#39;大连市&#39;, &#39;鞍山市&#39;, &#39;抚顺市&#39;, &#39;本溪市&#39;, &#39;丹东市&#39;, &#39;锦州市&#39;, &#39;营口市&#39;, &#39;阜新市&#39;, &#39;辽阳市&#39;,
             &#39;盘锦市&#39;, &#39;铁岭市&#39;, &#39;朝阳市&#39;, &#39;葫芦岛市&#39;],  
  &#39;吉林省&#39;: [&#39;长春市&#39;, &#39;吉林市&#39;, &#39;四平市&#39;, &#39;辽源市&#39;, &#39;通化市&#39;, &#39;白山市&#39;, &#39;松原市&#39;, &#39;白城市&#39;, &#39;延边朝鲜族自治州&#39;], 
 &#39;黑龙江省&#39;: [&#39;哈尔滨市&#39;, &#39;齐齐哈尔市&#39;, &#39;鸡西市&#39;, &#39;鹤岗市&#39;, &#39;双鸭山市&#39;, &#39;大庆市&#39;, &#39;伊春市&#39;, &#39;佳木斯市&#39;,
              &#39;七台河市&#39;, &#39;牡丹江市&#39;, &#39;黑河市&#39;, &#39;绥化市&#39;, &#39;大兴安岭地区&#39;], 
 &#39;上海市&#39;: [&#39;市辖区&#39;],  
 &#39;江苏省&#39;: [&#39;南京市&#39;, &#39;无锡市&#39;, &#39;徐州市&#39;, &#39;常州市&#39;, &#39;苏州市&#39;, &#39;南通市&#39;, &#39;连云港市&#39;, &#39;淮安市&#39;, &#39;盐城市&#39;, &#39;扬州市&#39;, 
           &#39;镇江市&#39;, &#39;泰州市&#39;, &#39;宿迁市&#39;],  
 &#39;浙江省&#39;: [&#39;杭州市&#39;, &#39;宁波市&#39;, &#39;温州市&#39;, &#39;嘉兴市&#39;, &#39;湖州市&#39;, &#39;绍兴市&#39;, &#39;金华市&#39;, &#39;衢州市&#39;, &#39;舟山市&#39;, &#39;台州市&#39;, 
           &#39;丽水市&#39;], 
 &#39;安徽省&#39;: [&#39;合肥市&#39;, &#39;芜湖市&#39;, &#39;蚌埠市&#39;, &#39;淮南市&#39;, &#39;马鞍山市&#39;, &#39;淮北市&#39;, &#39;铜陵市&#39;, &#39;安庆市&#39;, &#39;黄山市&#39;,
            &#39;滁州市&#39;, &#39;阜阳市&#39;, &#39;宿州市&#39;, &#39;六安市&#39;, &#39;亳州市&#39;, &#39;池州市&#39;, &#39;宣城市&#39;], 
 &#39;福建省&#39;: [&#39;福州市&#39;, &#39;厦门市&#39;, &#39;莆田市&#39;, &#39;三明市&#39;, &#39;泉州市&#39;, &#39;漳州市&#39;, &#39;南平市&#39;, &#39;龙岩市&#39;, &#39;宁德市&#39;],  
 &#39;江西省&#39;: [&#39;南昌市&#39;, &#39;景德镇市&#39;, &#39;萍乡市&#39;, &#39;九江市&#39;, &#39;新余市&#39;, &#39;鹰潭市&#39;, &#39;赣州市&#39;, &#39;吉安市&#39;, &#39;宜春市&#39;, &#39;抚州市&#39;,
            &#39;上饶市&#39;], 
   &#39;山东省&#39;: [&#39;济南市&#39;, &#39;青岛市&#39;, &#39;淄博市&#39;, &#39;枣庄市&#39;, &#39;东营市&#39;, &#39;烟台市&#39;, &#39;潍坊市&#39;, &#39;济宁市&#39;,
                 &#39;泰安市&#39;, &#39;威海市&#39;, &#39;日照市&#39;, &#39;莱芜市&#39;, &#39;临沂市&#39;, &#39;德州市&#39;, &#39;聊城市&#39;, &#39;滨州市&#39;, &#39;菏泽市&#39;], 
   &#39;河南省&#39;: [&#39;郑州市&#39;, &#39;开封市&#39;, &#39;洛阳市&#39;, &#39;平顶山市&#39;, &#39;安阳市&#39;, &#39;鹤壁市&#39;, &#39;新乡市&#39;, &#39;焦作市&#39;, &#39;濮阳市&#39;,
              &#39;许昌市&#39;, &#39;漯河市&#39;, &#39;三门峡市&#39;, &#39;南阳市&#39;, &#39;商丘市&#39;, &#39;信阳市&#39;, &#39;周口市&#39;, &#39;驻马店市&#39;, &#39;省直辖县级行政区划&#39;], 
   &#39;湖北省&#39;: [&#39;武汉市&#39;, &#39;黄石市&#39;, &#39;十堰市&#39;, &#39;宜昌市&#39;, &#39;襄阳市&#39;, &#39;鄂州市&#39;, &#39;荆门市&#39;, &#39;孝感市&#39;, &#39;荆州市&#39;, &#39;黄冈市&#39;,
             &#39;咸宁市&#39;, &#39;随州市&#39;, &#39;恩施土家族苗族自治州&#39;, &#39;省直辖县级行政区划&#39;],  
    &#39;湖南省&#39;: [&#39;长沙市&#39;, &#39;株洲市&#39;, &#39;湘潭市&#39;, &#39;衡阳市&#39;, &#39;邵阳市&#39;, &#39;岳阳市&#39;, &#39;常德市&#39;, &#39;张家界市&#39;, &#39;益阳市&#39;, &#39;郴州市&#39;,
               &#39;永州市&#39;, &#39;怀化市&#39;, &#39;娄底市&#39;, &#39;湘西土家族苗族自治州&#39;],  
    &#39;广东省&#39;: [&#39;广州市&#39;, &#39;韶关市&#39;, &#39;深圳市&#39;, &#39;珠海市&#39;, &#39;汕头市&#39;, &#39;佛山市&#39;, &#39;江门市&#39;, &#39;湛江市&#39;, &#39;茂名市&#39;, &#39;肇庆市&#39;,
              &#39;惠州市&#39;, &#39;梅州市&#39;, &#39;汕尾市&#39;, &#39;河源市&#39;, &#39;阳江市&#39;, &#39;清远市&#39;, &#39;东莞市&#39;, &#39;中山市&#39;, &#39;潮州市&#39;, &#39;揭阳市&#39;,
               &#39;云浮市&#39;], 
   &#39;广西壮族自治区&#39;: [&#39;南宁市&#39;, &#39;柳州市&#39;, &#39;桂林市&#39;, &#39;梧州市&#39;, &#39;北海市&#39;, &#39;防城港市&#39;, &#39;钦州市&#39;, &#39;贵港市&#39;, &#39;玉林市&#39;,
             &#39;百色市&#39;, &#39;贺州市&#39;, &#39;河池市&#39;, &#39;来宾市&#39;, &#39;崇左市&#39;], 
  &#39;海南省&#39;: [&#39;海口市&#39;, &#39;三亚市&#39;, &#39;三沙市&#39;, &#39;儋州市&#39;, &#39;省直辖县级行政区划&#39;],  
  &#39;重庆市&#39;: [&#39;市辖区&#39;, &#39;县&#39;],  
  &#39;四川省&#39;: [&#39;成都市&#39;, &#39;自贡市&#39;, &#39;攀枝花市&#39;, &#39;泸州市&#39;, &#39;德阳市&#39;, &#39;绵阳市&#39;, &#39;广元市&#39;, &#39;遂宁市&#39;, &#39;内江市&#39;, &#39;乐山市&#39;,
               &#39;南充市&#39;, &#39;眉山市&#39;, &#39;宜宾市&#39;, &#39;广安市&#39;, &#39;达州市&#39;, &#39;雅安市&#39;, &#39;巴中市&#39;, &#39;资阳市&#39;, &#39;阿坝藏族羌族自治州&#39;, 
               &#39;甘孜藏族自治州&#39;, &#39;凉山彝族自治州&#39;],  
    &#39;贵州省&#39;: [&#39;贵阳市&#39;, &#39;六盘水市&#39;, &#39;遵义市&#39;, &#39;安顺市&#39;, &#39;毕节市&#39;, &#39;铜仁市&#39;, &#39;黔西南布依族苗族自治州&#39;,
             &#39;黔东南苗族侗族自治州&#39;, &#39;黔南布依族苗族自治州&#39;], 
   &#39;云南省&#39;: [&#39;昆明市&#39;, &#39;曲靖市&#39;, &#39;玉溪市&#39;, &#39;保山市&#39;, &#39;昭通市&#39;, &#39;丽江市&#39;, &#39;普洱市&#39;, &#39;临沧市&#39;, &#39;楚雄彝族自治州&#39;,
             &#39;红河哈尼族彝族自治州&#39;, &#39;文山壮族苗族自治州&#39;, &#39;西双版纳傣族自治州&#39;, &#39;大理白族自治州&#39;, 
             &#39;德宏傣族景颇族自治州&#39;, &#39;怒江傈僳族自治州&#39;, &#39;迪庆藏族自治州&#39;],  
  &#39;西藏自治区&#39;: [&#39;拉萨市&#39;, &#39;日喀则市&#39;, &#39;昌都市&#39;, &#39;林芝市&#39;, &#39;山南市&#39;, &#39;那曲地区&#39;, &#39;阿里地区&#39;], 
   &#39;陕西省&#39;: [&#39;西安市&#39;, &#39;铜川市&#39;, &#39;宝鸡市&#39;, &#39;咸阳市&#39;, &#39;渭南市&#39;, &#39;延安市&#39;, &#39;汉中市&#39;, &#39;榆林市&#39;, &#39;安康市&#39;, &#39;商洛市&#39;],
  &#39;甘肃省&#39;: [&#39;兰州市&#39;, &#39;嘉峪关市&#39;, &#39;金昌市&#39;, &#39;白银市&#39;, &#39;天水市&#39;, &#39;武威市&#39;, &#39;张掖市&#39;, &#39;平凉市&#39;, &#39;酒泉市&#39;, &#39;庆阳市&#39;, 
              &#39;定西市&#39;, &#39;陇南市&#39;, &#39;临夏回族自治州&#39;, &#39;甘南藏族自治州&#39;], 
   &#39;青海省&#39;: [&#39;西宁市&#39;, &#39;海东市&#39;, &#39;海北藏族自治州&#39;, &#39;黄南藏族自治州&#39;, &#39;海南藏族自治州&#39;, &#39;果洛藏族自治州&#39;,
             &#39;玉树藏族自治州&#39;, &#39;海西蒙古族藏族自治州&#39;],  
    &#39;宁夏回族自治区&#39;: [&#39;银川市&#39;, &#39;石嘴山市&#39;, &#39;吴忠市&#39;, &#39;固原市&#39;, &#39;中卫市&#39;], 
  &#39;新疆维吾尔自治区&#39;: [&#39;乌鲁木齐市&#39;, &#39;克拉玛依市&#39;, &#39;吐鲁番市&#39;, &#39;哈密市&#39;, &#39;昌吉回族自治州&#39;,
                   &#39;博尔塔拉蒙古自治州&#39;, &#39;巴音郭楞蒙古自治州&#39;, &#39;阿克苏地区&#39;, &#39;克孜勒苏柯尔克孜自治州&#39;,
                    &#39;喀什地区&#39;, &#39;和田地区&#39;, &#39;伊犁哈萨克自治州&#39;, &#39;塔城地区&#39;, &#39;阿勒泰地区&#39;, &#39;自治区直辖县级行政区划&#39;], 
     &#39;台湾省&#39;: [&#39;台湾&#39;], 
      &#39;香港特别行政区&#39;: [&#39;香港&#39;], 
       &#39;澳门特别行政区&#39;: [&#39;澳门&#39;]
}module.exports = citys;
Copy after login

Use it like this:

<region-widget target="{{select}}" bindregionChange="onRegionChange">
    <text>{{select[0]}}{{select[1]}}</text></region-widget>
Copy after login
Page({
  data: {
      select:["",""]
  },
  onRegionChange:function(e){
    console.log(e.detail);
    this.setData({select: [e.detail.province, e.detail.city]})
  }  ...}
Copy after login

The above is the detailed content of How does the applet implement and call the region selector?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Develop WeChat applet using Python Develop WeChat applet using Python Jun 17, 2023 pm 06:34 PM

With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

Can small programs use react? Can small programs use react? Dec 29, 2022 am 11:06 AM

Mini programs can use react. How to use it: 1. Implement a renderer based on "react-reconciler" and generate a DSL; 2. Create a mini program component to parse and render DSL; 3. Install npm and execute the developer Build npm in the tool; 4. Introduce the package into your own page, and then use the API to complete the development.

Implement card flipping effects in WeChat mini programs Implement card flipping effects in WeChat mini programs Nov 21, 2023 am 10:55 AM

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: &lt;!--index.wxml--&gt;&l

Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Oct 31, 2023 pm 09:25 PM

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

How uniapp achieves rapid conversion between mini programs and H5 How uniapp achieves rapid conversion between mini programs and H5 Oct 20, 2023 pm 02:12 PM

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

Tutorial on writing a simple chat program in Python Tutorial on writing a simple chat program in Python May 08, 2023 pm 06:37 PM

Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

How to get membership in WeChat mini program How to get membership in WeChat mini program May 07, 2024 am 10:24 AM

1. Open the WeChat mini program and enter the corresponding mini program page. 2. Find the member-related entrance on the mini program page. Usually the member entrance is in the bottom navigation bar or personal center. 3. Click the membership portal to enter the membership application page. 4. On the membership application page, fill in relevant information, such as mobile phone number, name, etc. After completing the information, submit the application. 5. The mini program will review the membership application. After passing the review, the user can become a member of the WeChat mini program. 6. As a member, users will enjoy more membership rights, such as points, coupons, member-exclusive activities, etc.

How to operate mini program registration How to operate mini program registration Sep 13, 2023 pm 04:36 PM

Mini program registration operation steps: 1. Prepare copies of personal ID cards, corporate business licenses, legal person ID cards and other filing materials; 2. Log in to the mini program management background; 3. Enter the mini program settings page; 4. Select " "Basic Settings"; 5. Fill in the filing information; 6. Upload the filing materials; 7. Submit the filing application; 8. Wait for the review results. If the filing is not passed, make modifications based on the reasons and resubmit the filing application; 9. The follow-up operations for the filing are Can.

See all articles