Reputation: 91
I have a dynamic table with 4 columns. I want to make the fourth column a dynamic dropdown list in which the user should be able to change the value.
For creating a dynamic table I've followed the below tutorial.
https://blog.logrocket.com/complete-guide-building-smart-data-table-react/
I am a newbie to React.
This is how I need the table. Sorry for blurring the content.
Upvotes: 0
Views: 10396
Reputation: 192
Look This Example :
Table.Js
import React, { Component } from 'react'
class Table extends Component {
constructor(props) {
super(props)
this.state = {
students: [
{ id: 1, name: 'Wasif', age: 21, email: '[email protected]' },
{ id: 2, name: 'Ali', age: 19, email: '[email protected]' },
{ id: 3, name: 'Saad', age: 16, email: '[email protected]' },
{ id: 4, name: 'Asad', age: 25, email: '[email protected]' }
]
}
}
viewRow(id,e) {
alert('selectedId:'+ id);
localStorage.setItem('transactionId',id);
}
renderTableData() {
return this.state.students.map((student, index) => {
const { id, name, age, email } = student //destructuring
return (
<tr key={id}>
<td>{id}</td>
<td>{name}</td>
<td>{age}</td>
<td>{email}</td>
<td>
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</td>
<td><button onClick={(e) => this.viewRow(id, e)}>View Row Id</button></td>
</tr>
)
})
}
Result :
Upvotes: 2