my attempt fiddle here ..........
my attempt fiddle here ..........
您可以通过操作react状态来实现。在您的情况下,您正在使用嵌套对象,因此在更新它们时需要小心。
修改您的state
并不是一个好主意,因为它可能会导致错误或意外行为。
仔细查看handling
函数的工作方式。
这里有一个实时演示。
class App extends React.Component {
state = {
rows: []
};
handleChange = idx => e => {
const { name, value } = e.target;
const rows = [...this.state.rows];
rows[idx] = {
[name]: value
};
this.setState({
rows
});
};
handleAddRow = () => {
const item = {
name: "",
mobile: ""
};
this.setState({
rows: [...this.state.rows, item]
});
};
handleRemoveRow = () => {
this.setState({
rows: this.state.rows.slice(0, -1)
});
};
render() {
return (
<div>
<div className="container">
<div className="row clearfix">
<div className="col-md-12 column">
<table
className="table table-bordered table-hover"
id="tab_logic"
>
<thead>
<tr>
<th className="text-center"> # </th>
<th className="text-center"> Name </th>
<th className="text-center"> Mobile </th>
</tr>
</thead>
<tbody>
{this.state.rows.map((item, idx) => (
<tr id="addr0" key={idx}>
<td>{idx}</td>
<td>
<input
type="text"
name="name"
value={this.state.rows[idx].name}
onChange={this.handleChange(idx)}
className="form-control"
/>
</td>
<td>
<input
type="text"
name="mobile"
value={this.state.rows[idx].mobile}
onChange={this.handleChange(idx)}
className="form-control"
/>
</td>
</tr>
))}
</tbody>
</table>
<button
onClick={this.handleAddRow}
className="btn btn-default pull-left"
>
Add Row
</button>
<button
onClick={this.handleRemoveRow}
className="pull-right btn btn-default"
>
Delete Row
</button>
</div>
</div>
</div>
</div>
);
}
}
render(<App />, document.getElementById("root"));
提示:如果我能给你一个建议,我会说你需要更多地学习有关React-JavaScript的知识才能快速前进,现在你需要很好地理解基础知识。
如果有人在寻找 React Hooks 版本,这是 Arnold 回答的一个示例实现:
import React, { useState } from "react";
const App = () => {
const [rows, setRows] = useState([{}]);
const columnsArray = ["name", "id", "mobile", "date", "amount"]; // pass columns here dynamically
const handleAddRow = () => {
const item = {};
setRows([...rows, item]);
};
const postResults = () => {
console.log(rows); // there you go, do as you please
};
const handleRemoveSpecificRow = (idx) => {
const tempRows = [...rows]; // to avoid direct state mutation
tempRows.splice(idx, 1);
setRows(tempRows);
};
const updateState = (e) => {
let prope = e.target.attributes.column.value; // the custom column attribute
let index = e.target.attributes.index.value; // index of state array -rows
let fieldValue = e.target.value; // value
const tempRows = [...rows]; // avoid direct state mutation
const tempObj = rows[index]; // copy state object at index to a temporary object
tempObj[prope] = fieldValue; // modify temporary object
// return object to rows` clone
tempRows[index] = tempObj;
setRows(tempRows); // update state
};
return (
<div>
<div className="container">
<div className="row clearfix">
<div className="col-md-12 column">
<table className="table table-bordered table-hover" id="tab_logic">
<thead>
<tr>
<th className="text-center"> # </th>
{columnsArray.map((column, index) => (
<th className="text-center" key={index}>
{column}
</th>
))}
<th />
</tr>
</thead>
<tbody>
{rows.map((item, idx) => (
<tr key={idx}>
<td>{idx + 1}</td>
{columnsArray.map((column, index) => (
<td key={index}>
<input
type="text"
column={column}
value={rows[idx][column]}
index={idx}
className="form-control"
onChange={(e) => updateState(e)}
/>
</td>
))}
<td>
<button
className="btn btn-outline-danger btn-sm"
onClick={() => handleRemoveSpecificRow(idx)}
>
Remove
</button>
</td>
</tr>
))}
</tbody>
</table>
<button onClick={handleAddRow} className="btn btn-primary">
Add Row
</button>
<button
onClick={postResults}
className="btn btn-success float-right"
>
Save Results
</button>
</div>
</div>
</div>
</div>
);
};
export default App;
import React from "react";
import { render } from "react-dom";
const styles = {
fontFamily: "sans-serif",
textAlign: "left"
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
rows: []
};
}
handleAddRow = () => {
this.setState((prevState, props) => {
const row = {content: "hello this is a new row!" };
return { rows: [...prevState.rows, row] };
});
};
handleRemoveRow = () => {
this.setState((prevState, props) => {
return { rows: prevState.rows.slice(1) };
});
};
render() {
console.log(this.state);
return (
<div style={styles}>
<table>
<tbody>
{this.state.rows.map(row => (
<tr>
<td>{row.content}</td>
</tr>
))}
<tr>
<td className="" onClick={this.handleAddRow}>
(+)
</td>
{Boolean(this.state.rows.length) && (
<td onClick={this.handleRemoveRow}>(-)</td>
)}
</tr>
</tbody>
</table>
</div>
);
}
}
render(<App />, document.getElementById("root"));
感谢上面的答案。 对于“handleChange”的处理,我想发表一点意见。 我们需要修改以下内容吗?
rows[idx] = {
[name]: value
};
to the following for saving the data into rows:
const rowInfo = rows[idx];
rowInfo[name] = value;