Reactjs shows only first record each time a modal pop up button is clicked












0















I have 9 records in reactjs array which is displayed with each records having its own Bootstrap Modal View button.



{ "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" },


When I click on the First Modal View Button, I got the correct records (Name=Tony, Age=18) displayed in the Modal view.



When I click on the second record Modal View button, am supposed to have second record (Name=John, Age=21) displayed in the Modal but instead, it shows the first record that is (Name=Tony, Age=18). Hence the Modal View keeps showing the first record no matter which view button is clicked.



Here is the entire Codes



<!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">&times;</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>









share|improve this question


















  • 3





    clearly you have used 0 in the viewData function, use a parameter and pass the i value from the render

    – Avinash
    Nov 25 '18 at 20:29













  • Please can you emphasize with code sample. between thanks for your response so far

    – jmarkatti
    Nov 25 '18 at 20:34
















0















I have 9 records in reactjs array which is displayed with each records having its own Bootstrap Modal View button.



{ "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" },


When I click on the First Modal View Button, I got the correct records (Name=Tony, Age=18) displayed in the Modal view.



When I click on the second record Modal View button, am supposed to have second record (Name=John, Age=21) displayed in the Modal but instead, it shows the first record that is (Name=Tony, Age=18). Hence the Modal View keeps showing the first record no matter which view button is clicked.



Here is the entire Codes



<!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">&times;</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>









share|improve this question


















  • 3





    clearly you have used 0 in the viewData function, use a parameter and pass the i value from the render

    – Avinash
    Nov 25 '18 at 20:29













  • Please can you emphasize with code sample. between thanks for your response so far

    – jmarkatti
    Nov 25 '18 at 20:34














0












0








0








I have 9 records in reactjs array which is displayed with each records having its own Bootstrap Modal View button.



{ "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" },


When I click on the First Modal View Button, I got the correct records (Name=Tony, Age=18) displayed in the Modal view.



When I click on the second record Modal View button, am supposed to have second record (Name=John, Age=21) displayed in the Modal but instead, it shows the first record that is (Name=Tony, Age=18). Hence the Modal View keeps showing the first record no matter which view button is clicked.



Here is the entire Codes



<!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">&times;</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>









share|improve this question














I have 9 records in reactjs array which is displayed with each records having its own Bootstrap Modal View button.



{ "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" },


When I click on the First Modal View Button, I got the correct records (Name=Tony, Age=18) displayed in the Modal view.



When I click on the second record Modal View button, am supposed to have second record (Name=John, Age=21) displayed in the Modal but instead, it shows the first record that is (Name=Tony, Age=18). Hence the Modal View keeps showing the first record no matter which view button is clicked.



Here is the entire Codes



<!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">&times;</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>






reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 20:26









jmarkattijmarkatti

18219




18219








  • 3





    clearly you have used 0 in the viewData function, use a parameter and pass the i value from the render

    – Avinash
    Nov 25 '18 at 20:29













  • Please can you emphasize with code sample. between thanks for your response so far

    – jmarkatti
    Nov 25 '18 at 20:34














  • 3





    clearly you have used 0 in the viewData function, use a parameter and pass the i value from the render

    – Avinash
    Nov 25 '18 at 20:29













  • Please can you emphasize with code sample. between thanks for your response so far

    – jmarkatti
    Nov 25 '18 at 20:34








3




3





clearly you have used 0 in the viewData function, use a parameter and pass the i value from the render

– Avinash
Nov 25 '18 at 20:29







clearly you have used 0 in the viewData function, use a parameter and pass the i value from the render

– Avinash
Nov 25 '18 at 20:29















Please can you emphasize with code sample. between thanks for your response so far

– jmarkatti
Nov 25 '18 at 20:34





Please can you emphasize with code sample. between thanks for your response so far

– jmarkatti
Nov 25 '18 at 20:34












1 Answer
1






active

oldest

votes


















1














You'll want to add a property to your state, currentRec, to keep track of which array item you clicked on.



As well you need to change your viewData() to accept the index of which array item has been clicked. Once you have this value, you can set it to currentRec to know which array item to show.



After that you can use this currentRec to display the current array item in your modal body instead always returning the first element in the array via [0] which was the problem.






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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








share|improve this answer



















  • 1





    Thanks Sir, Andrews

    – jmarkatti
    Nov 25 '18 at 21:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471599%2freactjs-shows-only-first-record-each-time-a-modal-pop-up-button-is-clicked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You'll want to add a property to your state, currentRec, to keep track of which array item you clicked on.



As well you need to change your viewData() to accept the index of which array item has been clicked. Once you have this value, you can set it to currentRec to know which array item to show.



After that you can use this currentRec to display the current array item in your modal body instead always returning the first element in the array via [0] which was the problem.






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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








share|improve this answer



















  • 1





    Thanks Sir, Andrews

    – jmarkatti
    Nov 25 '18 at 21:06
















1














You'll want to add a property to your state, currentRec, to keep track of which array item you clicked on.



As well you need to change your viewData() to accept the index of which array item has been clicked. Once you have this value, you can set it to currentRec to know which array item to show.



After that you can use this currentRec to display the current array item in your modal body instead always returning the first element in the array via [0] which was the problem.






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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








share|improve this answer



















  • 1





    Thanks Sir, Andrews

    – jmarkatti
    Nov 25 '18 at 21:06














1












1








1







You'll want to add a property to your state, currentRec, to keep track of which array item you clicked on.



As well you need to change your viewData() to accept the index of which array item has been clicked. Once you have this value, you can set it to currentRec to know which array item to show.



After that you can use this currentRec to display the current array item in your modal body instead always returning the first element in the array via [0] which was the problem.






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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








share|improve this answer













You'll want to add a property to your state, currentRec, to keep track of which array item you clicked on.



As well you need to change your viewData() to accept the index of which array item has been clicked. Once you have this value, you can set it to currentRec to know which array item to show.



After that you can use this currentRec to display the current array item in your modal body instead always returning the first element in the array via [0] which was the problem.






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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





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" }
],
currentRec: undefined
};
this.viewData = this.viewData.bind(this);
}

// Display Data in a Modal when View button is Clicked

viewData = (i) => {
this.setState({ currentRec: i });
console.log(`Selected record index: ${i}`);
}

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(i); }}
className="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#myModal"
>
view from Modal
</button>
</li>
))}
</ul>
</div>
<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">
&times;
</button>
</div>

{this.state.currentRec !== undefined &&
<div className="modal-body">
Name: {this.state.rec[this.state.currentRec].name} <br />
Age: {this.state.rec[this.state.currentRec].Age} <br />
</div>}

<div className="modal-footer">
<button
type="button"
className="btn btn-danger"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}

ReactDOM.render(
<Application/>,
document.body
);

<div class="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 20:42









Shawn AndrewsShawn Andrews

965718




965718








  • 1





    Thanks Sir, Andrews

    – jmarkatti
    Nov 25 '18 at 21:06














  • 1





    Thanks Sir, Andrews

    – jmarkatti
    Nov 25 '18 at 21:06








1




1





Thanks Sir, Andrews

– jmarkatti
Nov 25 '18 at 21:06





Thanks Sir, Andrews

– jmarkatti
Nov 25 '18 at 21:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471599%2freactjs-shows-only-first-record-each-time-a-modal-pop-up-button-is-clicked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

To store a contact into the json file from server.js file using a class in NodeJS

Marschland