欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

微信小程序下拉框组件使用方法详解

时间:2020-11-25 12:32:15|栏目:JavaScript代码|点击:

本文实例为大家分享了微信小程序下拉框组件的使用方法,供大家参考,具体内容如下

适用场景

1、省市三级联动
2、出生日期选择
3、性别选择
4、一般性的下拉选择等

一、省市三级联动使用

注意mode = region,以及value = “一维数组”

//.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>

二、出生日期选择

注意mode = date,以及value = “日期字符串”

<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>
</picker>

三、性别选择

注意,不填mode默认为selector,range=”一维数组”,value=”当前选中索引”

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

以上都需要在.js里设置相关初始变量!

//.js
 var app = getApp();

 data: {

  region:['河北','沧州','河间'],
  date:'2010-10-10',
  gender:['男','女'],
  index:0
 },

 bindViewEvent:function(e){

  app.process(this,e);  
 }

相关js类

//component.js
const select = require('../component/select.js');
const upload = require('../component/upload.js');

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 || 'date';
  this.mode = data.mode || 'selector';
 }

 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: ['original', 'compressed'],
   sourceType: ['album', 'camera'],
   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;

上一篇:JS控制表格实现一条光线流动分割行的方法

栏    目:JavaScript代码

下一篇:微信小程序如何获取用户收货地址

本文标题:微信小程序下拉框组件使用方法详解

本文地址:http://www.codeinn.net/misctech/26221.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有