I had the same problem. This happens because <th> cells are assigned as
sorters for columns based on their index in the order they are declared. So
for example, the 5th <th> is assigned to the 5th column. If you use multiple
header rows, colspans and rowspans then the indexes become misaligned. What
this really needs is an elegant solution that calculates a best guess for
each column taking into account rowspans and colspans but in the meantime
there are two relatively easy workarounds. 
The first is to replace <th> elements that should not be sortable with <td>
elements. You can assign them a class such as .th and style them up to look
like header cells. 

If you don't want to mess around with your markup then I have written a
small modification for the tablesorter that allows you to use metadata to
specifically assign <th> elements to certain columns. You just need to add
something like class="{sortIndex: 5}" to your <th> elements. Then you need
to replace the buildHeaders function with the following: 

function buildHeaders(table) {

    if(table.config.debug) { var time = new Date(); }

    var meta = ($.metadata) ? true : false, tableHeadersRows = [];

    for(var i = 0; i < table.tHead.rows.length; i++) {
tableHeadersRows[i]=0; };

    $tableHeaders = $("thead th",table);

    $tableHeaders.each(function(index) {

        var sortIndex;
        if($.metadata && $(this).metadata().sortIndex)
        {
            sortIndex = $(this).metadata().sortIndex;
        }
        else
        {
            sortIndex = index;
        }
        this.count = 0;
        this.column = sortIndex;
        this.order = formatSortingOrder(table.config.sortInitialOrder);

        if(checkHeaderMetadata(this) || checkHeaderOptions(table,sortIndex))
this.sortDisabled = true;

        if(!this.sortDisabled) {
            $(this).addClass(table.config.cssHeader);
        }

        // add cell to headerList
        table.config.headerList[sortIndex]= this;
    });

    if(table.config.debug) { benchmark("Built headers:", time);
log($tableHeaders); }

    return $tableHeaders;

};


Scott-221 wrote:
> 
> 
> I have a complex table header that contains multiple <tr> elements in
> the <thead>. An example is at http://donpoo.net/jquery/. The table
> renders okay but sorting doesn't work correctly. Clicking on the
> "Latency (ms)" column in the "Totals" group results in the column "Ops/
> s" in the "Read" group to get sorted.
> 
> I think this is due to using col spans with multiple <tr> elements in
> the <thead>. Is there a patch I can test out or does anyone have a
> suggestion on how to fxi?
> 
> 

-- 
View this message in context: 
http://www.nabble.com/jquery-tablesorter-2.0.1-with-tr-in-thead-tp15392363s27240p15502443.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to