Home  >  Article  >  WeChat Applet  >  How to use the WeChat mini program drop-down box component

How to use the WeChat mini program drop-down box component

云罗郡主
云罗郡主Original
2019-01-21 13:44:274880browse

This article mainly introduces the use of the drop-down box component of the WeChat applet in detail. It is written in a very comprehensive and detailed manner and has a certain reference value. Friends in need can refer to it for learning. Down. If there are any deficiencies, criticisms and corrections are welcome. [Recommended tutorial: 小program development tutorial]

Applicable scenarios

1. Three-level linkage between provinces and cities
2. Date of birth selection
3.Gender Selection
4, general drop-down selection, etc.

1. Provincial and municipal three-level linkage use

Note mode = region, and value = "One-dimensional array ”

//.wxml
<picker mode="region" bindchange="bindViewEvent" data-model="component"
data-method="bindSelect" data-name="region" value="{{region}}" custom-item="{{customItem}}">
<view class="picker">
   当前选择:{{region[0]}},{{region[1]}},{{region[2]}}
</view>
</picker>

2. Birth date selection

Note mode = date, and value = “date string”

<picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindViewEvent" data-model="component" data-method="bindSelect" date-mode="date" data-name="date">
  <view class="picker">
  {{date}}
  </view>

3. Gender selection

<picker bindchange="bindViewEvent" data-model="component" data-method="bindSelect" data-name="index" value=&#39;{{index}}&#39; range="{{gender}}">
  <view class="label-right">
    {{gender[index]}}
  </view>
</picker>

All of the above need to set relevant initial variables in .js!

var app = getApp();
 data: {
  region:[&#39;湖南&#39;,&#39;长沙&#39;,&#39;岳麓&#39;],
  date:&#39;2010-10-10&#39;,
  gender:[&#39;男&#39;,&#39;女&#39;],
  index:0
 }
 bindViewEvent:function(e){
  app.process(this,e);  
 }

Related js classes

//component.js
const select = require(&#39;../component/select.js&#39;);
const upload = require(&#39;../component/upload.js&#39;);
class component{
 constructor(com, that) {
  this.com = com;
  this.that = that;
 }
 //绑定下拉框选择事件
 bindSelect(data){
  let self = this;
  let mode = data.currentTarget.dataset.mode;
  let name = data.currentTarget.dataset.name;
  let picker = new select({
   that: self.that,
   mode: mode,
   name: name
  });
  picker.change(data.detail.value);
 }
 //点击事件,传递参数为e.currentTarget.dataset
 bindImageChoose(data){
  //图片上传
  this.uploader = new upload({
   that: that,
   name: data.name,
   mode: data.mode,
   count: data.count || 9
  });
  this.uploader.choose();
 }
 bindImageDel(data){
  //图片上传
  this.uploader = new upload({
   that: that,
   name: data.name,
   mode: data.mode,
   count: data.count || 9
  });
  this.uploader.del(data.index);
 }
}
module.exports = component;
//select.js
/*
* 下拉框对象
*/
class picker{
 constructor(data){
  this.that = data.that;
  this.name = data.name || &#39;date&#39;;
  this.mode = data.mode || &#39;selector&#39;;
 }
 show(name,data){
  let view = {};
  view[name] = data;
  this.that.setData(view);
 }
 change(data){
  let self = this;
  self.show(self.name, data);
 }
}
module.exports = picker;
//upload.js
class picUploader {
 constructor(data) {
  this.that = data.that;
  this.name = data.name;
  this.mode = data.mode || 1;
  this.count = this.model == 1 ? 1 : data.count || 9;
 }
 /*
 * 选择图片
 */
 choose() {
  const self = this;
  wx.chooseImage({
   count: (self.count - self.that.data[self.name].length),
   sizeType: [&#39;original&#39;, &#39;compressed&#39;],
   sourceType: [&#39;album&#39;, &#39;camera&#39;],
   success: function (res) {
    var tempFilePaths = res.tempFilePaths;
    self.append(tempFilePaths);
   }
  })
 }
 /*
 * 显示图片
 */
 show() {
  let self = this;
  let view = {};
  view[self.name] = self.that.data[self.name];
  self.that.setData(view);
 }
 /*
 * 追加图片
 */
 append(data) {
  const self = this;
  for (let i = 0; i < data.length; i++) {
   self.that.data[self.name].push(data[i]);
  }
  self.show();
 }
 /*
 * 删除图片
 */
 del(index) {
  let self = this;
  self.that.data[self.name].splice(index, 1);
  self.show();
 }
}
module.exports = picUploader;


The above is the detailed content of How to use the WeChat mini program drop-down box component. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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