## 创建修改页
1. 修改页元素与新增差不多,不同的是修改页面需要在组件加载的时候获取数据,从而绑定在页面上
2. 将上一节的新增页代码过来稍作修改
~~~
import React, { PureComponent } from 'react';
import router from 'umi/router';
import { Form, Input, Card, Button, message, DatePicker } from 'antd';
import moment from 'moment';
import Panel from '../../../components/Panel';
import styles from '../../../layouts/Sword.less';
import { submit } from '../../../services/demo';
import func from '../../../utils/Func';
const FormItem = Form.Item;
const { TextArea } = Input;
@Form.create()
class DemoEdit extends PureComponent {
handleSubmit = e => {
e.preventDefault();
const { form } = this.props;
form.validateFieldsAndScroll((err, values) => {
if (!err) {
const params = {
...values,
date: func.format(values.date),
};
submit(params).then(resp => {
if (resp.success) {
message.success('提交成功');
router.push('/platform/demo');
} else {
message.warn(resp.msg);
}
});
}
});
};
render() {
const {
form: { getFieldDecorator },
} = this.props;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};
const action = (
<Button type="primary" onClick={this.handleSubmit}>
提交
</Button>
);
return (
<Panel title="修改" back="/platform/demo" action={action}>
<Form hideRequiredMark style={{ marginTop: 8 }}>
<Card title="基本信息" className={styles.card} bordered={false}>
<FormItem {...formItemLayout} label="标题">
{getFieldDecorator('title', {
rules: [
{
required: true,
message: '请输入标题',
},
],
})(<Input placeholder="请输入标题" />)}
</FormItem>
<FormItem {...formItemLayout} label="日期">
{getFieldDecorator('date', {
rules: [
{
required: true,
message: '请输入日期',
},
],
})(
<DatePicker
style={{ width: '100%' }}
format="YYYY-MM-DD HH:mm:ss"
disabledDate={this.disabledDate}
showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
/>
)}
</FormItem>
<FormItem {...formItemLayout} label="内容">
{getFieldDecorator('content')(
<TextArea style={{ minHeight: 32 }} placeholder="请输入内容" rows={10} />
)}
</FormItem>
</Card>
</Form>
</Panel>
);
}
}
export default DemoEdit;
~~~
3. 到路由配置文件`router.config.js`增加路径
![](https://box.kancloud.cn/fca8ef23717aeb5a05a70d3f0af690e8_1432x544.png)
## 对接接口
1. 修改detail方法返回,模拟真实场景
~~~
function getFakeDetail(req, res) {
const json = { code: 200, success: true, msg: '操作成功' };
if (req.query.id === '1') {
json.data = {
id: '1',
title: '测试标题1',
content: '测试内容1',
date: '2018-05-08 12:00:00',
};
} else if (req.query.id === '2') {
json.data = {
id: '2',
title: '测试标题2',
content: '测试内容2',
date: '2018-06-08 12:00:00',
};
} else {
json.data = {
id: '3',
title: '测试标题3',
content: '测试内容3',
date: '2018-07-08 12:00:00',
};
}
return res.json(json);
}
~~~
2. 增加接口对接,因为涉及到UI数据绑定,所以需要用到state
~~~
state = {
data: {},
};
componentWillMount() {
const {
match: {
params: { id },
},
} = this.props;
detail({ id }).then(resp => {
if (resp.success) {
this.setState({ data: resp.data });
}
});
}
~~~
3. 页面跳转的时候可以通过props中的match获取url参数,主要获取方式为
~~~
const {
match: {
params: { id },
},
} = this.props;
~~~
4. 为三个组件增加数据绑定,需要注意的是,日期类型需要做下格式化才能初始化成功 `initialValue: moment(data.date, 'YYYY-MM-DD HH:mm:ss')`
~~~
{getFieldDecorator('title', {
rules: [
{
required: true,
message: '请输入标题',
},
],
initialValue: data.title,
})(<Input placeholder="请输入标题" />)}
~~~
~~~
{getFieldDecorator('date', {
rules: [
{
required: true,
message: '请输入日期',
},
],
initialValue: moment(data.date, 'YYYY-MM-DD HH:mm:ss'),
})(
<DatePicker
style={{ width: '100%' }}
format="YYYY-MM-DD HH:mm:ss"
showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
/>
)}
~~~
~~~
{getFieldDecorator('content', {
initialValue: data.content,
})(<TextArea style={{ minHeight: 32 }} placeholder="请输入内容" rows={10} />)}
~~~
5. 因为修改的时候,需要同时传递id,所以提交方法也得做如下修改
~~~
handleSubmit = e => {
e.preventDefault();
const {
form,
match: {
params: { id },
},
} = this.props;
form.validateFieldsAndScroll((err, values) => {
if (!err) {
const params = {
id,
...values,
date: func.format(values.date),
};
submit(params).then(resp => {
if (resp.success) {
message.success('提交成功');
router.push('/platform/demo');
} else {
message.warn(resp.msg);
}
});
}
});
};
~~~
6. 打开系统,点击修改查看数据显示成功
![](https://box.kancloud.cn/f9a406398e0887b31d820145717dd6b2_2512x1420.png)
7. 点击返回再次点击`测试标题2`的修改,发现数据也同步变化
![](https://box.kancloud.cn/3fe1e3dfcc9cc1cf04a85201b688a485_2510x1400.png)
8. 点击提交查看控制台,数据也传递正确
![](https://box.kancloud.cn/d918456a1bd63793ad8b72b275d6464e_1986x1134.png)
## 全部代码一览
~~~
import React, { PureComponent } from 'react';
import router from 'umi/router';
import { Form, Input, Card, Button, message, DatePicker } from 'antd';
import moment from 'moment';
import Panel from '../../../components/Panel';
import styles from '../../../layouts/Sword.less';
import { submit, detail } from '../../../services/demo';
import func from '../../../utils/Func';
const FormItem = Form.Item;
const { TextArea } = Input;
@Form.create()
class DemoEdit extends PureComponent {
state = {
data: {},
};
componentWillMount() {
const {
match: {
params: { id },
},
} = this.props;
detail({ id }).then(resp => {
if (resp.success) {
this.setState({ data: resp.data });
}
});
}
handleSubmit = e => {
e.preventDefault();
const {
form,
match: {
params: { id },
},
} = this.props;
form.validateFieldsAndScroll((err, values) => {
if (!err) {
const params = {
id,
...values,
date: func.format(values.date),
};
submit(params).then(resp => {
if (resp.success) {
message.success('提交成功');
router.push('/platform/demo');
} else {
message.warn(resp.msg);
}
});
}
});
};
render() {
const {
form: { getFieldDecorator },
} = this.props;
const { data } = this.state;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};
const action = (
<Button type="primary" onClick={this.handleSubmit}>
提交
</Button>
);
return (
<Panel title="修改" back="/platform/demo" action={action}>
<Form hideRequiredMark style={{ marginTop: 8 }}>
<Card title="基本信息" className={styles.card} bordered={false}>
<FormItem {...formItemLayout} label="标题">
{getFieldDecorator('title', {
rules: [
{
required: true,
message: '请输入标题',
},
],
initialValue: data.title,
})(<Input placeholder="请输入标题" />)}
</FormItem>
<FormItem {...formItemLayout} label="日期">
{getFieldDecorator('date', {
rules: [
{
required: true,
message: '请输入日期',
},
],
initialValue: moment(data.date, 'YYYY-MM-DD HH:mm:ss'),
})(
<DatePicker
style={{ width: '100%' }}
format="YYYY-MM-DD HH:mm:ss"
showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }}
/>
)}
</FormItem>
<FormItem {...formItemLayout} label="内容">
{getFieldDecorator('content', {
initialValue: data.content,
})(<TextArea style={{ minHeight: 32 }} placeholder="请输入内容" rows={10} />)}
</FormItem>
</Card>
</Form>
</Panel>
);
}
}
export default DemoEdit;
~~~