ui-js-components-picker.md 6.8 KB
Newer Older
Z
zengyawen 已提交
1 2
# Picker

Z
zengyawen 已提交
3
Picker是滑动选择器组件,类型支持普通选择器、日期选择器、时间选择器、时间日期选择器和多列文本选择器。具体用法请参考[Picker API](../reference/arkui-js/js-components-basic-picker.md)
Z
zengyawen 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19


## 创建Picker组件

在pages/index目录下的hml文件中创建一个Picker组件。

```
<!-- index.hml -->
<div class="container">
  <picker>    picker  </picker>
<div>
```

```
/* index.css */
.container {
W
wangshuainan1 已提交
20 21
  width: 100%;
  height: 100%; 
Z
zengyawen 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
```

![zh-cn_image_0000001210951541](figures/zh-cn_image_0000001210951541.gif)


## 设置Picker类型

通过设置Picker的type属性来选择滑动选择器类型,如定义Picker为日期选择器。

```
<!-- index.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>
```

```
/* index.css */
.container {
W
wangshuainan1 已提交
47 48
  width: 100%;
  height: 100%;
Z
zengyawen 已提交
49 50 51 52 53 54
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.pickertext{
Z
zengyawen 已提交
55
  margin-bottom: 30px;
Z
zengyawen 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69
}
```

```
// xxx.js
export default {
  data: {
    rangetext:['15', "20", "25"],
    textvalue:'Select text',
    datevalue:'Select date',
  }
}
```

Z
zengyawen 已提交
70
![zh-cn_image_0000001189098638](figures/zh-cn_image_0000001189098638.gif)
Z
zengyawen 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> - 普通选择器设置取值范围时,需要使用数据绑定的方式。
> 
> - 日期选择器的lunarswitch属性只支持手机和平板设备。


## 设置时间展现格式

Picker的hours属性定义时间的展现格式,可选类型有12小时制和24小时制。

```
<!-- index.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>
```

```
/* index.css */
.container {
W
wangshuainan1 已提交
93 94
  width: 100%;
  height: 100%;
Z
zengyawen 已提交
95 96 97 98 99 100 101 102 103 104 105 106
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.pickertime {
  margin-bottom:50px;
  width: 300px;
  height: 50px;
}
```

Z
zengyawen 已提交
107
![zh-cn_image_0000001234327855](figures/zh-cn_image_0000001234327855.gif)
Z
zengyawen 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> - hours属性为12:按照12小时制显示,用上午和下午进行区分;
> 
> - hours属性为24:按照24小时制显示。


## 添加响应事件

对Picker添加change和cancel事件,来对选择的内容进行确定和取消。

```
<!-- index.hml -->
<div class="container">
  <picker id="picker_multi" type="multi-text" value="{{multitextvalue}}" columns="3" range="{{multitext}}" selected="
     {{multitextselect}}" onchange="multitextonchange" oncancel="multitextoncancel" class="pickermuitl"></picker>
</div>
```

```
/* index.css */
.container {
W
wangshuainan1 已提交
130 131
  width: 100%;
  height: 100%;
Z
zengyawen 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5; 
}
.pickermuitl {
  margin-bottom:20px;
  width: 600px;
  height: 50px;
  font-size: 25px;
  letter-spacing:15px;
}
```

```
// xxx.js
import prompt from '@system.prompt';
export default {
  data: {
    multitext:[["a", "b", "c"], ["e", "f", "g"], ["h", "i"]],
    multitextvalue:'Select multi-line text',
    multitextselect:[0,0,0],
  },
  multitextonchange(e) {
    this.multitextvalue=e.newValue;
    prompt.showToast({ message:"Multi-column text changed to:" + e.newValue })
  },
  multitextoncancel() {
    prompt.showToast({ message:"multitextoncancel" })
  },
}
```

Z
zengyawen 已提交
165
![zh-cn_image_0000001234009343](figures/zh-cn_image_0000001234009343.gif)
Z
zengyawen 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198


## 场景示例


在本场景中,开发者可以自定义填写当前的健康情况来进行打卡。


```
<!-- index.hml -->
<div class="doc-page">
  <text class="title">Health check-in</text>
  <div class="out-container">
    <text class="txt">Office:</text>
    <picker class="pick" focusable="true" type="text" value="{{pos}}" range="{{posarr}}" onchange="setPos"></picker>
  </div>
  <divider class="dvd"></divider>
  <div class="out-container">
    <text class="txt">Office hours:</text>
    <picker class="pick" type="date" value="{{datevalue}}"  start="2002-2-5" end="2030-6-5" selected="{{dateselect}}"
      lunarswitch="true" onchange="dateonchange"></picker>
  </div>
  <divider class="dvd"></divider>
  <div class="out-container">
    <text class="txt">Having fever or cold symptoms</text>
    <picker class="pick" type="text" value="{{yorn1}}" range="{{yesno}}" selected="1" onchange="isFever"></picker>
  </div>
  <divider class="dvd"></divider>
  <div class="out-container">
    <text class="txt">Close contact with someone with COVID-19</text>
    <picker class="pick" type="text" value="{{yorn2}}" range="{{yesno}}" selected="1" onchange="isTouch"></picker>
  </div>
  <div class="out-container">
Z
zengyawen 已提交
199
    <button value="Submit" style="margin-top:100px;width:50%;font-color:#0000ff;height:80px" onclick="showtoast"></button>
Z
zengyawen 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  </div>
</div>
```


```
/* index.css */
.doc-page {
  flex-direction: column;
  background-color: #F1F3F5;
}
.title {
  margin-top: 30px;
  margin-bottom: 30px;
  margin-left: 50px;
  font-weight: bold;
  color: #0000ff;
  font-size: 38px;
}
.out-container {
  flex-direction: column;
  align-items: center;
}
.pick {
  width: 80%;
  height: 76px;
  border: 1px solid #0000ff;
  border-radius: 20px;
  padding-left: 12px;
}
.txt {
  width: 80%;
  font-size: 18px;
  text-align: left;
  margin-bottom: 12px;
  margin-left: 12px;
}
.dvd {
  margin-top: 30px;
  margin-bottom: 30px;
  margin-left: 80px;
  margin-right: 80px;
  color: #6495ED;
  stroke-width: 6px;
}
```


```
// xxx.js
import pmt from '@system.prompt'
export default {
  data: {
    yorn1:'No',
    yorn2:'No',
    pos:'Home',
    yesno:['Yes', 'No'],
    posarr:['Home', 'Company'],
    datevalue:'Select time',
    datetimeselect:'2012-5-6-11-25',
    dateselect:'2021-9-17',
    showbuild:true
  },
  onInit() {
  },
  isFever(e) {
    this.yorn1 = e.newValue
  },
  isTouch(e) {
    this.yorn2 = e.newValue
  },
  setPos(e) {
    this.pos = e.newValue
    if (e.newValue === 'Non-research center') {
      this.showbuild = false
    } else {
      this.showbuild = true
    }
  },
  setbuild(e) {
    this.build = e.newValue
  },
  dateonchange(e) {
Z
zengyawen 已提交
283
    e.month=e.month+1;
Z
zengyawen 已提交
284
    this.datevalue = e.year + "-" + e.month + "-" + e.day;
Z
zengyawen 已提交
285
    pmt.showToast({ message:"date:"+e.year+"-"+e.month+"-"+e.day }) 
Z
zengyawen 已提交
286 287 288 289 290 291 292 293 294 295 296 297
  },
  showtoast() {
    pmt.showToast({
      message: 'Submitted.',
      duration: 2000,
      gravity: 'center'
    })
  }
}
```


Z
zengyawen 已提交
298
![zh-cn_image_0000001234342189](figures/zh-cn_image_0000001234342189.gif)