ant-design-vue/components/form/demo/time-related-controls.vue

134 lines
3.4 KiB
Vue
Raw Normal View History

2018-05-06 13:53:00 +00:00
<cn>
#### 时间类控件
2018-12-08 11:24:30 +00:00
时间类组件的 `value` 类型为 `moment` 对象所以在提交服务器前需要预处理
2018-05-06 13:53:00 +00:00
</cn>
<us>
#### Time-related Controls
2018-12-08 11:24:30 +00:00
The `value` of time-related components is a `moment` object, which we need to pre-process it before we submit to server.
2018-05-06 13:53:00 +00:00
</us>
<template>
2019-02-01 09:23:00 +00:00
<a-form
:form="form"
@submit="handleSubmit"
>
2019-02-01 09:23:00 +00:00
<a-form-item
v-bind="formItemLayout"
label="DatePicker"
>
<a-date-picker v-decorator="['date-picker', config]" />
</a-form-item>
<a-form-item
v-bind="formItemLayout"
label="DatePicker[showTime]"
>
<a-date-picker
v-decorator="['date-time-picker', config]"
show-time
format="YYYY-MM-DD HH:mm:ss"
/>
</a-form-item>
<a-form-item
v-bind="formItemLayout"
label="MonthPicker"
>
<a-monthPicker v-decorator="['month-picker', config]" />
</a-form-item>
<a-form-item
v-bind="formItemLayout"
label="RangePicker"
>
<a-range-picker v-decorator="['range-picker', rangeConfig]" />
</a-form-item>
<a-form-item
v-bind="formItemLayout"
label="RangePicker[showTime]"
>
<a-range-picker
v-decorator="['range-time-picker', rangeConfig]"
show-time
format="YYYY-MM-DD HH:mm:ss"
/>
</a-form-item>
<a-form-item
v-bind="formItemLayout"
label="TimePicker"
>
<a-time-picker v-decorator="['time-picker', config]" />
</a-form-item>
<a-form-item
:wrapper-col="{
xs: { span: 24, offset: 0 },
sm: { span: 16, offset: 8 },
}"
>
<a-button
type="primary"
html-type="submit"
>
Submit
</a-button>
</a-form-item>
</a-form>
</template>
2018-05-06 13:53:00 +00:00
<script>
export default {
data () {
return {
formItemLayout: {
labelCol: {
xs: { span: 24 },
sm: { span: 8 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
},
},
config: {
rules: [{ type: 'object', required: true, message: 'Please select time!' }],
},
rangeConfig: {
rules: [{ type: 'array', required: true, message: 'Please select time!' }],
},
2019-01-12 03:33:27 +00:00
};
},
2019-02-01 09:23:00 +00:00
beforeCreate () {
2019-04-20 06:19:13 +00:00
this.form = this.$form.createForm(this, { name: 'time_related_controls' });
2019-02-01 09:23:00 +00:00
},
2018-05-06 13:53:00 +00:00
methods: {
handleSubmit (e) {
2019-01-12 03:33:27 +00:00
e.preventDefault();
2018-05-06 13:53:00 +00:00
this.form.validateFields((err, fieldsValue) => {
if (err) {
2019-01-12 03:33:27 +00:00
return;
2018-05-06 13:53:00 +00:00
}
// Should format date value before submit.
2019-01-12 03:33:27 +00:00
const rangeValue = fieldsValue['range-picker'];
const rangeTimeValue = fieldsValue['range-time-picker'];
2018-05-06 13:53:00 +00:00
const values = {
...fieldsValue,
'date-picker': fieldsValue['date-picker'].format('YYYY-MM-DD'),
'date-time-picker': fieldsValue['date-time-picker'].format('YYYY-MM-DD HH:mm:ss'),
'month-picker': fieldsValue['month-picker'].format('YYYY-MM'),
'range-picker': [rangeValue[0].format('YYYY-MM-DD'), rangeValue[1].format('YYYY-MM-DD')],
'range-time-picker': [
rangeTimeValue[0].format('YYYY-MM-DD HH:mm:ss'),
rangeTimeValue[1].format('YYYY-MM-DD HH:mm:ss'),
],
'time-picker': fieldsValue['time-picker'].format('HH:mm:ss'),
2019-01-12 03:33:27 +00:00
};
console.log('Received values of form: ', values);
});
2018-05-06 13:53:00 +00:00
},
},
2019-01-12 03:33:27 +00:00
};
2018-05-06 13:53:00 +00:00
</script>
2018-05-08 03:20:07 +00:00
2018-05-06 13:53:00 +00:00