最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - Creating table using loop in class method - Stack Overflow

programmeradmin1浏览0评论

I would like to generate a table in render() method of my class.

So in different example/project in past, I have pushed divs to an array and then return this array and it worked fine in terms of displaying but in this case when I call this.tableGenerator() it displays the result as a plain text, not rendered HTML.

Can you please tell me why this happens? Or maybe there is another method I can loop this table out?

tableGenerator = () => {

    let table = [];

    table.push('<tr>');
    //loop for columns

    table.push('</tr>');

    return table;

}

Here is example I was talking about: it generates a board of divs and now I want to change it to display as a table:

createMap = (cols, total) => {
    let table = []; let nL = ''; let idRow = 0; let idCol = 0;

    for (let i = 0; i < total; i++) {
      idCol++;
      if (i%cols === 0){
        nL = 'newLine';
        idRow += 1;
        idCol = 1;
      }
      else {
        nL = '';
      }

      let toggledBackground = '';

        switch (this.state.cellStates[i]) {
          case 1:
            toggledBackground = 'alive';
            break;

          case 2:
            toggledBackground = 'dead';
            break;

          default:
            toggledBackground = '';
        }

      table.push(<div id={"pos-"+idRow+"-"+idCol} className={"square "+nL+" "+toggledBackground} onClick={() => this.changeState(i)}></div>);
    }
    return table;
  }

I would like to generate a table in render() method of my class.

So in different example/project in past, I have pushed divs to an array and then return this array and it worked fine in terms of displaying but in this case when I call this.tableGenerator() it displays the result as a plain text, not rendered HTML.

Can you please tell me why this happens? Or maybe there is another method I can loop this table out?

tableGenerator = () => {

    let table = [];

    table.push('<tr>');
    //loop for columns

    table.push('</tr>');

    return table;

}

Here is example I was talking about: it generates a board of divs and now I want to change it to display as a table:

createMap = (cols, total) => {
    let table = []; let nL = ''; let idRow = 0; let idCol = 0;

    for (let i = 0; i < total; i++) {
      idCol++;
      if (i%cols === 0){
        nL = 'newLine';
        idRow += 1;
        idCol = 1;
      }
      else {
        nL = '';
      }

      let toggledBackground = '';

        switch (this.state.cellStates[i]) {
          case 1:
            toggledBackground = 'alive';
            break;

          case 2:
            toggledBackground = 'dead';
            break;

          default:
            toggledBackground = '';
        }

      table.push(<div id={"pos-"+idRow+"-"+idCol} className={"square "+nL+" "+toggledBackground} onClick={() => this.changeState(i)}></div>);
    }
    return table;
  }
Share Improve this question edited Jan 18, 2018 at 7:50 Mateusz Osuch asked Jan 18, 2018 at 7:36 Mateusz OsuchMateusz Osuch 3571 gold badge4 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You need something like this:

tableGenerator = () => {
  return (
    <table>
      <tr>
        {columns.map(column => <th>{column.data}</th>)}
      </tr>
    </table>
  );
}

Where column.data is the info you want to display (I used data as a general value but it could be label, name, info, etc).

发布评论

评论列表(0)

  1. 暂无评论