WeChat applet picker multi-column selector (mode = multiSelector)

WeChat applet picker multi-column selector (mode = multiSelector)

vue-next-admin, based on vue3.x + CompositionAPI + typescript + vite + element plus + vue-router-next + next.vuex, is an open source free template library for mobile phones, tablets and PCs

1. Effect diagram (multiple columns)

insert image description here

2. Normal selector: mode = selector, multi-column selector: mode = multiSelector

Document address: WeChat development document picker選擇器

  • Normal selector: mode = selector,一維數組:array: ['美國', '中國', '巴西', '日本']
  • Multi-column selector: mode = multiSelector,二維數組:multiArray: [['無脊柱動物', '脊柱動物'], ['扁性動物', '線形動物', '環節動物', '軟體動物', '節肢動物'], ['豬肉絳蟲', '吸血蟲']]

The format is incorrect and needs to be processed into the corresponding array format. The following is a pit diagram:

insert image description here

3. app.json

{
  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/picker/picker"
  ],
  "entryPagePath": "pages/picker/picker",
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle": "black"
  },
  "sitemapLocation": "sitemap.json",
  "usingComponents": {
    "van-button": "@vant/weapp/button/index",
    "van-area": ​​"@vant/weapp/area/index"
  }
}

4. picker.wxml

<!--pages/picker/picker.wxml-->
<view>
  <view class="section__title">Multi-column selector</view>
  <picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange"
    value="{{multiIndex}}" range="{{newArr}}">
    <view class="picker">
      Current selection: <van-button type="primary">
        {{newArr[0][multiIndex[0]]}}, {{newArr[1][multiIndex[1]]}}, {{newArr[2][multiIndex[2]]}}</van-button>
    </view>
  </picker>
</view>

5. picker.js

多列選擇器:mode = multiSelector

Pay attention to the array format: multiArray數組has children , to process a two-dimensional array:

// pages/picker/picker.js
Page({
  /**
   * Initial data of the page */
  data: {
    multiArray: [{
        id: 1,
        label: "Southeast",
        children: [{
            id: 2,
            label: "Shanghai",
            children: [{
                id: 3,
                label: "Putuo",
              },
              {
                id: 4,
                label: "Huangpu",
              },
              {
                id: 5,
                label: "Xuhui",
              },
            ],
          },
          {
            id: 7,
            label: "Jiangsu",
            children: [{
                id: 8,
                label: "Nanjing",
              },
              {
                id: 9,
                label: "Suzhou",
              },
              {
                id: 10,
                label: "Wuxi",
              },
            ],
          },
          {
            id: 12,
            label: "Zhejiang",
            children: [{
                id: 13,
                label: "Hangzhou",
              },
              {
                id: 14,
                label: "Ningbo",
              },
              {
                id: 15,
                label: "Jiaxing",
              },
            ],
          },
        ],
      },
      {
        id: 17,
        label: "Northwest",
        children: [{
            id: 18,
            label: "Shaanxi",
            children: [{
                id: 19,
                label: "Xi'an",
              },
              {
                id: 20,
                label: "Yan'an",
              },
            ],
          },
          {
            id: 21,
            label: "Xinjiang Uyghur Autonomous Region",
            children: [{
                id: 22,
                label: "Urumqi",
              },
              {
                id: 23,
                label: "Karamay",
              },
            ],
          },
        ],
      },
    ],
    multiIndex: [0, 0, 0],
    multiIds: [],
    newArr: [],
  },

  bindMultiPickerChange(e) {
    console.log(this.data.multiIds);
  },
  bindMultiPickerColumnChange(e) {
    let data = {
      newArr: this.data.newArr,
      multiIndex: this.data.multiIndex,
      multiIds: this.data.multiIds,
    };
    data.multiIndex[e.detail.column] = e.detail.value;

    let searchColumn = () => {
      let arr1 = [];
      let arr2 = [];
      this.data.multiArray.map((v, vk) => {
        if (data.multiIndex[0] === vk) {
          data.multiIds[0] = {
            ...v,
          };
          v.children.map((c, ck) => {
            arr1.push(c.label);
            if (data.multiIndex[1] === ck) {
              data.multiIds[1] = {
                ...c,
              };
              c.children.map((t, vt) => {
                arr2.push(t.label);
                if (data.multiIndex[2] === vt) {
                  data.multiIds[2] = {
                    ...t,
                  };
                }
              });
            }
          });
        }
      });
      data.newArr[1] = arr1;
      data.newArr[2] = arr2;
    };
    switch (e.detail.column) {
      case 0:
        // Restore the initial value each time you switch data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        //Execute function processing searchColumn();
        break;
      case 1:
        data.multiIndex[2] = 0;
        searchColumn();
        break;
    }
    this.setData(data);
  },

  /**
   * Life cycle function--listen for page loading*/
  onLoad: function (options) {
    let state = {
      arr: [],
      arr1: [],
      arr2: [],
      arr3: [],
      multiIds: []
    }
    this.data.multiArray.map((v, vk) => {
      state.arr1.push(v.label);
      if (this.data.multiIndex[0] === vk) {
        state.multiIds[0] = v;
      }
      if (state.arr2.length <= 0) {
        v.children.map((c, ck) => {
          state.arr2.push(c.label);
          if (this.data.multiIndex[1] === ck) {
            state.multiIds[1] = c;
          }
          if (state.arr3.length <= 0) {
            c.children.map((t, tk) => {
              state.arr3.push(t.label);
              if (this.data.multiIndex[2] === tk) {
                state.multiIds[2] = t;
              }
            });
          }
        });
      }
    });
    state.arr[0] = state.arr1;
    state.arr[1] = state.arr2;
    state.arr[2] = state.arr3;
    this.setData({
      newArr: state.arr,
      multiIds: state.multiIds,
    });
  },

  /**
   * Life cycle function - listen for the completion of the initial rendering of the page*/
  onReady: function () {},

  /**
   * Life cycle function--monitor page display*/
  onShow: function () {},

  /**
   * Life cycle function--listen for page hiding*/
  onHide: function () {},

  /**
   * Life cycle function--monitor page uninstallation*/
  onUnload: function () {},

  /**
   * Page related event processing function - listen to user pull-down action */
  onPullDownRefresh: function () {},

  /**
   * The function that handles the bottoming event on the page*/
  onReachBottom: function () {},

  /**
   * User clicks on the upper right corner to share*/
  onShareAppMessage: function () {},
});

This is the end of this article about WeChat mini program picker multi-column selector (mode = multiSelector). For more relevant mini program picker multi-column selector content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet implements custom picker selector pop-up content
  • WeChat applet picker date and time selector

<<:  Why not use UTF-8 encoding in MySQL?

>>:  Linux installation steps for Jenkins and various problem solving (page access initialization password)

Recommend

The difference between Input's size and maxlength attributes

I recently used the input size and maxlength attri...

Pitfalls and solutions for upgrading MySQL 5.7.23 in CentOS 7

Preface Recently, I found a pitfall in upgrading ...

Mysql database scheduled backup script sharing

BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin...

Methods for defragmenting and reclaiming space in MySQL tables

Table of contents Causes of MySQL Table Fragmenta...

Detailed explanation of Nginx proxy_redirect usage

Today, I encountered a little problem when I was ...

A brief discussion on the Linux kernel's support for floating-point operations

Currently, most CPUs support floating-point units...

MySQL common statements for viewing transactions and locks

Some common statements for viewing transactions a...

HTML set as homepage and add to favorites_Powernode Java Academy

How to implement the "Set as homepage" ...

JS deep and shallow copy details

Table of contents 1. What does shallow copy mean?...

How to create a new user in CentOS and enable key login

Table of contents Create a new user Authorize new...

Install Zookeeper under Docker (standalone and cluster)

After starting Docker, let's take a look at t...