ant-design-vue/components/form/demo/coordinated.vue

94 lines
1.9 KiB
Vue
Raw Normal View History

2018-05-06 10:32:40 +00:00
<cn>
#### 表单联动
使用 `setFieldsValue` 来动态设置其他控件的值
</cn>
<us>
#### Coordinated Controls
Use `setFieldsValue` to set other control's value programmaticly.
</us>
2018-05-08 03:20:07 +00:00
2018-06-23 09:17:45 +00:00
<template>
2019-02-01 09:23:00 +00:00
<a-form
:form="form"
@submit="handleSubmit"
>
2018-06-23 09:17:45 +00:00
<a-form-item
2019-02-01 09:23:00 +00:00
label="Note"
:label-col="{ span: 5 }"
:wrapper-col="{ span: 12 }"
2018-06-23 09:17:45 +00:00
>
<a-input
v-decorator="[
'note',
{rules: [{ required: true, message: 'Please input your note!' }]}
]"
/>
2018-06-23 09:17:45 +00:00
</a-form-item>
<a-form-item
2019-02-01 09:23:00 +00:00
label="Gender"
:label-col="{ span: 5 }"
:wrapper-col="{ span: 12 }"
2018-06-23 09:17:45 +00:00
>
<a-select
v-decorator="[
'gender',
{rules: [{ required: true, message: 'Please select your gender!' }]}
]"
2019-02-01 09:23:00 +00:00
placeholder="Select a option and change input text above"
@change="handleSelectChange"
2018-06-23 09:17:45 +00:00
>
2019-02-01 09:23:00 +00:00
<a-select-option value="male">
male
</a-select-option>
<a-select-option value="female">
female
</a-select-option>
2018-06-23 09:17:45 +00:00
</a-select>
</a-form-item>
<a-form-item
2019-02-01 09:23:00 +00:00
:wrapper-col="{ span: 12, offset: 5 }"
2018-06-23 09:17:45 +00:00
>
2019-02-01 09:23:00 +00:00
<a-button
type="primary"
html-type="submit"
>
2018-06-23 09:17:45 +00:00
Submit
</a-button>
</a-form-item>
</a-form>
</template>
2018-05-06 10:32:40 +00:00
2018-06-23 09:17:45 +00:00
<script>
export default {
data () {
return {
formLayout: 'horizontal',
2019-04-20 06:19:13 +00:00
form: this.$form.createForm(this, { name: 'coordinated' }),
2019-01-12 03:33:27 +00:00
};
2018-06-23 09:17:45 +00:00
},
2018-05-06 10:32:40 +00:00
methods: {
handleSubmit (e) {
2019-01-12 03:33:27 +00:00
e.preventDefault();
2018-05-06 10:32:40 +00:00
this.form.validateFields((err, values) => {
if (!err) {
2019-01-12 03:33:27 +00:00
console.log('Received values of form: ', values);
2018-05-06 10:32:40 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-05-06 10:32:40 +00:00
},
handleSelectChange (value) {
2019-01-12 03:33:27 +00:00
console.log(value);
2018-05-06 10:32:40 +00:00
this.form.setFieldsValue({
note: `Hi, ${value === 'male' ? 'man' : 'lady'}!`,
2019-01-12 03:33:27 +00:00
});
2018-05-06 10:32:40 +00:00
},
},
2019-01-12 03:33:27 +00:00
};
2018-05-06 10:32:40 +00:00
</script>
2018-05-08 03:20:07 +00:00
2018-05-06 10:32:40 +00:00
2018-06-23 09:17:45 +00:00