近日遇到一个需要,就是picker功能,通过选择显示值而取得真实的value值,怎么理解呢。就好像我们HTML中的select,我选这的是“否”,其实我选择的值是“0”,而我选择的是“是”则选择的是“1”。下面这个picker功能就是我要选择显示中文,而选择了其对应的字符串,即当我选择了“多威尔公司大门”我要选择“1234567890a0”,下面是效果图。
//下面是小程序js实现代码
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
console.log('onLoad', options)
var userName = wx.getStorageSync('userName');
var userPassword = wx.getStorageSync('userPassword');
var that = this
wx.request({
url: 'https://www.100txy.com/Home/door/getcontroller', //仅为示例,并非真实的接口地址
data: {
username: userName,
password: userPassword,
},
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('得到的数组',res.data)
that.setData({
Doornos: res.data.a,
Controllers: res.data.b,
})
}
})
}
//下面是小程序wxml代码
<view class="container">
<view class="page-body">
<view class="page-section">
<!-- <view class="weui-cells__title">设备区域</view> -->
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd" style="width:35%;">
<image class="nameImage" src="../../image/address.png"></image>
<!-- <label class="loginLab">用户区域:</label> -->
<view class="weui-label">用户区域:</view>
</view>
<view class="weui-cell__bd">
<picker bindchange="bindDoornoChange" name="doorfieldno" value="{{Doornos[index2].no}}" range="{{Doornos}}" range-key="address">
<view class="weui-input">{{Doornos[index2].address}}</view>
</picker>
</view>
</view>
</view>
<!-- <view class="weui-cells__title">设备ID</view> -->
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd" style="width:35%;">
<image class="nameImage" src="../../image/door.png"></image>
<!-- <label class="loginLab">门禁设备:</label> -->
<view class="weui-label">门禁设备:</view>
</view>
<view class="weui-cell__bd">
<picker bindchange="bindPickerChange" name="controllersn" value="{{Controllers[index].sn}}" range="{{Controllers}}" range-key="name">
<view class="weui-input">{{Controllers[index].name}}</view>
</picker>
</view>
</view>
</view>
</view>
<view class="btn-area">
<button bindtap="makeRequest" type="primary" disabled="{{buttonDisabled}}" loading="{{loading}}">开门</button>
</view>
</view>
</view>
//下面是PHP接口函数代码
public function getcontroller(){
$xtlen=$this->redis->scard("xtset");//读取心跳包xtset集合里面个数
$xtArr = $this->redis->smembers('xtset');//于数组的形式打印出集合里面的数据
foreach ($xtArr as $key => $value) {
if(!$this->redis->get($value)){
$this->redis->srem('xtset',$value);//set集合删除某个元素的操作
}
}
$cardno=$_REQUEST['username'];
$password=$_REQUEST['password'];
$appInfo=M('Smallapp')->where(array('cardno'=>$cardno,'password'=>$password))->field('doorfieldno,doorstr,name,regtime,cardenddate,address')->find();
$dbname=M('User')->where(array('doorfieldno'=>$appInfo['doorfieldno']))->limit(1)->getField('dbname');
$DoorcontrollerModel= M('Doorcontroller','tab_','mysql://leixiaot:leixiaotian123@127.0.0.1:3306/'.$dbname);
$areaArr=explode('-',$appInfo['doorstr']);
foreach ($areaArr as $key => $value) {
$doorcontrollerinfo=$DoorcontrollerModel->where(array('r_no'=>$value))->field('areaname,controllersn')->find();
if(in_array($appInfo['doorfieldno'].'-'.$doorcontrollerinfo['controllersn'],$xtArr)){
$controllerArr[$key]['name']=$doorcontrollerinfo['areaname'];
$controllerArr[$key]['sn']=$doorcontrollerinfo['controllersn'];
// $controllerArr[$doorcontrollerinfo['controllersn']]=$doorcontrollerinfo['areaname'];
}
}
$doorareaArr=array(
array(
'no'=>$appInfo['doorfieldno'],
'address'=>$appInfo['address']
),
);
echo json_encode(array('a'=>$doorareaArr,'b'=>$controllerArr));
}
其实这个接口函数最重要的是最后两个二维数组$doorareaAr,$controllerArr