picker是滑动选择器组件,类型支持普通选择器、日期选择器、时间选择器、时间日期选择器和多列文本选择器。
创建picker组件
在pages/index目录下的hml文件中创建一个picker组件。
<!-- xxx.hml -->
<div class="container">
<picker> picker </picker>
</div>
/* xxx.css */
.container {
width: 100%;
height: 100%;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #F1F3F5;
}
设置picker类型
通过设置picker的type属性来选择滑动选择器类型,如定义picker为日期选择器。
<!-- xxx.hml -->
<div class="container">
<picker id="picker_text" type="text" value="{
{textvalue}}"range="{
{rangetext}}" class="pickertext" ></picker>
<picker id="picker_date" type="date" value="{
{datevalue}}" lunarswitch="true" start="2002-2-5" end="2030-6-5" class="pickerdate"></picker>
</div>
/* xxx.css */
.container {
width: 100%;
height: 100%;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #F1F3F5;
}
.pickertext{
margin-bottom: 30px;
}
// xxx.js
export default {
data: {
rangetext:['15', "20", "25"],
textvalue:'Select text',
datevalue:'Select date',
}
}
说明
普通选择器设置取值范围时,需要使用数据绑定的方式。
设置时间展现格式
picker的hours属性定义时间的展现格式,可选类型有12小时制和24小时制。
<!-- xxx.hml -->
<div class="container">
<picker id="picker_time" type="time" value="12-hour format" hours="12" onchange="timeonchange" class="pickertime"></picker>
<picker id="picker_time" type="time" value="24-hour format" hours="24" onchange="timeonchange" class="pickertime"></picker>
</div>
/* xxx.css */
.container {
width: 100%;
height: 100%;
flex-direction: column;
justify-content: center;
align-items