Toggle display specific element inside map function onClick












0















I have a plus sign which onClick switches to Minus sign. With that, a list of grades is also displayed based on toggle.



Currently when I click on the plug sign, all the elements expand displaying list for all students. I want to only toggle the list for the specific student.



The data is being pulled from an API then being displayed.



class Home extends Component {
constructor(props) {
super(props);
this.state = {
students: ,
search: "",
display: false
};
}

_iconOnClick = event => {
this.state.display ? this.setState({ display: false }) : this.setState({ display: true })
;
};

_renderDataFromAPI = () => {
//search filter
let filteredName = this.state.students.filter(student => {
return (
student.firstName
.toLowerCase()
.includes(this.state.search.toLowerCase()) ||
student.lastName.toLowerCase().includes(this.state.search.toLowerCase())
);
});

return filteredName.map((student,i) => {
let average =
student.grades.reduce((x, y) => parseInt(x) + parseInt(y)) /
student.grades.length;
return (
<div key={i}>
<div className="data">
<img className="img" src={student.pic} align="left" />
{this.state.display ? (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick}
/>
) : (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="plus"
onClick={this._iconOnClick}
/>
)}

<h1>
{student.firstName.toUpperCase()} {student.lastName.toUpperCase()}
</h1>
<p>Email: {student.email}</p>
<p>Company: {student.company}</p>
<p>Skill: {student.skill}</p>
<p>Average: {average}%</p>
<br />
{this.state.display ? (
<div >
{student.grades.map((grade, i) => {
return (
<p key={i}>
Test {i + 1}: {grade}%
</p>
);
})}
</div>
) : (
<div />
)}
</div>
<hr style={{ borderColor: "#e2e1e1" }} />
</div>
);
});
};









share|improve this question

























  • Can you share your _iconOnClick function?

    – JP4
    Nov 22 '18 at 3:37













  • @JP4 just made the edit.

    – Binny.H
    Nov 22 '18 at 3:39











  • I suggest you to create 2 functions to catch on expand and on collapse events. Also, how did you update the value this.state.search?

    – Foo
    Nov 22 '18 at 7:59
















0















I have a plus sign which onClick switches to Minus sign. With that, a list of grades is also displayed based on toggle.



Currently when I click on the plug sign, all the elements expand displaying list for all students. I want to only toggle the list for the specific student.



The data is being pulled from an API then being displayed.



class Home extends Component {
constructor(props) {
super(props);
this.state = {
students: ,
search: "",
display: false
};
}

_iconOnClick = event => {
this.state.display ? this.setState({ display: false }) : this.setState({ display: true })
;
};

_renderDataFromAPI = () => {
//search filter
let filteredName = this.state.students.filter(student => {
return (
student.firstName
.toLowerCase()
.includes(this.state.search.toLowerCase()) ||
student.lastName.toLowerCase().includes(this.state.search.toLowerCase())
);
});

return filteredName.map((student,i) => {
let average =
student.grades.reduce((x, y) => parseInt(x) + parseInt(y)) /
student.grades.length;
return (
<div key={i}>
<div className="data">
<img className="img" src={student.pic} align="left" />
{this.state.display ? (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick}
/>
) : (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="plus"
onClick={this._iconOnClick}
/>
)}

<h1>
{student.firstName.toUpperCase()} {student.lastName.toUpperCase()}
</h1>
<p>Email: {student.email}</p>
<p>Company: {student.company}</p>
<p>Skill: {student.skill}</p>
<p>Average: {average}%</p>
<br />
{this.state.display ? (
<div >
{student.grades.map((grade, i) => {
return (
<p key={i}>
Test {i + 1}: {grade}%
</p>
);
})}
</div>
) : (
<div />
)}
</div>
<hr style={{ borderColor: "#e2e1e1" }} />
</div>
);
});
};









share|improve this question

























  • Can you share your _iconOnClick function?

    – JP4
    Nov 22 '18 at 3:37













  • @JP4 just made the edit.

    – Binny.H
    Nov 22 '18 at 3:39











  • I suggest you to create 2 functions to catch on expand and on collapse events. Also, how did you update the value this.state.search?

    – Foo
    Nov 22 '18 at 7:59














0












0








0








I have a plus sign which onClick switches to Minus sign. With that, a list of grades is also displayed based on toggle.



Currently when I click on the plug sign, all the elements expand displaying list for all students. I want to only toggle the list for the specific student.



The data is being pulled from an API then being displayed.



class Home extends Component {
constructor(props) {
super(props);
this.state = {
students: ,
search: "",
display: false
};
}

_iconOnClick = event => {
this.state.display ? this.setState({ display: false }) : this.setState({ display: true })
;
};

_renderDataFromAPI = () => {
//search filter
let filteredName = this.state.students.filter(student => {
return (
student.firstName
.toLowerCase()
.includes(this.state.search.toLowerCase()) ||
student.lastName.toLowerCase().includes(this.state.search.toLowerCase())
);
});

return filteredName.map((student,i) => {
let average =
student.grades.reduce((x, y) => parseInt(x) + parseInt(y)) /
student.grades.length;
return (
<div key={i}>
<div className="data">
<img className="img" src={student.pic} align="left" />
{this.state.display ? (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick}
/>
) : (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="plus"
onClick={this._iconOnClick}
/>
)}

<h1>
{student.firstName.toUpperCase()} {student.lastName.toUpperCase()}
</h1>
<p>Email: {student.email}</p>
<p>Company: {student.company}</p>
<p>Skill: {student.skill}</p>
<p>Average: {average}%</p>
<br />
{this.state.display ? (
<div >
{student.grades.map((grade, i) => {
return (
<p key={i}>
Test {i + 1}: {grade}%
</p>
);
})}
</div>
) : (
<div />
)}
</div>
<hr style={{ borderColor: "#e2e1e1" }} />
</div>
);
});
};









share|improve this question
















I have a plus sign which onClick switches to Minus sign. With that, a list of grades is also displayed based on toggle.



Currently when I click on the plug sign, all the elements expand displaying list for all students. I want to only toggle the list for the specific student.



The data is being pulled from an API then being displayed.



class Home extends Component {
constructor(props) {
super(props);
this.state = {
students: ,
search: "",
display: false
};
}

_iconOnClick = event => {
this.state.display ? this.setState({ display: false }) : this.setState({ display: true })
;
};

_renderDataFromAPI = () => {
//search filter
let filteredName = this.state.students.filter(student => {
return (
student.firstName
.toLowerCase()
.includes(this.state.search.toLowerCase()) ||
student.lastName.toLowerCase().includes(this.state.search.toLowerCase())
);
});

return filteredName.map((student,i) => {
let average =
student.grades.reduce((x, y) => parseInt(x) + parseInt(y)) /
student.grades.length;
return (
<div key={i}>
<div className="data">
<img className="img" src={student.pic} align="left" />
{this.state.display ? (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick}
/>
) : (
<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="plus"
onClick={this._iconOnClick}
/>
)}

<h1>
{student.firstName.toUpperCase()} {student.lastName.toUpperCase()}
</h1>
<p>Email: {student.email}</p>
<p>Company: {student.company}</p>
<p>Skill: {student.skill}</p>
<p>Average: {average}%</p>
<br />
{this.state.display ? (
<div >
{student.grades.map((grade, i) => {
return (
<p key={i}>
Test {i + 1}: {grade}%
</p>
);
})}
</div>
) : (
<div />
)}
</div>
<hr style={{ borderColor: "#e2e1e1" }} />
</div>
);
});
};






javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 3:39







Binny.H

















asked Nov 22 '18 at 3:31









Binny.HBinny.H

386




386













  • Can you share your _iconOnClick function?

    – JP4
    Nov 22 '18 at 3:37













  • @JP4 just made the edit.

    – Binny.H
    Nov 22 '18 at 3:39











  • I suggest you to create 2 functions to catch on expand and on collapse events. Also, how did you update the value this.state.search?

    – Foo
    Nov 22 '18 at 7:59



















  • Can you share your _iconOnClick function?

    – JP4
    Nov 22 '18 at 3:37













  • @JP4 just made the edit.

    – Binny.H
    Nov 22 '18 at 3:39











  • I suggest you to create 2 functions to catch on expand and on collapse events. Also, how did you update the value this.state.search?

    – Foo
    Nov 22 '18 at 7:59

















Can you share your _iconOnClick function?

– JP4
Nov 22 '18 at 3:37







Can you share your _iconOnClick function?

– JP4
Nov 22 '18 at 3:37















@JP4 just made the edit.

– Binny.H
Nov 22 '18 at 3:39





@JP4 just made the edit.

– Binny.H
Nov 22 '18 at 3:39













I suggest you to create 2 functions to catch on expand and on collapse events. Also, how did you update the value this.state.search?

– Foo
Nov 22 '18 at 7:59





I suggest you to create 2 functions to catch on expand and on collapse events. Also, how did you update the value this.state.search?

– Foo
Nov 22 '18 at 7:59












1 Answer
1






active

oldest

votes


















1














Move the display to each student.



let dataFromAPI = getDataFromAPI();
this.setState({
students: dataFromAPI.map(
student => Object.assign(student, { display: false })
});


Display the list...



 {student.display ? (
<div >
{student.grades.map((grade, i) => { ...


Toggle the show/hide



_iconOnClick = id => {
let students = this.students.map(s => {
if (id == s.id) {
return Object.assign(s, { display: !s.display });
}
return s;
});
this.setState({ students });
};


...



<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick(student.id)} />





share|improve this answer


























  • Can you expand on this? I'm a little confused

    – Binny.H
    Nov 22 '18 at 4:55












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%2f53423481%2ftoggle-display-specific-element-inside-map-function-onclick%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














Move the display to each student.



let dataFromAPI = getDataFromAPI();
this.setState({
students: dataFromAPI.map(
student => Object.assign(student, { display: false })
});


Display the list...



 {student.display ? (
<div >
{student.grades.map((grade, i) => { ...


Toggle the show/hide



_iconOnClick = id => {
let students = this.students.map(s => {
if (id == s.id) {
return Object.assign(s, { display: !s.display });
}
return s;
});
this.setState({ students });
};


...



<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick(student.id)} />





share|improve this answer


























  • Can you expand on this? I'm a little confused

    – Binny.H
    Nov 22 '18 at 4:55
















1














Move the display to each student.



let dataFromAPI = getDataFromAPI();
this.setState({
students: dataFromAPI.map(
student => Object.assign(student, { display: false })
});


Display the list...



 {student.display ? (
<div >
{student.grades.map((grade, i) => { ...


Toggle the show/hide



_iconOnClick = id => {
let students = this.students.map(s => {
if (id == s.id) {
return Object.assign(s, { display: !s.display });
}
return s;
});
this.setState({ students });
};


...



<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick(student.id)} />





share|improve this answer


























  • Can you expand on this? I'm a little confused

    – Binny.H
    Nov 22 '18 at 4:55














1












1








1







Move the display to each student.



let dataFromAPI = getDataFromAPI();
this.setState({
students: dataFromAPI.map(
student => Object.assign(student, { display: false })
});


Display the list...



 {student.display ? (
<div >
{student.grades.map((grade, i) => { ...


Toggle the show/hide



_iconOnClick = id => {
let students = this.students.map(s => {
if (id == s.id) {
return Object.assign(s, { display: !s.display });
}
return s;
});
this.setState({ students });
};


...



<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick(student.id)} />





share|improve this answer















Move the display to each student.



let dataFromAPI = getDataFromAPI();
this.setState({
students: dataFromAPI.map(
student => Object.assign(student, { display: false })
});


Display the list...



 {student.display ? (
<div >
{student.grades.map((grade, i) => { ...


Toggle the show/hide



_iconOnClick = id => {
let students = this.students.map(s => {
if (id == s.id) {
return Object.assign(s, { display: !s.display });
}
return s;
});
this.setState({ students });
};


...



<FontAwesomeIcon
style={{
float: "right",
fontSize: "30px",
marginRight: "40px"
}}
icon="minus"
onClick={this._iconOnClick(student.id)} />






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 19:24

























answered Nov 22 '18 at 3:46









below-1below-1

3113




3113













  • Can you expand on this? I'm a little confused

    – Binny.H
    Nov 22 '18 at 4:55



















  • Can you expand on this? I'm a little confused

    – Binny.H
    Nov 22 '18 at 4:55

















Can you expand on this? I'm a little confused

– Binny.H
Nov 22 '18 at 4:55





Can you expand on this? I'm a little confused

– Binny.H
Nov 22 '18 at 4:55




















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%2f53423481%2ftoggle-display-specific-element-inside-map-function-onclick%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents