如何在React Bootstrap表格中创建嵌套列

huangapple go评论60阅读模式
英文:

How to create nested columns in React Bootstrap Table

问题

我正在使用React Bootstrap表格这里

您可以在此处查看工作代码

我想创建一个看起来像这样的表格。

如何在React Bootstrap表格中创建嵌套列
我的数据如下:

const data = [
  {
    rollId: 1,
    name: "Momo",
    marks: {
      maths: 30,
      english: 24,
      science: 50,
      arts: 10
    },
    age: 14
  },
  {
    rollId: 2,
    name: "Sana",
    marks: {
      maths: 30,
      english: 24,
      science: 50,
      arts: 10
    },
    age: 14
  },
  {
    rollId: 3,
    name: "Mark",
    marks: {
      maths: 30,
      english: 24,
      science: 50,
      arts: 10
    },
    age: 14
  }
]

我已经使用了以下代码:

<Table
  striped
  bordered
  hover
  responsive
  bgcolor="white"
  size="lg"
  style={{ opacity: "0.8" }}
>
  <thead
    style={{ backgroundColor: "#198754" }}
    className="text-white"
  >
    <tr>
      <th rowSpan={2}>ROll ID</th>
      <th rowSpan={2}>Name</th>
      <th rowSpan={2} colSpan={4}>
        <tr>
          <th colSpan={4}>Marks</th>
        </tr>
        <tr>
          <th rowSpan={1}>Maths</th>
          <th rowSpan={1}>English</th>
          <th rowSpan={1}>Science</th>
          <th rowSpan={1}>Arts</th>
        </tr>
      </th>
      <th rowSpan={2}>Age</th>
    </tr>
  </thead>
  <tbody>
    {data.map((dat) => {
      return (
        <tr key={dat.rollId}>
          <td key={dat.rollId}>{dat.rollId}</td>
          <td>{dat.name}</td>
          <td>{dat.marks.maths}</td>
          <td>{dat.marks.english}</td>
          <td>{dat.marks.science}</td>
          <td>{dat.marks.arts}</td>
          <td >{dat.age}</td>
        </tr>
      );
    })}
  </tbody>
</Table>

但它没有显示4列中的科目。请帮助我创建这个表格。

英文:

I am using react bootstrap table here

You can see the working code here

I wanted to created a table which looks like this .

如何在React Bootstrap表格中创建嵌套列
My data looks like this

const data = [
{
rollId: 1,
name:&quot;Momo&quot;,
marks: {
maths: 30,
english:24,
science:50,
arts:10
},
age:14
},
{
rollId: 2,
name:&quot;Sana&quot;,
marks: {
maths: 30,
english:24,
science:50,
arts:10
},
age:14
},
{
rollId: 3,
name:&quot;Mark&quot;,
marks: {
maths: 30,
english:24,
science:50,
arts:10
},
age:14
}
]

I had used the following Code

&lt;Table
striped
bordered
hover
responsive
bgcolor=&quot;white&quot;
size=&quot;lg&quot;
style={{ opacity: &quot;0.8&quot; }}
&gt;
&lt;thead
style={{ backgroundColor: &quot;#198754&quot; }}
className=&quot;text-white&quot;
&gt;
&lt;tr&gt;
&lt;th rowSpan={2}&gt;ROll ID&lt;/th&gt;
&lt;th rowSpan={2}&gt;Name&lt;/th&gt;
&lt;th rowSpan={2} colSpan={4} &gt;
&lt;tr&gt;
&lt;th colSpan={4}&gt;Marks&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th rowSpan={1}&gt;Maths&lt;/th&gt;
&lt;th rowSpan={1}&gt;English &lt;/th&gt;
&lt;th rowSpan={1}&gt;Science&lt;/th&gt;
&lt;th rowSpan={1}&gt;Arts&lt;/th&gt;
&lt;/tr&gt;
&lt;/th&gt;
&lt;th rowSpan={2}&gt;Age&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{data.map((dat) =&gt; {
return (
&lt;tr key={dat.rollId}&gt;
&lt;td key={dat.rollId}&gt;{dat.rollId}&lt;/td&gt;
&lt;td&gt;{dat.name}&lt;/td&gt;
&lt;td&gt;{dat.marks.maths}&lt;/td&gt;
&lt;td&gt;{dat.marks.english}&lt;/td&gt;
&lt;td&gt;{dat.marks.science}&lt;/td&gt;
&lt;td&gt;{dat.marks.arts}&lt;/td&gt;
&lt;td &gt;{dat.age}&lt;/td&gt;
&lt;/tr&gt;
);
})}
&lt;/tbody&gt;
&lt;/Table&gt;

But it is not showing the subjects in 4 columns. Please help me to create this table

答案1

得分: 1

抱歉回复晚了,也许你现在已经找到了解决方案。就代码而言,我已经快速复制了表格,并使用以下HTML代码成功运行:

基本上,您可以使用colgroup标签将多个列分组在一起,然后在标题列中使用这些分组,如下所示。希望这有意义。

<table class="table table-primary table-striped-columns">
    <colgroup span="4"></colgroup>
    <thead>
        <tr>
            <th rowspan="2">ROll ID</th>
            <th rowspan="2">Name</th>
            <th colspan="4" scope="colgroup">Marks</th>
        </tr>
        <tr>
            <th scope="col">Maths</th>
            <th scope="col">English</th>
            <th scope="col">Science</th>
            <th scope="col">Arts</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>John</td>
            <td>5</td>
            <td>10</td>
            <td>3</td>
            <td>7</td>
        </tr>
    </tbody>
</table>

以下是显示其外观的图像:

如何在React Bootstrap表格中创建嵌套列

英文:

Sorry for the overdue reply, you might have come up with a solution by now. For what it is worth, I have replicated the table quickly and it worked using the HTML code below:

Essentially, you use the colgroup tag to group a number of columns together which then you will use in the header column as shown below. Hope it makes sense.

&lt;table class=&quot;table table-primary table-striped-columns&quot;&gt;
&lt;colgroup span=&quot;4&quot;&gt;&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th rowspan=&quot;2&quot;&gt;ROll ID&lt;/th&gt;
&lt;th rowspan=&quot;2&quot;&gt;Name&lt;/th&gt;
&lt;th colspan=&quot;4&quot; scope=&quot;colgroup&quot;&gt;Marks&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot;&gt;Maths&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;English&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;Science&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;Arts&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

An image that shows how it looks:

如何在React Bootstrap表格中创建嵌套列

答案2

得分: 0

尝试使用属性'colspan',并创建一个位于其下方的额外行,例如:

<th rowSpan={2} colspan={4}>分数</th>
<tr>
<td>数学</td>
<td>英语</td>
<td>科学</td>
<td>艺术</td>
</tr>
英文:

Try using the attribute 'colspan' and create an additional row that will be below it e.g:

&lt;th rowSpan={2} colspan={4} &gt;Marks&lt;/th&gt;
&lt;tr&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;English&lt;/td&gt;
&lt;td&gt;Science&lt;/td&gt;
&lt;td&gt;Arts&lt;/td&gt;
&lt;/tr&gt;

huangapple
  • 本文由 发表于 2023年2月27日 17:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定