openharmony 鸿蒙 js-components-basic-picker-view

2025-06-12 浏览 (1)

picker-view

NOTE This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

The <picker-view> component provides the view that shows an embedded scrollable selector on the screen.

Child Components

Not supported

Attributes

In addition to the universal attributes, the following attributes are supported.

NameTypeDefault ValueMandatoryDescription
typestringtextNoType of the scrollable selector, which cannot be changed dynamically. Available values are as follows:
- text: text selector.
- time: time selector.
- date: date selector.
- datetime: date and time selector.
- multi-text: multi-column text selector.

Text Selector

NameTypeDefault ValueMandatoryDescription
rangeArray-NoValue range of the text selector.
Use the data binding mode, for example, range = {{data}}. Declare the corresponding variable data: ["15", "20", "25"] in JavaScript.
selectedstring0NoDefault value of the text selector. The value is the index of range.
indicatorprefixstring-NoPrefix field added when a value is specified for the text selector.
indicatorsuffixstring-NoSuffix field added when a value is specified for the text selector.

Time Selector

NameTypeDefault ValueMandatoryDescription
containsecondbooleanfalseNoWhether seconds are contained.
selectedstringCurrent timeNoDefault value of the time selector, in the format of HH:mm.
If seconds are contained, the format is HH:mm:ss.
hoursnumber241-4
-5+
NoTime format used by the time selector. Available values are as follows:
- 12: displayed in 12-hour format and distinguished by a.m. and p.m.
- 24: displayed in 24-hour format.
Since API version 5, the default value is the most commonly-used hour format in the current locale.

Date Selector

NameTypeDefault ValueMandatoryDescription
start<time>1970-1-1NoStart date of the date selector, in the format of YYYY-MM-DD.
end<time>2100-12-31NoEnd date of the date selector, in the format of YYYY-MM-DD.
selectedstringCurrent dateNoDefault value of the date selector, in the format of YYYY-MM-DD.
lunar5+booleanfalseNoWhether the pop-up window displays the lunar calendar.
lunarswitchbooleanfalseNoWhether 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.

Date and Time Selector

NameTypeDefault ValueMandatoryDescription
selectedstringCurrent date and timeNoDefault 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.
hoursnumber241-4
-5+
NoTime format used by the date and time selector. Available values are as follows:
- 12: displayed in 12-hour format and distinguished by a.m. and p.m.
- 24: displayed in 24-hour format.
Since API version 5, the default value is the most commonly-used hour format in the current locale.
lunar5+booleanfalseNoWhether the pop-up window displays the lunar calendar.
lunarswitchbooleanfalseNoWhether 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.

Multi-Column Text Selector

NameTypeDefault ValueMandatoryDescription
columnsnumber-YesNumber of columns in the multi-column text selector.
rangeTwo-dimensional array-NoItems 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"]].
Use the data binding mode, for example, range = {{data}}. Declare the corresponding variable data: ["15", "20", "25"] in JavaScript.
selectedArray[0,0,0,…]NoDefault 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, the following styles are supported.

NameTypeDefault ValueMandatoryDescription
color<color>#ffffffNoFont color of a candidate item.
font-size<length>16pxNoFont size of a candidate item. The value is of the length type, in pixels.
selected-color<color>#ff0a69f7NoFont color of the selected item.
selected-font-size<length>20pxNoFont size of the selected item. The value is of the length type, in pixels.
disappear-color5+<color>#ffffffNoFont 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-size5+<length>14pxNoFont size of the items that gradually disappear. Disappearing items are the top option and bottom option of a column containing five options in total.
font-familystringsans-serifNoFont 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 is used for the text.

Events

The following events are supported.

Text Selector

NameParameterDescription
change{ newValue: newValue, newSelected: newSelected }Triggered when a value is specified for the text selector.

Time Selector

NameParameterDescription
change{ hour: hour, minute: minute, [second:second]}Triggered when a value is specified for the time selector.
If seconds are contained, the value contains hour, minute, and second.

Date Selector

NameParameterDescription
change{ year:year, month:month, day:day }Triggered when a value is specified for the date selector.

Date and Time Selector

NameParameterDescription
change{ year:year, month:month, day:day, hour:hour, minute:minute }Triggered when a value is specified for the date and time selector.

Multi-Column Text Selector

NameParameterDescription
columnchange{ column:column, newValue:newValue, newSelected:newSelected }Triggered when the value of a column in the multi-column selector changes.
column: column whose value has changed.
newValue: selected value.
newSelected: index of the selected value.

Methods

Not supported

Example

Text Selector

<!-- 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>
/* xxx.css */
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50%;
}
.title {
    font-size: 30px;
    text-align: center;
    margin-top: 50%;
}
/* 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;
    },
}

picker-view0

Time Selector

<!-- 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>
/* xxx.css */
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50%;
}
.title {
    font-size: 31px;
    text-align: center;
    margin-top: 50%;
}
/* 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)}`;
  },
}

picker-view1

Date Selector

<!-- xxx.hml -->
<div class="container">
    <text class="title">
        Selected: {{date}}
    </text>
    <picker-view class="time-picker" type="date" selected="{{defaultTime}}" @change="handleChange" lunarswitch="true"></picker-view>
</div>
/* xxx.css */
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50%;
}
.title {
    font-size: 31px;
    text-align: center;
    margin-top: 50%;
}
/* xxx.js */
export default {
    data: {
        date: "",
    },
    handleChange(data) {
        this.date = data.year + "/" + data.month + "/" + data.day + "";
    },
}

picker-view2

Date and Time Selector

<!-- 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>
/* xxx.css */
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50%;
}
.title {
    font-size: 31px;
    text-align: center;
    margin-top: 50%;
}
/* xxx.js */
export default {
    data: {
        datetime: "",
    },
    handleChange(data) {
        this.datetime = data.year + "/" + data.month + "/" + data.day + "" + data.hour + ":" + data.minute + "";
    },
}

picker-view3

Multi-Column Text Selector

<!-- 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>
/* xxx.css */
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50%;
}
.title {
    font-size: 31px;
    text-align: center;
    margin-top: 50%;
}
/* 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;
    },
}

picker-view4

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Full)

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/lzCy4R