Dropdown box Value Lost After Clicking Event
I created a dropdown box with a onChange event to filter data, for example ['campuse A','campus B', ...]. When I selected the dropdown value, the returned data works fine but my dropdown box only has the selected value, 'campus A' and default "Select" value. How to fix this problem? Thanks.
App.js
import React, { Component } from 'react';
import './App.css';
import Course from './Components/Course';
import axios from 'axios';
//import CampusName from './Components/CampusName';
class App extends Component {
constructor() {
super();
this.state ={
courses:,
selectedCampus:''
};
}
componentDidMount(){
axios.get('myapiURL')
.then(response=>{
this.setState({courses:response.data});
});
}
_getUniqueCampusName(){
const courses=this.state.courses;
const campusSelection=[...new Set(courses.map(item=>item.CAMPUS_NAME))];
return campusSelection.map((campus,index)=>(<CampusName key={index} value=
{campus} onChange="this._campusFilter.bind(this)"/>));
}
//campus filter
_campusFilter(event){
const selection = event.target.options[event.target.selectedIndex];
let filteredData;
if (selection.value === "0") {
filteredData= this.state.courses;
return;
}
else{
filteredData = this.state.courses.filter(item=>{
return (item.CAMPUS_NAME === event.target.value);
});
}
this.setState({
selectedCampus: event.target.value,
courses: filteredData
});
}
render() {
const courses= this.state.courses.map(course=>(<Course ID={course.ID}
coursename={course.SUBJECT} />));
const campusName=this._getUniqueCampusName();
const renderCourses = courses.map((course, index) => {
return <li key={index}>{<Course key={index} ID={course.ID} SUBJECT= {course.SUBJECT} CAMPUS_NAME={course.CAMPUS_NAME} />}</li>;
});
return (
<div className="App">
<div className="filter">
<select name="campus" onChange={this._campusFilter.bind(this)}>
<option value="0">Select a Campus</option>
{campusName}
</select>
</div>
<div className="Course">Courses: {renderCourses}
</div>
</div>
);
}
}
export default App;
Course.js
import React from 'react';
const course=props=>{
return(
<div className="Course">
<p>ID: {props.ID}</p>
<p>SUBJECT: {props.SUBJECT}</p>
<p>Campus: {props.CAMPUS_NAME}</p>
</div>
);
}
export default course;
reactjs
add a comment |
I created a dropdown box with a onChange event to filter data, for example ['campuse A','campus B', ...]. When I selected the dropdown value, the returned data works fine but my dropdown box only has the selected value, 'campus A' and default "Select" value. How to fix this problem? Thanks.
App.js
import React, { Component } from 'react';
import './App.css';
import Course from './Components/Course';
import axios from 'axios';
//import CampusName from './Components/CampusName';
class App extends Component {
constructor() {
super();
this.state ={
courses:,
selectedCampus:''
};
}
componentDidMount(){
axios.get('myapiURL')
.then(response=>{
this.setState({courses:response.data});
});
}
_getUniqueCampusName(){
const courses=this.state.courses;
const campusSelection=[...new Set(courses.map(item=>item.CAMPUS_NAME))];
return campusSelection.map((campus,index)=>(<CampusName key={index} value=
{campus} onChange="this._campusFilter.bind(this)"/>));
}
//campus filter
_campusFilter(event){
const selection = event.target.options[event.target.selectedIndex];
let filteredData;
if (selection.value === "0") {
filteredData= this.state.courses;
return;
}
else{
filteredData = this.state.courses.filter(item=>{
return (item.CAMPUS_NAME === event.target.value);
});
}
this.setState({
selectedCampus: event.target.value,
courses: filteredData
});
}
render() {
const courses= this.state.courses.map(course=>(<Course ID={course.ID}
coursename={course.SUBJECT} />));
const campusName=this._getUniqueCampusName();
const renderCourses = courses.map((course, index) => {
return <li key={index}>{<Course key={index} ID={course.ID} SUBJECT= {course.SUBJECT} CAMPUS_NAME={course.CAMPUS_NAME} />}</li>;
});
return (
<div className="App">
<div className="filter">
<select name="campus" onChange={this._campusFilter.bind(this)}>
<option value="0">Select a Campus</option>
{campusName}
</select>
</div>
<div className="Course">Courses: {renderCourses}
</div>
</div>
);
}
}
export default App;
Course.js
import React from 'react';
const course=props=>{
return(
<div className="Course">
<p>ID: {props.ID}</p>
<p>SUBJECT: {props.SUBJECT}</p>
<p>Campus: {props.CAMPUS_NAME}</p>
</div>
);
}
export default course;
reactjs
I think I know why the dropdown list only has the selected value option because courses only contains filtered data in _getUniqueCampusName() function, line "const courses=this.state.courses;" . How to make a copy of the original courses?
– user788448
Nov 13 '18 at 0:20
add a comment |
I created a dropdown box with a onChange event to filter data, for example ['campuse A','campus B', ...]. When I selected the dropdown value, the returned data works fine but my dropdown box only has the selected value, 'campus A' and default "Select" value. How to fix this problem? Thanks.
App.js
import React, { Component } from 'react';
import './App.css';
import Course from './Components/Course';
import axios from 'axios';
//import CampusName from './Components/CampusName';
class App extends Component {
constructor() {
super();
this.state ={
courses:,
selectedCampus:''
};
}
componentDidMount(){
axios.get('myapiURL')
.then(response=>{
this.setState({courses:response.data});
});
}
_getUniqueCampusName(){
const courses=this.state.courses;
const campusSelection=[...new Set(courses.map(item=>item.CAMPUS_NAME))];
return campusSelection.map((campus,index)=>(<CampusName key={index} value=
{campus} onChange="this._campusFilter.bind(this)"/>));
}
//campus filter
_campusFilter(event){
const selection = event.target.options[event.target.selectedIndex];
let filteredData;
if (selection.value === "0") {
filteredData= this.state.courses;
return;
}
else{
filteredData = this.state.courses.filter(item=>{
return (item.CAMPUS_NAME === event.target.value);
});
}
this.setState({
selectedCampus: event.target.value,
courses: filteredData
});
}
render() {
const courses= this.state.courses.map(course=>(<Course ID={course.ID}
coursename={course.SUBJECT} />));
const campusName=this._getUniqueCampusName();
const renderCourses = courses.map((course, index) => {
return <li key={index}>{<Course key={index} ID={course.ID} SUBJECT= {course.SUBJECT} CAMPUS_NAME={course.CAMPUS_NAME} />}</li>;
});
return (
<div className="App">
<div className="filter">
<select name="campus" onChange={this._campusFilter.bind(this)}>
<option value="0">Select a Campus</option>
{campusName}
</select>
</div>
<div className="Course">Courses: {renderCourses}
</div>
</div>
);
}
}
export default App;
Course.js
import React from 'react';
const course=props=>{
return(
<div className="Course">
<p>ID: {props.ID}</p>
<p>SUBJECT: {props.SUBJECT}</p>
<p>Campus: {props.CAMPUS_NAME}</p>
</div>
);
}
export default course;
reactjs
I created a dropdown box with a onChange event to filter data, for example ['campuse A','campus B', ...]. When I selected the dropdown value, the returned data works fine but my dropdown box only has the selected value, 'campus A' and default "Select" value. How to fix this problem? Thanks.
App.js
import React, { Component } from 'react';
import './App.css';
import Course from './Components/Course';
import axios from 'axios';
//import CampusName from './Components/CampusName';
class App extends Component {
constructor() {
super();
this.state ={
courses:,
selectedCampus:''
};
}
componentDidMount(){
axios.get('myapiURL')
.then(response=>{
this.setState({courses:response.data});
});
}
_getUniqueCampusName(){
const courses=this.state.courses;
const campusSelection=[...new Set(courses.map(item=>item.CAMPUS_NAME))];
return campusSelection.map((campus,index)=>(<CampusName key={index} value=
{campus} onChange="this._campusFilter.bind(this)"/>));
}
//campus filter
_campusFilter(event){
const selection = event.target.options[event.target.selectedIndex];
let filteredData;
if (selection.value === "0") {
filteredData= this.state.courses;
return;
}
else{
filteredData = this.state.courses.filter(item=>{
return (item.CAMPUS_NAME === event.target.value);
});
}
this.setState({
selectedCampus: event.target.value,
courses: filteredData
});
}
render() {
const courses= this.state.courses.map(course=>(<Course ID={course.ID}
coursename={course.SUBJECT} />));
const campusName=this._getUniqueCampusName();
const renderCourses = courses.map((course, index) => {
return <li key={index}>{<Course key={index} ID={course.ID} SUBJECT= {course.SUBJECT} CAMPUS_NAME={course.CAMPUS_NAME} />}</li>;
});
return (
<div className="App">
<div className="filter">
<select name="campus" onChange={this._campusFilter.bind(this)}>
<option value="0">Select a Campus</option>
{campusName}
</select>
</div>
<div className="Course">Courses: {renderCourses}
</div>
</div>
);
}
}
export default App;
Course.js
import React from 'react';
const course=props=>{
return(
<div className="Course">
<p>ID: {props.ID}</p>
<p>SUBJECT: {props.SUBJECT}</p>
<p>Campus: {props.CAMPUS_NAME}</p>
</div>
);
}
export default course;
reactjs
reactjs
asked Nov 12 '18 at 23:09
user788448user788448
727
727
I think I know why the dropdown list only has the selected value option because courses only contains filtered data in _getUniqueCampusName() function, line "const courses=this.state.courses;" . How to make a copy of the original courses?
– user788448
Nov 13 '18 at 0:20
add a comment |
I think I know why the dropdown list only has the selected value option because courses only contains filtered data in _getUniqueCampusName() function, line "const courses=this.state.courses;" . How to make a copy of the original courses?
– user788448
Nov 13 '18 at 0:20
I think I know why the dropdown list only has the selected value option because courses only contains filtered data in _getUniqueCampusName() function, line "const courses=this.state.courses;" . How to make a copy of the original courses?
– user788448
Nov 13 '18 at 0:20
I think I know why the dropdown list only has the selected value option because courses only contains filtered data in _getUniqueCampusName() function, line "const courses=this.state.courses;" . How to make a copy of the original courses?
– user788448
Nov 13 '18 at 0:20
add a comment |
0
active
oldest
votes
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271426%2fdropdown-box-value-lost-after-clicking-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271426%2fdropdown-box-value-lost-after-clicking-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I think I know why the dropdown list only has the selected value option because courses only contains filtered data in _getUniqueCampusName() function, line "const courses=this.state.courses;" . How to make a copy of the original courses?
– user788448
Nov 13 '18 at 0:20