首页 > 编程语言 > ant design vue中日期选择框混合时间选择器的用法说明
2020
10-28

ant design vue中日期选择框混合时间选择器的用法说明

首先时间格式化用到moment方法,需要在页面中引入moment组件

import moment from 'moment'

结构代码:

<a-date-picker
   style="width:100%"
   :getCalendarContainer="(triggerNode) => triggerNode.parentNode"
   format="YYYY-MM-DD HH:mm:ss"
   v-decorator="[
   'pushtime',
   {
   rules: [{ required: true, message: '请输入发布时间!' }]
   }
   ]"
   :showTime="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
   :disabledDate="disabledDate"
   :disabledDateTime="disabledDateTime"
   placeholder="请选择时间"
   @change="onChange"
   @ok="onOk" />

其中,showTime.defaultValue是设置的默认展示时间,disabledDate为禁用日期,disabledDataTime为禁用时间,详细属性说明可查阅官方文档

下面是方法代码:

methods: {
 moment,
 onChange (value, dateString) {
 console.log('Selected Time: ', value)
 console.log('Formatted Selected Time: ', dateString)
 },
 onOk (value) {
 console.log('onOk: ', value)
 },
 range (start, end) {
 const result = []
 for (let i = start; i < end; i++) {
 result.push(i)
 }
 return result
 },
 disabledDate (current) {
 // Can not select days before today and today
 return current && current < moment().endOf('day')
 },
 
 disabledDateTime () {
 return {
 disabledHours: () => this.range(0, 24).splice(4, 20),
 disabledMinutes: () => this.range(30, 60),
 disabledSeconds: () => [55, 56]
 }
 }
}

补充知识:初始化antDesign RangePicker默认选择日期及限制日期可选范围

主要做了两个设置:

1、初始化默认选择日期;

2、限制日期可选范围(限制最大可选范围是最近6个月)

具体实现代码出下:

import React, { PureComponent } from 'react';
import moment from 'moment';
import { 
 Form, 
 Modal,
 DatePicker,
} from 'antd';
 
const FormItem = Form.Item;
const { RangePicker } = DatePicker;
 
@Form.create()
class ExportModal extends PureComponent {
 
 // 表单提交
 okHandle = () => {
 const { handleExportByTime, form } = this.props;
 form.validateFields((err, fieldsValue) => {
 const rangeValue = fieldsValue['range-picker'];
 if (err) return;
 const values ={
 ...fieldsValue,
 'date': [rangeValue[0].format('YYYY-MM-DD'), rangeValue[1].format('YYYY-MM-DD')],
 }
 // 重置表单
 form.resetFields();
 handleExportByTime(values);
 });
 };
 
 // 不可选择的时间段
 disabledDate = current => current && current > moment().endOf('day') || current < moment().subtract(6, 'months');
 
 render() {
 const {
 form: { getFieldDecorator },
 handleModalVisible,
 submitting,
 modalVisible,
 } = this.props;
 
 const formItemLayout = {
 labelCol: { span: 4 },
 wrapperCol: { span: 14 }
 };
 
 // 初始化日期显示 
 const defaultSelectDate = {
 startDate: moment().subtract(1, 'weeks'),
 endDate: moment().endOf('day')
 }
 
 return (
 <Modal
 destroyOnClose
 title='按时间段导出'
 centered
 keyboard={false}
 maskClosable={false}
 visible={modalVisible}
 confirmLoading={submitting}
 onOk={this.okHandle}
 onCancel={() => handleModalVisible()}
 >
 <FormItem {...formItemLayout} label='时间段' extra='最长可导出最近6个月数据'>
  {getFieldDecorator('range-picker',{
  initialValue: [defaultSelectDate.startDate, defaultSelectDate.endDate]
  })(
  <RangePicker disabledDate={this.disabledDate} />
  )}
 </FormItem>
 </Modal>
 );
 }
}
export default ExportModal;

以上这篇ant design vue中日期选择框混合时间选择器的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧