I am using react bootstrap table here
You can see the working code here
I wanted to created a table which looks like this .
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
}
]
I had used the following Code
<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>
But it is not showing the subjects in 4 columns. Please help me to create this table
2
Answers
Try using the attribute ‘colspan’ and create an additional row that will be below it e.g:
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.
An image that shows how it looks: