Detailed description of WeChat mini-app province/city selector instances

Source: Internet
Author: User
This article mainly introduces detailed information about the small program provincial and municipal selector instances. the title bar is highlighted by the gesture sliding switch between regions, and vice versa. the currently selected title is highlighted in red; return to the previous level and click a region to clear the child region. For more information, see the sub-program provincial and municipal selector:

I recently learned a small program. in order to test my learning performance, I made a small example and searched for similar instances online. I found this is better. let's take a look.

1. move between regions and highlight the title bar accordingly.

The idea is: use the current to determine the highlight style.

1. monitor the position where swiper rolls:

 
  
CurrentChanged: function (e) {// swiper scroll to make the current value passively changed, used to highlight var current = e. detail. current; this. setData ({current: current });}
 

2. check whether the highlighted style is added to the layout file. area-selected is the target style and color: red;

       
  
   {{provinceName}}
        
  
   {{provinceName}}
      
     
       
  
   {{cityName}}
        
  
   {{cityName}}
      
     
       
  
   {{regionName}}
        
  
   {{regionName}}
      
     
       
  
   {{townName}}
        
  
   {{townName}}
      
 

3. click "select" for the next level at the upper level.

ProvinceTapped: function (e) {... that. setData ({cityName: 'Select ', city: array, cityObjects: area });...}

Other levels and so on.

The effect is as follows:

2. click switch in the title bar to switch between regions.

ChangeCurrent: function (e) {// record the region level var current = e. currentTarget. dataset. current; this. setData ({current: current}) where the title of the click is located });}

Title bar Bind a click event on

 

The effect is as follows:

Data binding is really cool to use, and a few words of code completes the click switch. if other platforms write the same function, it will take half a day.

3. after you click an area at the previous level, you need to automatically clear the array, index, name, and array at the next level. Otherwise, the logic is disordered.

For example, after selecting Beijing, Chaoyang district, and the Third Ring Road in sequence, the province is back to Zhejiang province. at this time, the second-level and third-level areas are still within the originally selected Chaoyang district and third ring road, the content of the left and right sliding areas is also displayed incorrectly.

To solve this bug, you need to handle the tapped click event again and clear the sub-level selection.

ProvinceTapped: function (e) {// identifies the currently clicked province, recording that its name and primary key id depend on it var index = e. currentTarget. dataset. index; // current is 1, so that the page slides to the municipal list from the left. // provinceIndex is the identifier of the urban data. this. setData ({current: 2, cityIndex: index, regionIndex:-1, townIndex:-1, cityName: this. data. city [index], regionName: '', townName:'', region: [], town: []});...}

4. fixed the bug of providing four swiper-items upon initialization.

The processing method is to add an array to check whether the number of elements is zero. for example, a city is displayed only when it has a value.

          
             
                
                   
     
      {{item}}
                    
     
      {{item}}
                  
               
            
         
 

Generate it in the js file accordingly

This. setData ({-current: 2, cityIndex: index, regionIndex:-1, townIndex:-1, this. setData ({region: array, regionObjects: area}); + // make sure that the array data is generated and swiper + that is moved. setData ({+ current: 2 + });});

Note that the current setData operation is moved under region and regionObject to ensure that there is a value first and then the swiper displacement is controlled.

The last gif image compared with the native picker:

There is no need to repeatedly click and select on picker, and there will be no cross-level click issues. Is there a better experience?

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more details about small programs, province, and city selector instances, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.