Hamish Grubijan
Hamish Grubijan

Reputation: 10820

JQuery Tablesorter - leave the first row intact

I am using the jQuery Tablesorter 2.0 plugin to create a sortable table of ... say books. The very first row of the table will be a summary of all books, and I want it to always be at the top.

I need to customize the behavior of the tablesorter, but I do not think I can/should do that with a custom parser. What I think I need is a custom comparator function - one that would allow me to compare any two rows myself, ideally also being able to invoke the default behavior if the condition that I am looking for has not been met.

Sample algorithm would be like so (hopefully I did not invert my result):

function customSort(lhs, rhs, table, column_index) {
    if (lhs === 'magic_str') {
        return -1; // Magic string wins
    }
    if (rhs === 'magic_str') {
        return 1; // Magic string wins
    }
    return defaultSort(lhs, rhs, table, column_index);
}

Now, I know some JS but not enough to pry a popular library open and rewrite parts of it, particularly as a patch that would go on top, after I load the original. (that would be ideal). Also, if I can accomplish this sort of behavior in any other way, I am all ears.

Upvotes: 4

Views: 2525

Answers (1)

Mottie
Mottie

Reputation: 86413

An easier solution would be to try out my fork of tablesorter and just put that row you don't want to sort into a separate tbody - this won't work with the original tablesorter.

<table class="tablesorter">
    <thead>
        ...
    </thead>
    <tbody class="tablesorter-infoOnly">
        ... <!-- non-sorting tbody -->
    </tbody>
    <tbody>
        ...
    </tbody>
</table>

Since it is only one row, you don't really need to add the "tablesorter-infoOnly" class name; it does prevent column styling being applied to it. Try out this demo and remove the tbody class name to see the difference.

And FYI, you can add any number of tbodies to the table.

Upvotes: 6

Related Questions