js-components-basic-picker-view.md 15.3 KB
Newer Older
1
# picker-view
Z
zengyawen 已提交
2

3 4 5
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

7 8 9 10
The **\<picker-view>** component provides the view that shows an embedded scrollable selector on the screen.


## Child Components
Z
zengyawen 已提交
11 12 13

Not supported

14 15 16 17 18 19 20 21 22

## Attributes

In addition to the [universal attributes](../arkui-js/js-components-common-attributes.md), the following attributes are supported.

| Name  | Type    | Default Value | Mandatory  | Description                                      |
| ---- | ------ | ---- | ---- | ---------------------------------------- |
| type | string | text | No   | Type of the scrollable selector, which cannot be changed dynamically. Available values are as follows:<br>- **text**: text selector.<br>- **time**: time selector.<br>- **date**: date selector.<br>- **datetime**: date and time selector.<br>- **multi-text**: multi-column text selector.|

E
ester.zhou 已提交
23
### Text Selector
24 25 26 27 28 29 30 31

| Name             | Type    | Default Value | Mandatory  | Description                                      |
| --------------- | ------ | ---- | ---- | ---------------------------------------- |
| range           | Array  | -    | No   | Value range of the text selector.<br>Use the data binding mode, for example, `range = {{data}}`. Declare the corresponding variable `data: ["15", "20", "25"]` in JavaScript.|
| selected        | string | 0    | No   | Default value of the text selector. The value is the index of **range**.            |
| indicatorprefix | string | -    | No   | Prefix field added when a value is specified for the text selector.                        |
| indicatorsuffix | string | -    | No   | Suffix field added when a value is specified for the text selector.                        |

E
ester.zhou 已提交
32
### Time Selector
33 34 35 36 37

| Name           | Type     | Default Value                                | Mandatory  | Description                                      |
| ------------- | ------- | ----------------------------------- | ---- | ---------------------------------------- |
| containsecond | boolean | false                               | No   | Whether seconds are contained.                             |
| selected      | string  | Current time                               | No   | Default value of the time selector, in the format of HH:mm.<br>If seconds are contained, the format is HH:mm:ss.|
E
ester.zhou 已提交
38
| hours         | number  | 24<sup>1-4</sup><br>-<sup>5+</sup> | No   | Time format used by the time selector. Available values are as follows:<br>- **12**: displayed in 12-hour format and distinguished by a.m. and p.m.<br>- **24**: displayed in 24-hour format.<br>Since API version 5, the default value is the most commonly-used hour format in the current locale.|
39

E
ester.zhou 已提交
40
### Date Selector
41 42 43 44 45 46 47 48 49

| Name                | Type          | Default Value       | Mandatory  | Description                                      |
| ------------------ | ------------ | ---------- | ---- | ---------------------------------------- |
| start              | &lt;time&gt; | 1970-1-1   | No   | Start date of the date selector, in the format of YYYY-MM-DD.       |
| end                | &lt;time&gt; | 2100-12-31 | No   | End date of the date selector, in the format of YYYY-MM-DD.       |
| selected           | string       | Current date      | No   | Default value of the date selector, in the format of YYYY-MM-DD.      |
| lunar<sup>5+</sup> | boolean      | false      | No   | Whether the pop-up window displays the lunar calendar.                     |
| lunarswitch        | boolean      | false      | No   | Whether to display the lunar calendar switch in the date selector. When this switch is displayed, the user can switch between the lunar calendar and Gregorian calendar. Turn on the switch to display the lunar calendar, and turn off the switch to hide the lunar calendar.|

E
ester.zhou 已提交
50
### Date and Time Selector
51 52 53 54

| Name                | Type     | Default Value                                | Mandatory  | Description                                      |
| ------------------ | ------- | ----------------------------------- | ---- | ---------------------------------------- |
| selected           | string  | Current date and time                             | No   | Default value of the date and time selector. The value can be in the format of MM-DD-HH-mm or YYYY-MM-DD-HH-mm. If the year is not set, the current year is used by default. The value you set is the date selected by default in the pop-up window.|
E
ester.zhou 已提交
55
| hours              | number  | 24<sup>1-4</sup><br>-<sup>5+</sup> | No   | Time format used by the date and time selector. Available values are as follows:<br>- **12**: displayed in 12-hour format and distinguished by a.m. and p.m.<br>- **24**: displayed in 24-hour format.<br>Since API version 5, the default value is the most commonly-used hour format in the current locale.|
56 57 58
| lunar<sup>5+</sup> | boolean | false                               | No   | Whether the pop-up window displays the lunar calendar.                   |
| lunarswitch        | boolean | false                               | No   | Whether to display the lunar calendar switch in the date and time selector. When this switch is displayed, the user can switch between the lunar calendar and Gregorian calendar. Turn on the switch to display the lunar calendar, and turn off the switch to hide the lunar calendar.|

E
ester.zhou 已提交
59
### Multi-Column Text Selector
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

| Name      | Type     | Default Value      | Mandatory  | Description                                      |
| -------- | ------- | --------- | ---- | ---------------------------------------- |
| columns  | number  | -         | Yes   | Number of columns in the multi-column text selector.                           |
| range    | Two-dimensional array| -         | No   | Items of the multi-column text selector. The value is a two-dimensional array that indicates the number of columns. Each item in the array indicates the data of each column, for example, **[["a", "b"], ["c", "d"]]**.<br>Use the data binding mode, for example, `range = {{data}}`. Declare the corresponding variable `data: ["15", "20", "25"]` in JavaScript.|
| selected | Array   | [0,0,0,…]| No   | Default value of the multi-column text selector, which is an array consisting of the indexes of the selected items in each column.|


## Styles

In addition to the [universal styles](../arkui-js/js-components-common-styles.md), the following styles are supported.

| Name                              | Type            | Default Value       | Mandatory  | Description                                      |
| -------------------------------- | -------------- | ---------- | ---- | ---------------------------------------- |
| color                            | &lt;color&gt;  | \#ffffff   | No   | Font color of a candidate item.                                |
| font-size                        | &lt;length&gt; | 16px       | No   | Font size of a candidate item. The value is of the length type, in pixels.                  |
| selected-color                   | &lt;color&gt;  | #ff0a69f7  | No   | Font color of the selected item.                                |
| selected-font-size               | &lt;length&gt; | 20px       | No   | Font size of the selected item. The value is of the length type, in pixels.                  |
| disappear-color<sup>5+</sup>     | &lt;color&gt;  | \#ffffff   | No   | Font color of the items that gradually disappear. Disappearing items are the top option and bottom option of a column containing five options in total.  |
| disappear-font-size<sup>5+</sup> | &lt;length&gt; | 14px       | No   | Font size of the items that gradually disappear. Disappearing items are the top option and bottom option of a column containing five options in total.  |
E
ester.zhou 已提交
80
| font-family                      | string         | sans-serif | No   | Font family of the selector, in which fonts are separated by commas (,). Each font is set using a font name or font family name. The first font in the family or the specified [custom font](../arkui-js/js-components-common-customizing-font.md) is used for the text. |
81 82 83


## Events
Z
zengyawen 已提交
84 85 86

The following events are supported.

E
ester.zhou 已提交
87
### Text Selector
88 89 90

| Name    | Parameter                                      | Description             |
| ------ | ---------------------------------------- | --------------- |
E
ester.zhou 已提交
91
| change | { newValue: newValue, newSelected: newSelected } | Triggered when a value is specified for the text selector.|
92

E
ester.zhou 已提交
93
### Time Selector
94 95 96

| Name    | Parameter                                      | Description                             |
| ------ | ---------------------------------------- | ------------------------------- |
E
ester.zhou 已提交
97
| change | { hour: hour, minute: minute, [second:second]} | Triggered when a value is specified for the time selector.<br>If seconds are contained, the value contains hour, minute, and second.|
98

E
ester.zhou 已提交
99
### Date Selector
100 101 102

| Name    | Parameter                                      | Description             |
| ------ | ---------------------------------------- | --------------- |
E
ester.zhou 已提交
103
| change | { year:year, month:month, day:day } | Triggered when a value is specified for the date selector.|
104

E
ester.zhou 已提交
105
### Date and Time Selector
106 107 108

| Name    | Parameter                                      | Description               |
| ------ | ---------------------------------------- | ----------------- |
E
ester.zhou 已提交
109
| change | { year:year, month:month, day:day,  hour:hour, minute:minute } | Triggered when a value is specified for the date and time selector.|
110

E
ester.zhou 已提交
111
### Multi-Column Text Selector
112 113 114

| Name          | Parameter                                      | Description                                      |
| ------------ | ---------------------------------------- | ---------------------------------------- |
E
ester.zhou 已提交
115
| columnchange | { column:column, newValue:newValue, newSelected:newSelected } | Triggered when the value of a column in the multi-column selector changes. <br>**column**: column whose value has changed. <br>**newValue**: selected value. <br>**newSelected**: index of the selected value.|
116 117 118


## Methods
Z
zengyawen 已提交
119 120 121 122

Not supported


123
## Example
E
ester.zhou 已提交
124 125 126 127 128 129 130 131 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 165 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 199 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
1. Text Selector
    ```html
    <!-- xxx.hml -->
    <div class="container">
        <text class="title">
            Selected value: {{value}} Selected index: {{index}}
        </text>
        <picker-view class="text-picker" type="text" range="{{options}}" selected="0" indicatorprefix="prefix" indicatorsuffix="suffix" @change="handleChange"></picker-view>
    </div>
    ```
    
    ```css
    /* xxx.css */
    .container {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        left: 0px;
        top: 0px;
        width: 454px;
        height: 454px;
    }
    .title {
        font-size: 30px;
        text-align: center;
        margin-top: 20px;
    }
    ```
    
    ```js
    /* xxx.js */
    export default {
        data: {
            options: ['Option 1','Option 2','Option 3'],
            value: "Option 1",
            index: 0
        },
        handleChange(data) {
            this.value = data.newValue;
            this.index = data.newSelected;
        },
    }
    ```
    ![](figures/pickerview1.gif)

2. Time Selector
    ```html
    <!-- xxx.hml -->
    <div class="container">
      <text class="title">
        Selected: {{time}}
      </text>
      <picker-view class="time-picker" type="time" selected="{{defaultTime}}" @change="handleChange"></picker-view>
    </div>
    ```
    
    ```css
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
      left: 0px;
      top: 0px;
      width: 454px;
      height: 454px;
    }
    .title {
      font-size: 30px;
      text-align: center;
    }
    .time-picker {
      width: 500px;
      height: 400px;
      margin-top: 20px;
    }
    ```
    
    ```js
    /* xxx.js */
    export default {
      data: {
        defaultTime: "",
        time: "",
      },
      onInit() {
        this.defaultTime = this.now();
      },
      handleChange(data) {
        this.time = this.concat(data.hour, data.minute);
      },
      now() {
        const date = new Date();
        const hours = date.getHours();
        const minutes = date.getMinutes();
        return this.concat(hours, minutes);
      },
      fill(value) {
        return (value > 9 ? "" : "0") + value;
      },
      concat(hours, minutes) {
        return `${this.fill(hours)}:${this.fill(minutes)}`;
      },
    }
    ```
    
    ![](figures/pickerview2.gif)

3. Date Selector
    ```html
    <!-- xxx.hml -->
    <div class="container">
        <text class="title">
            Selected: {{time}}
        </text>
        <picker-view class="time-picker" type="time" selected="{{defaultTime}}" @change="handleChange"></picker-view>
    </div>
    ```
    
    ```css
    /* xxx.css */
    .container {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        left: 0px;
        top: 0px;
        width: 454px;
        height: 454px;
    }
    .title {
        font-size: 30px;
        text-align: center;
        margin-top: 20px;
    }
    .date-picker {
        width: 500px;
        height: 400px;
        margin-top: 50px;
    }
    ```
    
    ```js
    /* xxx.js */
    export default {
        data: {
            date: "",
        },
        handleChange(data) {
            this.date = data.year + "" + data.month + "" + data.day + "";
        },
    }
    ```
    

4. Date and Time Selector
    ```html
    <!-- xxx.hml -->
    <div class="container">
        <text class="title">
            Selected: {{datetime}}
        </text>
        <picker-view class="date-picker" type="datetime"  hours="24" lunarswitch="true" @change="handleChange"></picker-view>
    </div>
    ```
    
    ```css
    /* xxx.css */
    .container {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        left: 0px;
        top: 0px;
        width: 500px;
        height: 454px;
    }
    .title {
        font-size: 30px;
        text-align: center;
        margin-top: 20px;
    }
    .date-picker {
        width: 500px;
        height: 400px;
        margin-top: 50px;
    }
    ```
    
    ```js
    /* xxx.js */
    export default {
        data: {
            datetime: "",
        },
        handleChange(data) {
            this.datetime = data.year + "" + data.month + "" + data.day + "" + data.hour + "" + data.minute + "";
        },
    }
    ```
    

5. Multi-Column Text Selector

    ```html
    <!-- xxx.hml -->
    <div class="container">
        <text class="title">
            Selected: {{ value }}
        </text>
        <picker-view class="multitype-picker" type="multi-text" columns="3" range="{{ multitext }}" @columnchange="handleChange"></picker-view>
    </div>
    ```
    
    ```css
    /* xxx.css */
    .container {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        left: 0px;
        top: 0px;
        width: 500px;
        height: 454px;
    }
    .title {
        font-size: 30px;
        text-align: center;
        margin-top: 20px;
    }
    ```
    
    ```js
    /* xxx.js */
    export default {
        data: {
            multitext: [
                [1, 2, 3],
                [4, 5, 6],
                [7, 8, 9],
            ],
            value: ""
        },
        handleChange(data) {
            this.value = "Column: " + data.column + "," + "Value: " + data.newValue + ", Index:" + data.newSelected;
        },
    }
    ```
    ![](figures/pickerview5.gif)