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

The magic of CSS Pseudo-classes in Inline Styling

For the longest time, I've been under the impression that you cannot include in-line :hover{..} styles to an element. However, recently I stumbled upon this page which showcased examples like this. <a href="http://www.w3.org/" s ...

What could be the reason why both the add and remove functions are unable to work simultaneously within a JavaScript function?

Hi there! I recently started diving into JavaScript and encountered a little hiccup. I've been working on a dice game where images change randomly whenever a button is clicked. The images transition from one to another, but I wanted to add a rolling ...

Updating a Div with XML data through JQuery

Hey everyone, I have a simple question that I need help with... Currently, I am using jQuery to retrieve an XML document in the following format: $.ajax({ type: verb, url: url, dataType: datatype, success: callback }) } After receiving t ...

Chrome experiences a crash during regular expression matching operations

I have a challenge where I am attempting to validate 50 email addresses separated by commas using regex. Strangely, every time I run this operation, Chrome crashes. However, Safari seems to handle it without any issues. Below is the code snippet that I am ...

Retrieve the value of a PHP array within a for loop and transfer it to JQuery

*edited format I came across a PHP code for a calendar on the internet and I am currently working on implementing an onclick event that captures the date selected by a user as a variable (which will be used later in a database query). At this point, my g ...

Getting the (x,y) Coordinate Value from jQuery Script and Saving it as a NSString

div tag is essential for applying bold, italic, and various other formatting options in UIWebview. My goal is to retrieve the position coordinates when a user interacts with the div tag using JavaScript/jQuery. I stumbled upon the required code on JSFiddl ...

Tips for automatically refreshing a div in Drupal

I'm currently working with Drupal and I'm looking to update a specific field every X seconds without having to refresh the entire page. My current approach involves using the following code: <script> var refreshId = setInterval(function ...

Looking to enhance your navigation menu with drop shadows?

I am trying to add a drop shadow effect to the navigation menu on my WordPress website. You can view the desired effect here. However, I have attempted to apply it to my .navigation-main class without success. Below is the header code: <header id="mast ...

What methods can be used to restrict users from navigating to the previous or next page while they are scrolling horizontally within a scrollable div?

Experiencing a frustrating UX issue that arises from using a wide table within a scrollable div on a webpage. The problem occurs when users scroll horizontally using a trackpad on a mac, they sometimes unintentionally trigger the "go to next/last page" fe ...

What is the best way to choose two <li> elements with multiple classes in my WordPress navigation menu?

I am looking for a JavaScript function that will add the class "current_highlight" when an element with the class "activo2" also has the class "active". Here is my HTML code: <div class="navbar-header"> <button type="button " class="navbar-to ...

Update the image source with jQuery when hovering over it

Check out this code snippet: (view the fiddle here) <img class="changeable" src="http://placehold.it/100x120&text=anniversary"> <ul class="rollover_list"> <li><a href="#" rel="http://placehold.it/100x120&text=anniv ...

Error: The variable 'xxxxxx' is not recognized

Encountering an issue while trying to pass data via an onclick event and receiving the error message "Uncaught ReferenceError: 'xxxxxx' is not defined" var ShowDetails = function (insuredID) { $.ajax({ type: "GET", ...

Utilizing AMD Modules and TypeScript to Load Bootstrap

I am attempting to incorporate Bootstrap into my project using RequireJS alongside typescript AMD modules. Currently, my requireJS configuration looks like this: require.config({ shim: { bootstrap: { deps: ["jquery"] } }, paths: { ...

The mystery of the Accordion Effect: A Next.js/React.js issue where it slides up but refuses to

After implementing a custom accordion using next.js, I encountered an issue where the slide animation worked successfully when moving up and out upon clicking the button. However, when trying to move it back down into the content, it did not animate as exp ...

Use PHP to read and echo the entire contents of a file, then transmit it

Currently, I am utilizing the JQuery Form Plugin to facilitate file uploads. While I can successfully transmit the contents of an uploaded txt file to the associated jQuery code using the variable $c in my PHP script, I am interested in sending the entire ...

css optimizing image content for search engine visibility

My website's main page features a div with an image containing the message "contact us by calling 1 800 blabla..". I'm concerned that search engines may not be able to recognize or find my company's phone number since it is only displayed wi ...

How can I locate the element immediately preceding $(this)?

Struggling to retrieve the value of the $(.subname) when the .bigadminbutton is clicked and calling the updateSub() function. I have tried various methods like prev, sibling, parent, children, find, but none seem to work. This minor task is taking up too ...

Issues encountered when making ajax requests within a Phonegap mobile application

I'm currently working on building a basic RSS reader using Phonegap and jQuery. I found this helpful tutorial: . Initially, everything was functioning correctly when testing the code in my browser. The php-file successfully retrieved and displayed th ...

Is it possible to modify the border colors of separate elements in Bootstrap 5?

<div class="m-3 p-3 border-5 border-top border-danger border-bottom border-primary"> Can borders be styled with different colors? </div> Is there a method to achieve this, possibly using sass? ...

Iterate over the HTML elements within a class and retrieve a specific property in JSON

Currently, I am in the process of developing a straightforward application that involves making an Ajax request to retrieve a Json object and then passing it to an HTML document. Essentially, this application functions as a vending machine where the produc ...