0

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.

enter image description here

1 Answer 1

3

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 :

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

That worked :) . I have a nested JSON array of data, how do I specify the accessor and get those values?
you should set state of dropdown value in each row . there is good example here. check this. : stackoverflow.com/questions/39614373/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.