React.js - 创建简单表

时间:2016-09-13 04:58:42

标签: javascript reactjs

很抱歉看起来像是一个新手问题(一直在工作到很晚,刚刚开始使用React)但是我想弄清楚如何用 n x <来渲染一个简单的表strong> n 维度。

例如,在我的组件中,渲染输出将是这样的:

      <table id="simple-board">
        <tbody>
          <tr id="row0">
            <td id="cell0-0"></td>
            <td id="cell0-1"></td>
            <td id="cell0-2"></td>
          </tr>
          <tr id="row1">
            <td id="cell1-0"></td>
            <td id="cell1-1"></td>
            <td id="cell1-2"></td>
          </tr>
          <tr id="row2">
            <td id="cell2-0"></td>
            <td id="cell2-1"></td>
            <td id="cell2-2"></td>
          </tr>
        </tbody>
      </table>

其中每一行都有自己的ID,以及每个单元格。

初始状态

  constructor(props){
    super(props);
    this.state = {size: 3}
  }

是设置表格大小的内容。

让我感到害怕的是如何在JSX中实现for循环。

5 个答案:

答案 0 :(得分:18)

经过一夜安眠,我能够弄清楚:

import React, { Component } from 'react'

export default class Example extends Component {
  constructor(props){
    super(props);
    this.state = {size: 3}
  }
  render(){
    let rows = [];
    for (var i = 0; i < this.state.size; i++){
      let rowID = `row${i}`
      let cell = []
      for (var idx = 0; idx < this.state.size; idx++){
        let cellID = `cell${i}-${idx}`
        cell.push(<td key={cellID} id={cellID}></td>)
      }
      rows.push(<tr key={i} id={rowID}>{cell}</tr>)
    }
    return(
      <div className="container">
        <div className="row">
          <div className="col s12 board">
            <table id="simple-board">
               <tbody>
                 {rows}
               </tbody>
             </table>
          </div>
        </div>
      </div>
    )
  }
}

感谢大家的回复!

答案 1 :(得分:1)

这样的事情:

  <table id="simple-board">
    <tbody>
     {this.props.yourData.slice(0,this.state.size).map((item , index)=>{

        return(
      <tr key={index} id={`row${index}`}>
         {item.felanBisar.map((item2,index2)=>{
          return(
             <td id={`cell${index}-{index2}`}></td>
           );
          })}
      </tr>
       );
      })}
    </tbody>
  </table>

答案 2 :(得分:0)

一个选项(将内容移出render()

import React from 'react';
import SimpleListMenu from '../menu/SimpleMenuListMenu'; // < Material UI element

let rows = [
  'Setting One',
  'Setting Two',
  'Setting Three',
  'Setting Four',
];

export default class MyTable extends React.Component {

  createTable = () => {

    let table = []
    for (let i = 0; i < rows.length; i++) {
      let children = []
      children.push(<td>{rows[i]}</td>, <td>{<SimpleListMenu />}</td>)
      table.push(<tr>{children}</tr>)
    }
    return table

  }

  render() {
    return(
      <table>
        {this.createTable()}
      </table>
    )
  }

}

另一个选择:

import React from 'react';

let id = 0;
function createData(option, type) {
  id += 1;
  return { id, option, type };
}

let rows = [
  createData('Setting One', 'Private'),
  createData('Setting Two', 'Public'),
  createData('Setting Three', 'Group'),
  createData('Setting Four', 'Private'),
];

export default class MyTable extends React.Component {


  render() {
    return(
      <table>
      {rows.map(row => (
        <tr key={row.id}>
          <td>{row.option}</td>
          <td>{row.type}</td>
        </tr>
      ))}
      </table>
    )
  }

}

请参阅:https://material-ui.com/demos/tables/

答案 3 :(得分:0)

  private getRows(): any {

    let rows: any[] = [];

    if (this.state.data)
      this.state.data.map((element) => {
        rows.push(<tr>
            <td style={{border: '1px solid black'}}>
              {element.title}
            </td>
         </tr>);
      });

    return rows;
  }

  render( ) { 
                        
        return <div>           

          <table style={{border: '1px solid black'}}>

            {this.getRows()}

          </table>

        </div>                
        
    }

答案 4 :(得分:0)

从带有 arrayfunction 和带有备用 bgColor 的行构建表:

function buildTable(arr){
  const rows = arr.map((row,i) => { 
      return <tr style={{backgroundColor: i%2 ? '#F0FFF2':'white'}} key={i}>
                <td>{row[0]}</td><td>{row[1]}</td>
             </tr> 
  })
  return <table><tbody>{rows}</tbody></table>
}

const data = [["FieldA", "aaa"],["FieldB", "bbb"],["FieldC", "ccc"]];
buildTable(data);