“3aed77bc84013fced136977b7cc17fff60eddf7a”上不存在“drivers/git@gitcode.net:openanolis/cloud-kernel.git”
ts-methods-datepicker-dialog.md 3.3 KB
Newer Older
E
esterzhou 已提交
1 2
# Date Picker Dialog Box

E
ester.zhou 已提交
3 4 5 6
A date picker dialog box is a dialog box that allows users to select a date from the given range.

>  **NOTE**
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
E
esterzhou 已提交
8 9 10

## DatePickerDialog.show

E
ester.zhou 已提交
11
show(options?: DatePickerDialogOptions)
E
esterzhou 已提交
12

E
ester.zhou 已提交
13
Shows a date picker dialog box.
E
esterzhou 已提交
14

E
ester.zhou 已提交
15 16
- DatePickerDialogOptions
  | Name| Type| Mandatory| Default Value| Description|
E
esterzhou 已提交
17
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
18 19 20 21
  | start | Date | No| Date('1970-1-1') | Start date of the picker.|
  | end | Date | No| Date('2100-12-31') | End date of the picker.|
  | selected | Date | No| Current system date| Date of the selected item.|
  | lunar | boolean | No| false | Whether to display the lunar calendar.|
E
ester.zhou 已提交
22 23 24
  | onAccept | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult)) => void | No| - | Callback invoked when the OK button in the dialog box is clicked.|
  | onCancel | () => void | No| - | Callback invoked when the Cancel button in the dialog box is clicked.|
  | onChange | (value: [DatePickerResult](ts-basic-components-datepicker.md#DatePickerResult)) => void | No| - | Callback invoked when the selected item in the picker changes.|
E
esterzhou 已提交
25 26 27 28

## Example

### Date Picker Sample Code (With Lunar Calendar)
E
ester.zhou 已提交
29 30
```ts
// xxx.ets
E
esterzhou 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
@Entry
@Component
struct DatePickerDialogExample01 {
  @State isLunar: boolean = true
  selectedDate: Date = new Date("2000-1-1")

  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.Center }) {
      Button("DatePickerDialog").onClick(() => {
        DatePickerDialog.show({
          start: new Date("2000-1-1"),
          end: new Date("2005-1-1"),
          selected: this.selectedDate,
          lunar: this.isLunar,
          onAccept: (value: DatePickerResult) => {
E
ester.zhou 已提交
47
            this.selectedDate.setFullYear(value.year, value.month, value.day)
E
esterzhou 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
          },
          onCancel: () => {
            console.info("DatePickerDialog:onCancel()")
          },
          onChange: (value: DatePickerResult) => {
            console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
          }
        })
      })
    }
  }
}
```
### Date Picker Sample Code (No Lunar Calendar)
E
ester.zhou 已提交
63 64
```ts
// xxx.ets
E
esterzhou 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
@Entry
@Component
struct DatePickerDialogExample02 {
  @State isLunar: boolean = false
  selectedDate: Date = new Date("2000-1-1")

  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.Center }) {
      Button("DatePickerDialog").onClick(() => {
        DatePickerDialog.show({
          start: new Date("2000-1-1"),
          end: new Date("2005-1-1"),
          selected: this.selectedDate,
          lunar: this.isLunar,
          onAccept: (value: DatePickerResult) => {
E
ester.zhou 已提交
81
            this.selectedDate.setFullYear(value.year, value.month, value.day)
E
esterzhou 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
            console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
          },
          onCancel: () => {
            console.info("DatePickerDialog:onCancel()")
          },
          onChange: (value: DatePickerResult) => {
            console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
          }
        })
      })
    }
  }
}
```