Skip to content

DatePicker

选择年月日

html

<pa-date-picker v-model="dateValue" title="选择年月日" @confirm="onConfirm" />
ts

import dayjs from 'dayjs'
import { ref } from 'vue'

const dateValue = ref<Date>(new Date())

const onConfirm = (date: Date) => {
  const newVal = dayjs(date)
  uni.showToast({
    icon: 'none',
    title: `当前选中的值是:${newVal.format('YYYY-MM-DD HH:mm:ss')}`,
  })
}

配合Popup使用

html

<pa-cell title="选择年月日" :value="dateText" clickable @click="datePickerShow = true" />
<pa-popup v-model:show="datePickerShow" position="bottom">
  <pa-date-picker title="选择年月日" @confirm="onConfirm" />
</pa-popup>
ts

import dayjs from 'dayjs'
import { ref } from 'vue'

const datePickerShow = ref<boolean>(false)
const dateText = ref<string>('')

const onConfirm = (date: Date) => {
  const newVal = dayjs(date)
  dateText.value = newVal.format('YYYY-MM-DD')
  uni.showToast({
    icon: 'none',
    title: `当前选中的值是:${newVal.format('YYYY-MM-DD HH:mm:ss')}`,
  })
}

选择年月

html

<pa-date-picker title="选择年月" :columns-type="['year', 'month']" :formatter="formatter" />
ts

import type { DatePickerColumnType } from '../..'

const formatter = (type: DatePickerColumnType, value: string) => {
  if (type === 'year') {
    return `${value}年`
  } else if (type === 'month') {
    return `${value}月`
  } else if (type === 'day') {
    return `${value}日`
  }
  return value
}

选择月日

html

<pa-date-picker title="选择月日" :columns-type="['month', 'day']" :formatter="formatter" />
ts

import type { DatePickerColumnType } from '../..'

const formatter = (type: DatePickerColumnType, value: string) => {
  if (type === 'year') {
    return `${value}年`
  } else if (type === 'month') {
    return `${value}月`
  } else if (type === 'day') {
    return `${value}日`
  }
  return value
}

选择时间

html

<pa-date-picker title="选择时间" :columns-type="['hour', 'minute']" />

选择完整时间

html

<pa-date-picker title="选择完整时间" :columns-type="['year', 'month', 'day', 'hour', 'minute']" />

选择年月日小时

html

<pa-date-picker title="选择年月日小时" :columns-type="['year', 'month', 'day', 'hour']" />

显示列标题

html

<pa-date-picker v-model="dateValue" title="选择年月日" show-columns-header @confirm="onConfirm" />
ts

import dayjs from 'dayjs'
import { ref } from 'vue'

const dateValue = ref<Date>(new Date())

const onConfirm = (date: Date) => {
  const newVal = dayjs(date)
  uni.showToast({
    icon: 'none',
    title: `当前选中的值是:${newVal.format('YYYY-MM-DD HH:mm:ss')}`,
  })
}

选项过滤器

html

<pa-date-picker title="选项过滤器" :columns-type="['hour', 'minute']" :filter="filter" />
ts

import type { DatePickerColumnType, DatePickerOption } from '../..'

const filter = (type: DatePickerColumnType, options: DatePickerOption[]) => {
  if (type === 'minute') {
    return options.filter((option) => option.value % 5 === 0)
  }
  return options
}

选择顺序控制

html

<pa-date-picker
  title="选择顺序控制"
  :columns-type="['day', 'month', 'year']"
  :min-date="minDate"
  :max-date="maxDate"
/>
ts

/**
 * @description columns-type 值的顺序即为选择的顺序
 */
import dayjs from 'dayjs'

const minDate = dayjs().subtract(10, 'year').toDate()
const maxDate = dayjs().add(10, 'year').toDate()

DatePicker Props

参数说明类型默认值
modelValuedate-
columnsType选项类型,由选项组成数组,数据顺序代表排序顺序DatePickerColumnType[]-
minDate可选的最小时间,精确到分date-
maxDate可选的最大时间,精确到分date-
title顶部栏标题string''
showToolbar是否显示顶部栏booleantrue
showColumnsHeader是否显示列标题栏boolean-
columnsHeader列标题名称string[]|((types:DatePickerColumnType[])=>string[])-
confirmButtonText确认按钮文字string'确认'
cancelButtonText取消按钮文字string'取消'
optionHeight选项高度number44
visibleOptionNum可见选项个数number6
formatter选项格式化函数DatePickerFormatter-
filter选项过滤函数DatePickerFilter-

DatePicker Event

事件名参数
update:modelValue(value: Date)
change(value: Date)
confirm(value: Date)
cancel()

DatePicker Slot

名称说明
columns-top-
default-
default-

样式变量

🙈