Ashique
Ashique

Reputation: 91

How do I embed a dynamic Dropdown menu inside a table cell in React JS?

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

Upvotes: 0

Views: 10396

Answers (1)

Bahtiyar
Bahtiyar

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 :

enter image description here

Upvotes: 2

Related Questions