javascript - 如何仅在单击按钮的行更新按钮?
问题描述
目前,当我单击任何按钮时,所有编辑按钮都会更改为更新按钮,但我只想触发实际执行操作的行而不触发任何其他按钮。我在我的 ReactJs 项目中使用下面的代码片段。任何人都可以帮忙提出一些对我有用的建议。请注意,此时我没有使用钩子。
import React from 'react';
class GridSample extends React.Component {
constructor() {
super();
this.state = {
clicked: false,
};
this.handleOnedit = this.handleOnedit.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.handleUpdate = this.handleUpdate.bind(this);
}
handleOnedit = (event) => {
const par = event.target.parentNode;
const suppar = par.parentNode;
suppar.setAttribute('contenteditable', true);
suppar.focus();
this.setState({ clicked: true });
};
handleOndelete() {
alert('this is delete button');
}
handleCancel() {
window.location.replace(false);
}
handleUpdate() {
this.setState({
clicked: false,
});
}
render() {
console.clear();
return (
<div>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Action</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
<td>
{!this.state.clicked ? (
<input type="button" value="Edit" onClick={this.handleOnedit} />
) : (
<input
type="button"
value="Update"
onClick={this.handleUpdate}
/>
)}
{this.state.clicked ? (
<input
type="button"
value="Cancel"
onClick={this.handleCancel}
/>
) : (
<input
type="button"
value="Delete"
onClick={this.handleOndelete}
/>
)}
</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td>
{!this.state.clicked ? (
<input type="button" value="Edit" onClick={this.handleOnedit} />
) : (
<input
type="button"
value="Update"
onClick={this.handleUpdate}
/>
)}
{this.state.clicked ? (
<input
type="button"
value="Cancel"
onClick={this.handleCancel}
/>
) : (
<input
type="button"
value="Delete"
onClick={this.handleOndelete}
/>
)}
</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
<td>
{!this.state.clicked ? (
<input type="button" value="Edit" onClick={this.handleOnedit} />
) : (
<input
type="button"
value="Update"
onClick={this.handleUpdate}
/>
)}
{this.state.clicked ? (
<input
type="button"
value="Cancel"
onClick={this.handleCancel}
/>
) : (
<input
type="button"
value="Delete"
onClick={this.handleOndelete}
/>
)}
</td>
</tr>
</table>
</div>
);
}
}
export default GridSample;
解决方案
您应该尝试将索引作为参数传递并对其进行测试,而不是将值更改为您的 state.clicked 的 true 或 false(我添加了一个指向使用您的 calss 的沙箱的链接
import React from "react";
class GridSample extends React.Component {
constructor() {
super();
this.state = {
clicked: NaN,
items: [
{ Firstname: "Jack", Lastname: "Ali", Age: 25 },
{ Firstname: "Adam", Lastname: "Alan", Age: 55 }
]
};
this.handleOnedit = this.handleOnedit.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.handleUpdate = this.handleUpdate.bind(this);
}
handleOnedit = (event, i) => {
const par = event.target.parentNode;
const suppar = par.parentNode;
suppar.setAttribute("contenteditable", true);
suppar.focus();
this.setState({ ...this.state, clicked: i });
};
handleOndelete() {
alert("this is delete button");
}
handleCancel() {
window.location.replace(false);
}
handleUpdate(event, i) {
this.setState({ ...this.state, clicked: i });
}
render() {
console.clear();
return (
<div>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Action</th>
</tr>
{this.state.items.map((e, i) => {
return (
<tr key={i}>
<td>{e.Firstname}</td>
<td>{e.Lastname}</td>
<td>{e.Age}</td>
<td>
{!this.state.clicked === i ? (
<input
type="button"
value="Edit"
onClick={(event) => this.handleOnedit(event, i)}
/>
) : (
<input
type="button"
value="Update"
onClick={(event) => this.handleUpdate(event, i)}
/>
)}
{this.state.clicked === i ? (
<input
type="button"
value="Cancel"
onClick={(event) => this.handleCancel(event, i)}
/>
) : (
<input
type="button"
value="Delete"
onClick={(event) => this.handleOndelete(event, i)}
/>
)}
</td>
</tr>
);
})}
</table>
</div>
);
}
}
export default GridSample;
这是示例:https ://codesandbox.io/s/react-class-forked-ze3o9?file=/index.js
推荐阅读
- mysql - 如果至少一个条件不满足,则消除所有标识符
- html - 如何重新调整容器大小以适合其子项的宽度
- c++ - 我将如何在 C++20 中传递范围而不是迭代器对?
- vue.js - Vue-Multiselect删除事件如何获取值和id
- ios - Airplay 处于活动状态时,AVRoutePickerView 不会改变颜色
- spring - Spring data Page/Pageable 在大型数据集上返回重复项?
- sql - 从 2 个表中获取特定输出
- javascript - 使用 React.Children 从外部调用内部组件方法
- csv - Text::CSV 支持的编码
- unicode - Windows 服务在字符集为 unicode 的情况下工作正常,但具有多字节的相同代码开始失败并出现 1053 错误