前文中实现了用户添加与用户列表展示的功能,本篇带大家来完成用户的编辑与删除。
添加操作列
编辑与删除功能都是针对已存在的某一个用户执行的操作,所以在用户列表中需要再加一个“操作”列来展现【编辑】与【删除】这两个按钮。
修改/src/pages/UserList.js
文件,添加方法handleEdit与handleDel,并在table中添加一列:
...
class UserList extends React.Component {
constructor (props) { ... }
componentWillMount () { ... }
handleEdit (user) {
}
handleDel (user) {
}
render () {
const {userList} = this.state;
return (
<HomeLayout title="用户列表">
<table>
<thead>
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{
userList.map((user) => {
return (
<tr key={user.id}>
<td>{user.id}</td>
<td>{user.name}</td>
<td>{user.gender}</td>
<td>{user.age}</td>
<td>
<a href="javascript:void(0)" onClick={() => this.handleEdit(user)}>编辑</a>
<