【问题标题】:Render a table in the rows of another table in react在反应中渲染另一个表的行中的表
【发布时间】:2017-08-25 21:08:45
【问题描述】:

我想在 react 中创建这样的东西:

我创建了一个 renderTable 和一个 renderInside 函数。在 renderTable 中,我这样调用 renderInside:

const renderInside = (slipsList) => {
  if (slipsList) {
    return (
      <table className="table table-bordered">
        <thead>
          <tr>
            <th className="table__header">
              <div className="table__header__text">
                <span className="table__header--selected">
                  Symbol
                </span>
              </div>
            </th>
          </tr>
        </thead>
        <tbody>
          {slipsList.map((slip, i) =>
            <tr key={i}>
              <td>{slip.amount}</td>
            </tr>
          )}
        </tbody>
      </table>
    );
  }
  return (
    <div>Loading...</div>
  );
};

而renderTable是这样的:

const renderTable = (slipsList) => {
  if (slipsList) {
    return (
      <div className="table-scrollable-container">
        <div className="table-scrollable">
          <table className="table table-bordered">
            <thead>
              <tr>
                <th className="table__header">
                  <div className="table__header__text">
                    <span className="table__header--selected">
                      Date Valeur
                    </span>
                  </div>
                  <div className="table__header__arrow" />
                </th>
              </tr>
            </thead>
            <tbody>
              {slipsList.map((slip, i) =>
                <tr key={i}>
                  <td className="table-sticky--first-col">
                    {slip.confirmationDate}
                  </td>
                  <td>
                    {slip.netAmountEuro}
                  </td>
                  <tr className="collapsed">
                    <td colSpan="10">
                    {renderInside(slipsList)}
                  </td>
                </tr>
                </tr>
              )}
            </tbody>
          </table>
        </div>
      </div>
    );
  }
  return (
    <div>Loading...</div>
  );
};

但它不起作用。我使用另外两种方法来做到这一点,但我想要。对于每一行或主表,我必须放置辅助表。关于如何做到这一点的任何想法?

【问题讨论】:

  • 它显示什么吗??
  • 不显示关于 jsx 封闭标签的错误。
  • 你能显示错误吗?
  • 相邻的 JSX 元素必须包含在封闭标签中

标签: javascript reactjs nested html-table


【解决方案1】:

尝试使用这个:

renderInside 方法中:

{slipsList.map((slip, i) => (  //added this bracket
      <tr key={i}>
         <td>{slip.amount}</td>
      </tr>
    )
 )}

renderTable方法中:

{slipsList.map((slip, i) => (   //added this bracket
    <tr key={i}>
      <td className="table-sticky--first-col">
        {slip.confirmationDate}
      </td>
      <td>
        {slip.netAmountEuro}
      </td>
      <tr className="collapsed">
        <td colSpan="10">
          {renderInside(slipsList)}
        </td>
      </tr>
    </tr>
  )
)}

还有一件事,我认为您需要在{renderInside(slipsList)} 方法中传递slip 而不是slipsList

【讨论】:

  • 这样吗? {renderInside(slipsList, slip)}
  • 告诉我一件事,你想将相同的slipsList 数组传递给其他函数还是数组的每个值??
  • 两个数组的值都包含在slipsList中。因此,通过我的方法,我想将slipsList 与其他功能相同。也许我错了
  • 你能在rendertable方法中显示你的数据console.log('slipsList', slipsList);吗?
  • Array(22) 0:对象 1:对象 2:对象 3:对象 4:对象 5:对象 6:对象 7:对象 8:对象 9:对象 10:对象 11:对象 12:对象 13:对象 14:对象 15:对象 16:对象 17:对象 18:对象 19:对象 20:对象 21:对象
猜你喜欢
  • 2018-09-24
  • 2019-07-27
  • 1970-01-01
  • 2018-09-02
  • 2020-01-03
  • 2018-06-10
  • 1970-01-01
  • 2018-01-08
  • 2019-12-05
相关资源
最近更新 更多