如何在React-bootstrap中创建包含多行单元格的表格?

时间:2017-06-22 08:23:50

标签: javascript twitter-bootstrap reactjs react-redux

我想创建一些表格,其中一些单元格包含多行。 如果我这样做,它的工作:

               <Table bordered>
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Analysed  ID</th>
                            <th>Analysed Name</th>
                            <th>Solve To change</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td rowSpan="3">Date</td>
                        </tr>
                        <tr>
                            <td>ID</td>
                            <td>Name</td>
                            <td>Decision</td>
                        </tr>
                        <tr>
                            <td>ID</td>
                            <td>Name</td>
                            <td>Decision</td>
                        </tr>

                    </tbody>

                </Table>

我明白了: Table with multiline cell


现在我想添加我的3&#34; TR&#34;标签在一个组件中,因为在我想要使用for-cycle来创建许多这样的组件之后。但组件必须在一个封闭标签中返回内容。我试图控制我的3&#34; tr&#34;在一个父母&#34; tr&#34;,但我得到了错误。我能在这做什么?

2 个答案:

答案 0 :(得分:4)

无法创建一个React Component,它返回三个元素而不将它们包装在另一个元素中,例如div。否则,您将收到以下错误:

A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.

您的案例有点特殊,因为您不能将div作为tabletbody的直接子女,因此这是一个问题......

可以做的是创建一个返回数组的类函数。像这样:

class MyApp extends React.Component {

  getTr = () => {
    return [
      <tr key={0}>
        <td rowSpan="3">Date</td>
      </tr>,
      <tr key={1}>
        <td>ID</td>
        <td>Name</td>
        <td>Decision</td>
      </tr>,
      <tr key={2}>
        <td>ID</td>
        <td>Name</td>
        <td>Decision</td>
      </tr>
    ];
  }

  render() {
    return (
      <table className="table">
        <thead>
          <tr>
            <th>Date</th>
            <th>Analysed  ID</th>
            <th>Analysed Name</th>
            <th>Solve To change</th>
          </tr>
        </thead>
        <tbody>
          {this.getTr()}
          {this.getTr()}
          {this.getTr()}
        </tbody>
      </table>
    );
  }
}

ReactDOM.render(<MyApp />, document.getElementById("app"));
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

答案 1 :(得分:0)

您需要在一个div标签中包含tr标签。 rowSpan的正确方法是:

var MyRow = React.createClass({
  render: function () {
    return (
        <div>
            <tr>
              <td rowSpan="2">{this.props.item.date}</td>
              <td>{this.props.item.data[0].id}</td>
              <td>{this.props.item.data[0].name}</td>
              <td>{this.props.item.data[0].solve}</td>
            </tr>
            <tr>
              <td>{this.props.item.data[1].id}</td>
              <td>{this.props.item.data[1].name}</td>
              <td>{this.props.item.data[1].solve}</td>
            </tr>
        </div>
    );
  }
});

这是我的工作示例:http://jsfiddle.net/andrea689/e33pd14L/