Sorting table data by Table SubHeadings using Jquery

Utilizing jQuery tablesorter in my UI, I encountered a challenge with a table that has 2 rows of headers - one main header and one subheader. My goal is to enable sorting on the subheader. How can I achieve this?

Below is an example of my code structure:

<table border="1">
<thead>
<tr>
    <td>&nbsp;</td>
    <td colspan="3" align="center">English</td>
    <td colspan="3" align="center">Math</td>
    <td colspan="3" align="center">Science</td>                                                                                            
</tr>
<tr>
    <th>School Name</th>
    <th>Test 1</th>
    <th>Test 2</th>
    <th>Test 3</th>

    <th>Test 1</th>
    <th>Test 2</th>
    <th>Test 3</th>

    <th>Test 1</th>
    <th>Test 2</th>
    <th>Test 3</th>
</tr>
</thead>
<tbody>           
<tr>
    <th>School 1</th>
    <th>10</th>
    <th>10</th>
    <th>10</th>

    <th>10</th>
    <th>10</th>
    <th>10</th>

    <th>10</th>
    <th>10</th>
    <th>10</th>
</tr>    
<tr>
    <th>School 2</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
    <th>9</th>
</tr>  
</tbody>      

Answer №1

Out-of-the-box functionality is expected for tablesorter.

To see a demonstration using the original version of tablesorter, click here.

For a demo using my customized tablesorter fork, click here.

$(function () {
    $('table').tablesorter({
        theme: 'blue',
        widgets: ['zebra']
    });
});

In both scenarios, using th's in the tbody can disrupt the table's appearance. It is recommended to utilize td's in the tbody instead.

Additionally, there is a noticeable distinction between the original and the fork versions. In the forked counterpart, clicking on the main header sorts all columns it contains. To prevent this behavior, add a "sorter-false" class to the main header cell. This feature is not present in the original plugin.

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

I attempted to activate the hover effect on a DOM element using JavaScript, but was unsuccessful. It appears that achieving this is not possible. However, I am curious as to how Chrome is able to

I attempted to activate the hover state of a DOM element using JavaScript, but had no success. It appears that this task is not achievable in the way I initially approached it. I have heard that creating a class with a hover function and then adding or ...

Bootstrap dropdown menu fails to adapt to updated navbar height

I recently made a modification to the navbar size on my website, increasing it from 50px to 63px by tweaking a line in the bootstrap.css file. The exact code snippet I utilized for this adjustment is as follows: .navbar { position: relative; min ...

Searching for a column fails to yield any results after altering the value in

Here is a snippet of code that I am working with: <!DOCTYPE HTML> <html lang="en"> <head> <title> Exact matches overview </title> <script type="text/javascript" src="/static/s ...

Generate gzipped files using Angular CLI

I am attempting to populate a dist folder with the standard files along with their .gz versions. To achieve this, I used ng eject to obtain the webpack.config.js file in order to integrate the compression plugin from https://github.com/webpack-contrib/comp ...

Target only the <a> elements within a specific <div> using JavaScript

How can I target the first occurrence of the letter 'a' in this code using JavaScript? (href=some.php) <div class="accordionButton"><div id="acr_btn_title"><a href="some.php"><p>stuff</p></a></div><di ...

Most effective method for incorporating external content onto website without using iframes (all websites are owned by me)

Managing a collection of websites is no easy task, especially when they are all powered by WordPress. One common feature among these sites is a simple "name, graphic, and social media links" page located at foo.domain.com/partner. Now, I'm looking to ...

"Troubleshooting the issue of null values in ASP.NET Core Razor Pages when using Ajax POST

I'm currently working on an ASP.NET (6.0) Razor Pages project where I am facing an issue with posting data using AJAX. Despite my efforts, the posted data always ends up being null. Even after going through similar questions on Stack Overflow, I have ...

Styling a component next to another using CSS

Struggling with a simple question here. I'm experimenting with HTML and CSS, trying to create a page with a central content box flanked by two arrow images on each side. While the concept isn't too difficult, I'm running into issues with usi ...

What could be causing the DIV to be out of place when the window is resized?

When the window is minimized, all elements remain intact except for the navigation. The image below shows the layout when the window is maximized, while the image above depicts the layout when the window is minimized. HTML: <header> <div c ...

Place the border image underneath the div element

I successfully added a border image to the right side of my div, but now I want it to extend slightly below the div. Is there a way to increase the height of the border image? Or should I just float the image next to the div and how would I go about doing ...

Adjusting Font Size for Buttons on Mobile Devices in HTML

I am currently working on a website that I want to ensure is mobile-friendly. When testing it on my phone and in Chrome's device mode, I noticed that the majority of the text on the screen was displayed at an easily readable size without needing to zo ...

Viewport height-responsive image not functioning as expected in Firefox browser

Is there a way to set an image to the 100% height of the browser window, with a small padding at the bottom, and have it centered on the page? I created an example in codepen that works well in Chrome and Safari, but not in Firefox. In Firefox, the image ...

Utilizing a button to trigger a directive nested within another directive?

My Angular code involves creating a div using the 'box1' directive, which contains a button. When this button is clicked, I need it to trigger another directive called 'box2', which should be inserted inside 'box1'. How can I ...

Want to learn how to display a description below a photo when you hover over it in a photo gallery?

I am currently creating a basic photo gallery where hovering over a thumbnail displays an enlarged photo below it. My goal is to also have text displayed inside a white text box when hovering over each thumbnail, with unique descriptions for each. I need ...

The perplexing phenomena of Ajax jQuery errors

Hey there! I'm having a bit of trouble with ajax jquery and could use some guidance. $.ajax({ type:"get", url:"www.google.com", success: function(html) { alert("success"); }, error : function(request,status,error) { alert(st ...

Emphasize the current page's menu item for the user viewing it

I'm looking for a way to visually emphasize the current menu item so that users can easily identify the page they are currently on. This could be achieved through bolding or underlining, whichever works best. I'm hoping to accomplish this using o ...

Explore by the anchor tag

I've recently implemented a search bar utilizing Bootstrap. This is the code for the search bar: <div class="md-form mt-0"> <input class="form-control" id="myInput" type="text" placeholder="Sear ...

Arrange objects in dropdown menu to line up

I'm currently working on a dropdown menu and I have a specific requirement – the menu should always be split into two columns and be able to span multiple lines. However, I've encountered an issue where the columns are not aligned properly, cau ...

What is the best way to assign a jQuery variable to a PHP variable?

After spending some time searching and working on it, I still can't figure out the answer to this straightforward question. Here is the code I have been struggling with. <script> function calculate() { var total = (parseInt($('#stude ...

Fetching JSON data and showcasing it

I am experiencing an issue with displaying JSON data dynamically. I have over 80 rows of data and when I try to display the names dynamically, they all end up being the same. It seems to be taking the last data in the list. $(".mem-wrap").each(function ...