【发布时间】:2018-11-25 20:26:43
【问题描述】:
我在 reactjs 数组中有 9 条记录,每条记录都有自己的 Bootstrap 模式视图按钮。
{ "name" : "Tony", "Age" : "18"},
{ "name" : "John", "Age" : "21" },
{ "name" : "Luke", "Age" : "78" },
{ "name" : "Mark", "Age" : "90" },
{ "name" : "Jame", "Age" : "87" },
{ "name" : "Franco", "Age" : "34" },
{ "name" : "Franco", "Age" : "34" },
{ "name" : "Biggard", "Age" : "19" },
{ "name" : "tom", "Age" : "89" },
当我点击第一个模态视图按钮时,我在模态视图中显示了正确的记录 (Name=Tony, Age=18)。
当我单击第二条记录的模态视图按钮时,应该在模态中显示第二条记录 (Name=John, Age=21) 但它显示的第一条记录是(姓名=托尼,年龄=18)。因此,无论单击哪个视图按钮,模态视图都会一直显示第一条记录。
这是完整的代码
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<style>
.pic{
background:blue; color:white;}
</style>
<script src="build/react.min.js"></script>
<script src="build/react-dom.min.js"></script>
<script src="build/browser.min.js"></script>
<script src="build/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div id="app"></div>
<script type="text/babel">
class Application extends React.Component {
constructor(props) {
super(props)
this.state = {rec : [
{ "name" : "Tony", "Age" : "18"},
{ "name" : "John", "Age" : "21" },
{ "name" : "Luke", "Age" : "78" },
{ "name" : "Mark", "Age" : "90" },
{ "name" : "Jame", "Age" : "87" },
{ "name" : "Franco", "Age" : "34" },
{ "name" : "Franco", "Age" : "34" },
{ "name" : "Biggard", "Age" : "19" },
{ "name" : "tom", "Age" : "89" },
],
};
this.viewData = this.viewData.bind(this);
}
// Display Data in a Modal when View button is Clicked
viewData() {
//this.see=this.state.rec[0].name;
console.log(this.see);
}
render() {
return <div className="container">
<div>
<h3>List of Records</h3>
<ul>
{this.state.rec.map((obj, i) =>
<li key={i}>{obj.name} - {obj.Age} <button type="button" onClick={this.viewData} className="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">view from Modal</button></li>
)}
</ul>
</div>
//start Bootstrap modal
<div className="modal" id="myModal">
<div className="modal-dialog">
<div see={this.see} className="modal-content">
<div className="modal-header">
<h4 className="modal-title">Show Records in Modal</h4>
<button type="button" className="close" data-dismiss="modal">×</button>
</div>
<div className="modal-body">
Name: {this.state.rec[0].name} <br />
Age: {this.state.rec[0].Age} <br />
</div>
<div className="modal-footer">
<button type="button" className="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
// End Bootstrap Modal
</div>;
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
</script>
</body>
</html>
【问题讨论】:
-
显然你在 viewData 函数中使用了 0,使用一个参数并从渲染中传递 i 值
-
请用代码示例强调一下。感谢您到目前为止的回复
标签: reactjs