ant-design-vue/components/form/demo/dynamic-form-item.vue

141 lines
3.3 KiB
Vue
Raw Normal View History

2018-05-06 10:32:40 +00:00
<cn>
#### 动态增减表单项
动态增加减少表单项
</cn>
<us>
#### Dynamic Form Item
Add or remove form items dynamically.
</us>
<template>
2019-09-28 12:45:07 +00:00
<a-form :form="form" @submit="handleSubmit">
2019-02-01 09:23:00 +00:00
<a-form-item
v-for="(k, index) in form.getFieldValue('keys')"
:key="k"
v-bind="index === 0 ? formItemLayout : formItemLayoutWithOutLabel"
:label="index === 0 ? 'Passengers' : ''"
:required="false"
>
<a-input
v-decorator="[
`names[${k}]`,
{
validateTrigger: ['change', 'blur'],
2019-09-28 12:45:07 +00:00
rules: [
{
required: true,
whitespace: true,
message: 'Please input passenger\'s name or delete this field.',
},
],
},
2019-02-01 09:23:00 +00:00
]"
placeholder="passenger name"
style="width: 60%; margin-right: 8px"
/>
<a-icon
v-if="form.getFieldValue('keys').length > 1"
class="dynamic-delete-button"
type="minus-circle-o"
:disabled="form.getFieldValue('keys').length === 1"
@click="() => remove(k)"
/>
</a-form-item>
<a-form-item v-bind="formItemLayoutWithOutLabel">
2019-09-28 12:45:07 +00:00
<a-button type="dashed" style="width: 60%" @click="add">
2019-02-01 09:23:00 +00:00
<a-icon type="plus" /> Add field
</a-button>
</a-form-item>
<a-form-item v-bind="formItemLayoutWithOutLabel">
2019-09-28 12:45:07 +00:00
<a-button type="primary" html-type="submit">
2019-02-01 09:23:00 +00:00
Submit
</a-button>
</a-form-item>
</a-form>
</template>
2018-05-06 10:32:40 +00:00
<script>
2019-01-12 03:33:27 +00:00
let id = 0;
export default {
2019-09-28 12:45:07 +00:00
data() {
return {
formItemLayout: {
labelCol: {
xs: { span: 24 },
sm: { span: 4 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 20 },
},
},
formItemLayoutWithOutLabel: {
wrapperCol: {
xs: { span: 24, offset: 0 },
sm: { span: 20, offset: 4 },
},
},
2019-01-12 03:33:27 +00:00
};
},
2019-09-28 12:45:07 +00:00
beforeCreate() {
2019-04-20 06:19:13 +00:00
this.form = this.$form.createForm(this, { name: 'dynamic_form_item' });
2019-02-01 09:23:00 +00:00
this.form.getFieldDecorator('keys', { initialValue: [], preserve: true });
},
2018-05-06 10:32:40 +00:00
methods: {
2019-09-28 12:45:07 +00:00
remove(k) {
2019-01-12 03:33:27 +00:00
const { form } = this;
2018-05-06 10:32:40 +00:00
// can use data-binding to get
2019-01-12 03:33:27 +00:00
const keys = form.getFieldValue('keys');
2018-05-06 10:32:40 +00:00
// We need at least one passenger
if (keys.length === 1) {
2019-01-12 03:33:27 +00:00
return;
2018-05-06 10:32:40 +00:00
}
// can use data-binding to set
form.setFieldsValue({
keys: keys.filter(key => key !== k),
2019-01-12 03:33:27 +00:00
});
2018-05-06 10:32:40 +00:00
},
2019-09-28 12:45:07 +00:00
add() {
2019-01-12 03:33:27 +00:00
const { form } = this;
2018-05-06 10:32:40 +00:00
// can use data-binding to get
2019-01-12 03:33:27 +00:00
const keys = form.getFieldValue('keys');
2019-04-20 06:19:13 +00:00
const nextKeys = keys.concat(id++);
2018-05-06 10:32:40 +00:00
// can use data-binding to set
// important! notify form to detect changes
form.setFieldsValue({
keys: nextKeys,
2019-01-12 03:33:27 +00:00
});
2018-05-06 10:32:40 +00:00
},
2019-09-28 12:45:07 +00:00
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
},
},
2019-01-12 03:33:27 +00:00
};
2018-05-06 10:32:40 +00:00
</script>
<style>
.dynamic-delete-button {
cursor: pointer;
position: relative;
top: 4px;
font-size: 24px;
color: #999;
2019-09-28 12:45:07 +00:00
transition: all 0.3s;
2018-05-06 10:32:40 +00:00
}
.dynamic-delete-button:hover {
color: #777;
}
.dynamic-delete-button[disabled] {
cursor: not-allowed;
opacity: 0.5;
}
</style>