1# \<picker> Development 2 3The **\<picker>** component supports common, date, time, data and time, and multi-column text selectors. For details, see [picker](../reference/arkui-js/js-components-basic-picker.md). 4 5 6## Creating a \<picker> Component 7 8Create a **\<picker>** component in the .hml file under **pages/index**. 9 10```html 11<!-- xxx.hml --> 12<div class="container"> 13 <picker> picker </picker> 14</div> 15``` 16 17```css 18/* xxx.css */ 19.container { 20 width: 100%; 21 height: 100%; 22 flex-direction: column; 23 justify-content: center; 24 align-items: center; 25 background-color: #F1F3F5; 26} 27``` 28 29![en-us_image_0000001223287716](figures/en-us_image_0000001223287716.gif) 30 31 32## Setting the Picker Type 33 34Set the **type** attribute of the **\<picker>** component. For example, set it to **date**. 35 36```html 37<!-- xxx.hml --> 38<div class="container"> 39 <picker id="picker_text" type="text" value="{{textvalue}}"range="{{rangetext}}" class="pickertext" ></picker> 40 <picker id="picker_date" type="date" value="{{datevalue}}" lunarswitch="true" start="2002-2-5" end="2030-6-5" class="pickerdate"></picker> 41</div> 42``` 43 44```css 45/* xxx.css */ 46.container { 47 width: 100%; 48 height: 100%; 49 flex-direction: column; 50 justify-content: center; 51 align-items: center; 52 background-color: #F1F3F5; 53} 54.pickertext{ 55 margin-bottom: 30px; 56} 57``` 58 59```js 60// xxx.js 61export default { 62 data: { 63 rangetext:['15', "20", "25"], 64 textvalue:'Select text', 65 datevalue:'Select date', 66 } 67} 68``` 69 70![en-us_image_0000001267647893](figures/en-us_image_0000001267647893.gif) 71 72> **NOTE** 73> 74> When setting the value range of a common selector, you must use the data binding mode. 75 76 77## Setting the Time Format 78 79Set the **hours** attribute to specify the time format used by the time picker. Available values include **12** and **24**, indicating the 12-hour format and 24-hour format, respectively. 80 81```html 82<!-- xxx.hml --> 83<div class="container"> 84 <picker id="picker_time" type="time" value="12-hour format" hours="12" onchange="timeonchange" class="pickertime"></picker> 85 <picker id="picker_time" type="time" value="24-hour format" hours="24" onchange="timeonchange" class="pickertime"></picker> 86</div> 87``` 88 89```css 90/* xxx.css */ 91.container { 92 width: 100%; 93 height: 100%; 94 flex-direction: column; 95 justify-content: center; 96 align-items: center; 97 background-color: #F1F3F5; 98} 99.pickertime { 100 margin-bottom:50px; 101 width: 300px; 102 height: 50px; 103} 104``` 105 106![en-us_image_0000001222807808](figures/en-us_image_0000001222807808.gif) 107 108> **NOTE** 109> - When **hours** is set to **12**, the time is displayed in 12-hour format and distinguished by a.m. and p.m. 110> 111> - When **hours** is set to **24**, the time is displayed in 24-hour format. 112 113 114## Adding Response Events 115 116Add the **change** event to confirm selection and the **cancel** event to cancel selection. 117 118```html 119<!-- xxx.hml --> 120<div class="container"> 121 <picker id="picker_multi" type="multi-text" value="{{multitextvalue}}" columns="3" range="{{multitext}}" selected=" 122 {{multitextselect}}" onchange="multitextonchange" oncancel="multitextoncancel" class="pickermuitl"></picker> 123</div> 124``` 125 126```css 127/* xxx.css */ 128.container { 129 width: 100%; 130 height: 100%; 131 flex-direction: column; 132 justify-content: center; 133 align-items: center; 134 background-color: #F1F3F5; 135} 136.pickermuitl { 137 margin-bottom:20px; 138 width: 600px; 139 height: 50px; 140 font-size: 25px; 141 letter-spacing:15px; 142} 143``` 144 145```js 146// xxx.js 147import promptAction from '@ohos.promptAction'; 148export default { 149 data: { 150 multitext:[["a", "b", "c"], ["e", "f", "g"], ["h", "i"]], 151 multitextvalue:'Select multi-line text', 152 multitextselect:[0,0,0], 153 }, 154 multitextonchange(e) { 155 this.multitextvalue=e.newValue; 156 promptAction.showToast({ message:"Multi-column text changed to:" + e.newValue }) 157 }, 158 multitextoncancel() { 159 promptAction.showToast({ message:"multitextoncancel" }) 160 }, 161} 162``` 163 164![en-us_image_0000001223127748](figures/en-us_image_0000001223127748.gif) 165 166 167## Example Scenario 168 169 170Implement a health check-in application by using the **\<picker>** component. 171 172 173```html 174<!-- xxx.hml --> 175<div class="doc-page"> 176 <text class="title">Health check-in</text> 177 <div class="out-container"> 178 <text class="txt">Office:</text> 179 <picker class="pick" focusable="true" type="text" value="{{pos}}" range="{{posarr}}" onchange="setPos"></picker> 180 </div> 181 <divider class="dvd"></divider> 182 <div class="out-container"> 183 <text class="txt">Office hours:</text> 184 <picker class="pick" type="date" value="{{datevalue}}" start="2002-2-5" end="2030-6-5" selected="{{dateselect}}" 185 lunarswitch="true" onchange="dateonchange"></picker> 186 </div> 187 <divider class="dvd"></divider> 188 <div class="out-container"> 189 <text class="txt">Having fever or cold symptoms</text> 190 <picker class="pick" type="text" value="{{yorn1}}" range="{{yesno}}" selected="1" onchange="isFever"></picker> 191 </div> 192 <divider class="dvd"></divider> 193 <div class="out-container"> 194 <text class="txt">Close contact with someone with COVID-19</text> 195 <picker class="pick" type="text" value="{{yorn2}}" range="{{yesno}}" selected="1" onchange="isTouch"></picker> 196 </div> 197 <div class="out-container"> 198 <button value="Submit" style="margin-top:100px;width:50%;font-color:#0000ff;height:80px" onclick="showtoast"></button> 199 </div> 200</div> 201``` 202 203 204```css 205/* xxx.css */ 206.doc-page { 207 flex-direction: column; 208 background-color: #F1F3F5; 209} 210.title { 211 margin-top: 30px; 212 margin-bottom: 30px; 213 margin-left: 50px; 214 font-weight: bold; 215 color: #0000ff; 216 font-size: 38px; 217} 218.out-container { 219 flex-direction: column; 220 align-items: center; 221} 222.pick { 223 width: 80%; 224 height: 76px; 225 border: 1px solid #0000ff; 226 border-radius: 20px; 227 padding-left: 12px; 228} 229.txt { 230 width: 80%; 231 font-size: 18px; 232 text-align: left; 233 margin-bottom: 12px; 234 margin-left: 12px; 235} 236.dvd { 237 margin-top: 30px; 238 margin-bottom: 30px; 239 margin-left: 80px; 240 margin-right: 80px; 241 color: #6495ED; 242 stroke-width: 6px; 243} 244``` 245 246 247```js 248// xxx.js 249import promptAction from '@ohos.promptAction' 250export default { 251 data: { 252 yorn1:'No', 253 yorn2:'No', 254 pos:'Home', 255 yesno:['Yes', 'No'], 256 posarr:['Home', 'Company'], 257 datevalue:'Select time', 258 datetimeselect:'2012-5-6-11-25', 259 dateselect:'2021-9-17', 260 showbuild:true 261 }, 262 onInit() { 263 }, 264 isFever(e) { 265 this.yorn1 = e.newValue 266 }, 267 isTouch(e) { 268 this.yorn2 = e.newValue 269 }, 270 setPos(e) { 271 this.pos = e.newValue 272 if (e.newValue === 'Non-research center') { 273 this.showbuild = false 274 } else { 275 this.showbuild = true 276 } 277 }, 278 setbuild(e) { 279 this.build = e.newValue 280 }, 281 dateonchange(e) { 282 e.month=e.month+1; 283 this.datevalue = e.year + "-" + e.month + "-" + e.day; 284 promptAction.showToast({ message:"date:"+e.year+"-"+e.month+"-"+e.day }) 285 }, 286 showtoast() { 287 promptAction.showToast({ 288 message: 'Submitted.', 289 duration: 2000, 290 gravity: 'center' 291 }) 292 } 293} 294``` 295 296 297![en-us_image_0000001267887877](figures/en-us_image_0000001267887877.gif) 298