Skip to content

Commit

Permalink
11. Handle user edit
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 19, 2016
1 parent c387783 commit ed7a4ee
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/components/Users/UserModal.css
@@ -0,0 +1,3 @@

.normal {
}
97 changes: 97 additions & 0 deletions src/components/Users/UserModal.js
@@ -0,0 +1,97 @@
import React, { Component } from 'react';
import { Modal, Form, Input } from 'antd';
import styles from './UserModal.css';

const FormItem = Form.Item;

class UserEditModal extends Component {

constructor(props) {
super(props);
this.state = {
visible: false,
};
}

showModelHandler = (e) => {

This comment has been minimized.

Copy link
@eoeac

eoeac Apr 2, 2017

这里命名好像有点问题,应该是showModalHandler吧?后面也有类似的情况

This comment has been minimized.

Copy link
@sorrycc

sorrycc Apr 2, 2017

Author Member

是的,写错了。

if (e) e.stopPropagation();

This comment has been minimized.

Copy link
@rockallite

rockallite May 19, 2017

此处应为 e.preventDefault() 而不是 e.stopPropagation(),否则点击 Edit 链接后,页面会跳转而无法弹窗。(经桌面版 Chrome 和 Safari 测试过)

This comment has been minimized.

Copy link
@poyiding

poyiding Jun 25, 2017

是的,还有通常a标签不跳转不加href,可避免类似问题

This comment has been minimized.

Copy link
@hangyangws

hangyangws Nov 17, 2017

只有我觉得很是奇怪么:
明明可以用 span 标签 或者 button 标签,非要用 a 标签。

This comment has been minimized.

Copy link
@vivizhou0596

vivizhou0596 Apr 27, 2018

同感啊,第一次看那段代码就觉得应该用button啊,还以为用 a 是为了后面什么功能哦,但是好像实际没什么用

this.setState({
visible: true,
});
};

hideModelHandler = () => {
this.setState({
visible: false,
});
};

okHandler = () => {
const { onOk } = this.props;
this.props.form.validateFields((err, values) => {
if (!err) {
onOk(values);
this.hideModelHandler();
}
});
};

render() {
const { children } = this.props;
const { getFieldDecorator } = this.props.form;
const { name, email, website } = this.props.record;
const formItemLayout = {
labelCol: { span: 6 },
wrapperCol: { span: 14 },
};

return (
<span>
<span onClick={this.showModelHandler}>

This comment has been minimized.

Copy link
@YoFoon

YoFoon Apr 26, 2017

这里应该要用 this.showModalHandle.bind(this)
同下面的 this.hideModalHandler.bind(this)

This comment has been minimized.

Copy link
@joket1999

joket1999 May 5, 2017

@YoFoon 没错啊,不用bind且不能bind,因为这是箭头函数

{ children }
</span>
<Modal
title="Edit User"
visible={this.state.visible}
onOk={this.okHandler}
onCancel={this.hideModelHandler}
>
<Form horizontal onSubmit={this.okHandler}>

This comment has been minimized.

Copy link
@BoyInWindows

BoyInWindows Jan 4, 2018

报错Warning: Unknown prop 'horizontal' on <form> tag.
antd文档说:默认为horizontal所以删掉或这么写layout = 'horizontal'

<FormItem
{...formItemLayout}
label="Name"
>
{
getFieldDecorator('name', {
initialValue: name,
})(<Input />)
}
</FormItem>
<FormItem
{...formItemLayout}
label="Email"
>
{
getFieldDecorator('email', {
initialValue: email,
})(<Input />)
}
</FormItem>
<FormItem
{...formItemLayout}
label="Website"
>
{
getFieldDecorator('website', {
initialValue: website,
})(<Input />)
}
</FormItem>
</Form>
</Modal>
</span>
);
}
}

export default Form.create()(UserEditModal);
16 changes: 13 additions & 3 deletions src/components/Users/Users.js
Expand Up @@ -4,6 +4,7 @@ import { Table, Pagination, Popconfirm } from 'antd';
import { routerRedux } from 'dva/router';
import styles from './Users.css';
import { PAGE_SIZE } from '../../constants';
import UserModal from './UserModal';

function Users({ dispatch, list: dataSource, loading, total, page: current }) {
function deleteHandler(id) {
Expand All @@ -20,6 +21,13 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
}));
}

function editHandler(id, values) {
dispatch({
type: 'users/patch',
payload: { id, values },
});
}

const columns = [
{
title: 'Name',
Expand All @@ -40,10 +48,12 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
{
title: 'Operation',
key: 'operation',
render: (text, { id }) => (
render: (text, record) => (
<span className={styles.operation}>
<a href="">Edit</a>
<Popconfirm title="Confirm to delete?" onConfirm={deleteHandler.bind(null, id)}>
<UserModal record={record} onOk={editHandler.bind(null, record.id)}>
<a>Edit</a>
</UserModal>
<Popconfirm title="Confirm to delete?" onConfirm={deleteHandler.bind(null, record.id)}>
<a href="">Delete</a>
</Popconfirm>
</span>
Expand Down
5 changes: 5 additions & 0 deletions src/models/users.js
Expand Up @@ -29,6 +29,11 @@ export default {
const page = yield select(state => state.users.page);
yield put({ type: 'fetch', payload: { page } });
},
*patch({ payload: { id, values } }, { call, put, select }) {
yield call(usersService.patch, id, values);
const page = yield select(state => state.users.page);
yield put({ type: 'fetch', payload: { page } });
},
},
subscriptions: {
setup({ dispatch, history }) {
Expand Down
7 changes: 7 additions & 0 deletions src/services/users.js
Expand Up @@ -10,3 +10,10 @@ export function remove(id) {
method: 'DELETE',
});
}

export function patch(id, values) {
return request(`/api/users/${id}`, {
method: 'PATCH',
body: JSON.stringify(values),

This comment has been minimized.

Copy link
@yftan

yftan Sep 10, 2017

这个地方加上headers headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},可以解决用户编辑不生效的问题

This comment has been minimized.

Copy link
@skddyj

skddyj Oct 27, 2017

在哪里加啊?

This comment has been minimized.

Copy link
@cronaldoyang

cronaldoyang Sep 28, 2018

就在src/services/users.js的patch方法中加

});
}

8 comments on commit ed7a4ee

@ad50461354
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请教一下大神:

  • 需要注意的一点是,我们在这里如何处理 Modal 的 visible 状态,有几种选择:
  • 存 dva 的 model state 里
  • 存 component state 里
  • 另外,怎么存也是个问题,可以:
  • 只有一个 visible,然后根据用户点选的 user 填不同的表单数据
  • 几个 user 几个 visible
  • 此教程选的方案是 2-2,即存 component state,并且 visible 按 user 存。另外为了使用的简便,封装了一个 UserModal 的组件。
  • 完成后,切换到浏览器,应该就能对用户进行编辑了。

这段话中的“只有一个 visible,然后根据用户点选的 user 填不同的表单数据”和“几个 user 几个 visible”怎么理解?分别的意义是什么?在代码中哪一块体现了几个user几个visible的实现?

谢谢。 @sorrycc

@yangdonglai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ad50461354

  • “只有一个 visible,然后根据用户点选的 user 填不同的表单数据”

只有一个 modal 实例,多条记录共用一个 modal 实例

  • “几个 user 几个 visible”

多个 modal 实例,每条记录会产生单独的一个 modal 实例

@no13bus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

觉得2-2这种实现模态框展示和隐藏的方式比较好写。
现在公司有用1-1的方式的,组件-action-effect来回跳转,比较麻烦,虽然这样看来各个状态都在dva里面的model内进行管理,但是不免有些繁琐

@wkm-wangZhe
Copy link

@wkm-wangZhe wkm-wangZhe commented on ed7a4ee Oct 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot read property 'id' of undefined 改动后开始报这个错误 很不理解 求讲解

@jessiex
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wkm-wangZhe 你可能是把 render: (text, record) => ( 写成了 render: (text, {record}) => (

@Jaivenj
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我想问如何使编辑生效,现在编辑完后无法修改,还是原来的数据

@ensignwy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我想问如何使编辑生效,现在编辑完后无法修改,还是原来的数据

因为编辑用的是模拟数据啊,编辑了确实是没生效的,看请求发成功就可以了,要是想真正的修改就自己做数据库

@l-ichaochao
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

觉得2-2这种实现模态框展示和隐藏的方式比较好写。
现在公司有用1-1的方式的,组件-action-effect来回跳转,比较麻烦,虽然这样看来各个状态都在dva里面的model内进行管理,但是不免有些繁琐

求深解

Please sign in to comment.