Create a table from JSON completely dynamically

前端 未结 2 565
囚心锁ツ
囚心锁ツ 2021-01-28 17:41

I want to create entire table from a JSON array include dynamic ths, so a decoupled the head part to:

import React from \'react\';
import TableDataT         


        
相关标签:
2条回答
  • 2021-01-28 18:22

    There is a typo again in your <td>..</td> part, missing {} for your expression. For the dynamic solution after first map you need to map your Objects as well.

    <tr key={index}>
        <td>
            {
                Object.keys(currElement).map(key => currElement[key])
            }
        </td>
    </tr>
    
    0 讨论(0)
  • 2021-01-28 18:29

    You need to access the table data using key name as

    let rows = this.props.tableData.map((currElement, index) => {
          return (
          <tr>
              { Object.keys(currElement).map((item) =>
              (
                <td>{currElement[item]}</td>
         )
           )}
          </tr>   
          )
        });
    
    0 讨论(0)
提交回复
热议问题