I have an array of degrees in Students collection as a part of a bigger model mongodb and in degrees array i have note like an element how can i use it in form like an input and add it in table
Create.js Component Form: Construtor with attributes types string and array
constructor() {
super();
this.state = {
firstname: '',
lastname: '',
degrees : [
{id '',
note ''
}
]
};
}
onSubmit = (e) => {
e.preventDefault();
const { firstname, lastname } = this.state;
axios.post('/students', { firstname, lastname })
.then((result) => {
this.props.history.push("/")
});
}
Rander method for the form
render() {
const { firstname, lastname , degrees } = this.state;
return (
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Add Student
</h3>
</div>
<div class="panel-body">
<h4><Link to="/"><span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> Students List</Link></h4>
<form onSubmit={this.onSubmit}>
<div class="form-group">
<label for="isbn">First Name:</label>
<input type="text" class="form-control" name="firstname" value={firstname} onChange={this.onChange} placeholder="First Name" />
</div>
<div class="form-group">
<label for="title">Last Name:</label>
<input type="text" class="form-control" name="lastname" value={lastname} onChange={this.onChange} placeholder="Last Name" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
);
}
App.js Component Table for listing the form like First Name| Last Name |Note
constructor(props) {
super(props);
this.state = {
students: []
};
}
componentDidMount() {
axios.get('/students')
.then(res => {
this.setState({ students: res.data });
console.log(this.state.students);
});
}
Render method for listing data
render() {
return (
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Students List
</h3>
</div>
<div class="panel-body">
<h4><Link to="/create"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> Add Student</Link></h4>
<table class="table table-stripe">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Note</th>
</tr>
</thead>
<tbody>
{this.state.students.map(c =>
<tr>
<td><Link to={`/show/${c.id}`}>{c.firstname}</Link></td>
<td>{c.lastname}</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
</div>
);
}