在用antd的时候,咱们若是要对表单进行添加和删除该怎么弄呢,以下:css
import { connect } from 'dva'; import { Form, Input, Button } from 'antd'; import styles from './eg1.css'; const FormItem = Form.Item; function Page(props) { const { form } = props; const { getFieldDecorator, getFieldValue } = form // 表单提交 const handleSubmit = (e) => { e.preventDefault(); form.validateFields((err, values) => { if (!err) { console.log(values); } }); } // 添加 const add = () => { const list = form.getFieldValue('list'); const nextList = list.concat({}); form.setFieldsValue({ list: nextList, }); } // 删除 const deleteRow = (index) => { const list = form.getFieldValue('list'); const content = form.getFieldValue('content'); if (list.length === 1) { return; } form.setFieldsValue({ list: list.filter((item, key) => key !== index), content: content.filter((item, key) => key !== index), }); } getFieldDecorator('list', { initialValue: [{}] }); const list = getFieldValue('list'); const listContent = list.map((item, index) => { return ( <FormItem label='名称:' key={index}> {getFieldDecorator(`content[${index}].name`, { rules: [{ required: true, message: "名称不能为空!", }], })( <Input placeholder="请输入名称" style={{ width: '60%', marginRight: 8 }} /> )} {index > 0 ? ( <Button type="primary" onClick={deleteRow.bind(this, index)}>删除</Button> ) : null} </FormItem> ); }); return ( <div className={styles.normal}> <Form onSubmit={handleSubmit}> {listContent} <FormItem> <Button type="primary" htmlType="submit">提交</Button> <Button type="primary" style={{ marginLeft: '10px' }} onClick={add}>增长</Button> </FormItem> </Form> </div> ); } const page = Form.create()(Page); export default connect()(page);
这里不只能对表单进行增长和删除,还能对表单进行验证,看是否有输入,以上是自己没有数据的状况,若是是有数据的状况以下:html
import React from 'react'; import { connect } from 'dva'; import { Form, Input, Button } from 'antd'; import styles from './eg2.css'; const FormItem = Form.Item; function Page(props) { const { form } = props; const { getFieldDecorator, getFieldValue } = form // 表单提交 const handleSubmit = (e) => { e.preventDefault(); form.validateFields((err, values) => { if (!err) { console.log(values); } }); } // 添加 const add = () => { const list = form.getFieldValue('list'); const nextList = list.concat({}); form.setFieldsValue({ list: nextList, }); } // 删除 const deleteRow = (index) => { const list = form.getFieldValue('list'); const content = form.getFieldValue('content'); if (list.length === 1) { return; } form.setFieldsValue({ list: list.filter((item, key) => key !== index), content: content.filter((item, key) => key !== index), }); } const slist = [{ id:'0001', name: '黎明' }, { id:'0002', name: '晴天' }] getFieldDecorator('list', { initialValue: slist }); const list = getFieldValue('list'); const listContent = list.map((item, index) => { getFieldDecorator(`content[${index}].id`, {initialValue: item.id || ''}) return ( <FormItem label='名称:' key={index}> {getFieldDecorator(`content[${index}].name`, { rules: [{ required: true, message: "名称不能为空!", }], initialValue: item.name || '' })( <Input placeholder="请输入名称" style={{ width: '60%', marginRight: 8 }} /> )} {index > 0 ? ( <Button type="primary" onClick={deleteRow.bind(this, index)}>删除</Button> ) : null} </FormItem> ); }); return ( <div className={styles.normal}> <Form onSubmit={handleSubmit}> {listContent} <FormItem> <Button type="primary" htmlType="submit">提交</Button> <Button type="primary" style={{ marginLeft: '10px' }} onClick={add}>增长</Button> </FormItem> </Form> </div> ); } const page = Form.create()(Page); export default connect()(page);
通常若是自己有数据,都会有每行数据的id,可是这个id不显示,咱们都会用getFieldDecorator给id声明,这样在咱们提交表单的时候,就能够获得表单抓取到id的数据,有数据跟没有数据的差异就是,有数据须要在表单getFieldDecorator的时候给一个初始值,其余二者都同样react
具体代码下载地址:https://gitee.com/hope93/antd...git