Tips for building nested columns within React Bootstrap Table

I have been working with react bootstrap table, you can find more information here

You can check out the code in action by clicking here

My goal was to create a table that resembles the one shown below:

https://i.sstatic.net/jsZKe.png Here is an example of what my data looks like:

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: {
          math: 30,
          english:24,
          science:50,
          arts:10
        },
        age:14
      }
    ]

The code I used to create this table is as follows:

<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>

However, I am facing an issue where the subjects are not displaying in 4 separate columns. Can someone please help me fix this table?

Answer №1

Apologies for the delayed response, you may have already found a solution by now. I was able to recreate the table quickly and achieved success using the following HTML code:

Basically, the colgroup tag is utilized to group multiple columns together, which are then referenced in the header column as demonstrated below. Hopefully, this explanation clarifies things.

<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>

Here is an illustration of how the table appears:

Answer №2

To enhance the layout, consider incorporating the 'colspan' attribute and introducing an extra row beneath it like so:

<th rowspan={2} colspan={4} >Subjects</th>
<tr>
    <td>Mathematics</td>
    <td>English Literature</td>
    <td>Physics</td>
    <td>Fine Arts</td>
</tr>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensuring Compliance with GDPR through Cookie Consent Logic

Since the introduction of GDPR, I find myself in need of clarity on the steps to take both server-side and client-side to ensure compliance. Apologies for the plethora of questions. I currently have a first-party cookie that is used to store a session coo ...

Show the extracted data on the following web page once it has been cleaned

I am looking to connect a python file with an HTML page using Flask application. On the first page (upload.html), the user is required to select the job field and job title from two dropdown menus. Afterwards, the user needs to upload a file containing a l ...

Is there a solution to resolving the error message that mentions the deprecation of findDOMNode in StrictMode which appears each time I interact with the mui datatable?

Whenever I click on the muidatatable, an error message pops up. Is there a way to fix this issue? Will it lead to future problems when deploying the project? index.js:1 Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of ...

Steps for loading a different local JavaScript file by clicking on a button

My goal is to reload the browser page and display a different ReactJS component/file when a button is pressed. Initially, I attempted using href="./code.js" or window.location="./code.js" within the button props, but unfortunately, it did not yield the des ...

Align the image in the middle using JavaScript for Firebase

I am struggling to display the center of an image within a circle-shaped <img> tag with a border-radius of 50%. The issue arises when trying to display an image fetched from Firebase storage, rather than from a URL. In attempt to solve this problem, ...

The Freemode feature in SwiperJS is not functioning properly when used with React TypeScript

Having a slight issue with SwiperJS. Using version 10.1.0 and the following code : import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/css"; export default function Discover() { return ( <> ...

Utilizing the onscroll feature to trigger a function within a ScrollView

I am facing an issue with an animated view where I need to execute multiple events within the onScroll property. The current onScroll implementation is as follows: onScroll={ Animated.event( [{ nativeEvent: { conten ...

The text on my website is experiencing a cropping issue on the right side

I'm having an issue where the right side of all the text on my website is being cut off. I've tried using Firebug to inspect the element, but it's always a paragraph text and they have different parent divs. I'm not sure what CSS adjust ...

Using Angular components to manipulate the CSS in the DOM

Struggling with dom manipulation in an angular/node.js environment using typescript for my visualcomponent.html The second inline styling works fine, showing the h1 in blue color. However, I run into issues when trying to embed strings within the innerHTM ...

Incorporating a Bootstrap input group within a <td> element of a table

Encountering an issue when attempting to include a bootstrap input-group-addon with the input field nested inside a <td> tag within a table. Once all necessary classes are applied, there appears to be some spacing between the sign and the input field ...

Tips for customizing the main select all checkbox in Material-UI React data grid

Utilizing a data grid with multiple selection in Material UI React, I have styled the headings with a dark background color and light text color. To maintain consistency, I also want to apply the same styling to the select all checkbox at the top. Althou ...

How do I align the output of a <script> using CSS?

I'm facing some issues trying to center an external script on my webpage. It involves a Google widget using Google Maps to provide directions to our office. Below is the code snippet I am working with: <asp:Content ID="Content1" ContentPlaceHolder ...

What Is Preventing my Bootstrap Tabs from Displaying their Content?

I've been attempting to implement the nav-tabs feature from Bootstrap (version 5.2.2) into my Angular application. However, I've encountered an issue where only the content in the first tab is visible, while the contents of the other tabs remain ...

get direct access to the children of a specific div element

Here is the code snippet in HTML format. <div id="this"> <a href="xxx"> xx</a> <a href="yy"> yy</a> <a href="zzz"> zzz</a> aaa <a href="bbb"> bbb</a> ccc </div> I am look ...

CSS - Organization of Rows and Columns

I am in the process of designing a website that will feature news content, and I would like to give it a layout similar to a reddit news box. The layout would resemble the following structure. I am using angular material and CSS exclusively, without any fr ...

Guide to aligning a div element along the right edge of the webpage

I'm struggling to align this div all the way to the right side of the screen. Below is the HTML: <html> <head> title></title> <link rel="stylesheet" type="text/css" href="../css/style.css"/> </head> <h1&g ...

align text vertically over an image

I've come across this question many times, but none of the answers seem to solve my issue. My challenge is centered around aligning text on an image with regards to vertical positioning. Below is the code I'm working with: <div class="col-sm- ...

Enhance npm package by implementing custom functionality with React Component

I have designed an app that bears a striking resemblance to the following code: class MyCustomApp extends React.Component { constructor (props) { super(props) this.state = { tags: [ { id: 1, name: "Oranges" }, { id: 2, ...

Picture failed to load

I am struggling to get an image to display on my webpage, as only the alt text is appearing. Here is the code I am using: return ( <> <div className="container my-5"> <div className="row ju ...

Is there a way to refresh CSS styles when the window width is adjusted?

Is there a way to refresh CSS styles when the window width changes? I attempted this method, but unfortunately it did not work as expected. A simple refresh (F5) helps to rectify the CSS tags. jQuery(function($){ var windowWidth = $(window).width(); ...