user3928461
user3928461

Reputation: 343

How to sort one column depend on the value of another : jquery tablesorter

I am using the tablesorter from motti for sorting the html table.I am unable to find way to sort a certain column depend on another column value. In my case ,wanna sort the FULLNAME column depend on the LASTNAME column value which is basically hidden.Is this possible ? Thanks :-)

  <table id="tblDetails">
<thead>
    <tr>
        <th>FULL NAME</th>
        <th style="display:none;">LAST NAME</th>
        <th>GENDER</th>
        <th>ADDRESS</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>James</td>
        <td style="display:none;">Peter</td>
        <td>male</td>
        <td>washington dc</td>
    </tr>
    <tr>
        <td>jennifer</td>
        <td style="display:none;">lopez</td>
        <td>female</td>
        <td>New york</td>
    </tr>
    <tr>
        <td>Harrison</td>
        <td style="display:none;">Ford</td>
        <td>male</td>
        <td>washington dc</td>
    </tr>
</tbody>

Upvotes: 0

Views: 417

Answers (2)

Gkrish
Gkrish

Reputation: 1891

Simple solution would be just adding a hidden span or label before the column (FULLNAME).It will sort depend on the first added value

      <table id="tblDetails">
    <thead>
        <tr>
            <th>FULL NAME</th>
            <th>GENDER</th>
            <th>ADDRESS</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><span style="display:none;">Peter</span>James</td>
            <td>male</td>
            <td>washington dc</td>
        </tr>
   <tr>
        <td><span style="display:none;">lopez</span>jennifer</td>
        <td>female</td>
        <td>New york</td>
    </tr>
    </tbody>
    </table>

Upvotes: 1

ricardofagodoy
ricardofagodoy

Reputation: 146

Looking up docs: https://mottie.github.io/tablesorter/docs/#methods It seems like you can handle the click event on a desired column (as any HTML element click) and then trigger the sort to another column:

$('table').find('th:eq(2)').trigger('sort');

Upvotes: 1

Related Questions