tags:

views:

164

answers:

2

Hello, this is an ongoing project and I'm just about done. I've been developing a custom table sorter based on the tablesorter plugin for jQueryt. It's just about done, thankfully. My last question is this, I have a table header as follows:

<th class="blue_bg"><a rel = "Header" href="#" title="Sort column in decending order" class="">Seats Available</a></th>

I would like to add the inline metadata parser for disabling sorting on a particular column. I currently have a traversing function that goes through each header, without a class, and adds the parser as follows:

    //th without a class automatically get sorting disabled
    $("th[class='']").each(function(){
    $(this).addClass("{sorter: false}");
});

I also have one that goes through and searches for a specific string that indicates whether a column should be disabled from sorting:

    //Add disabling parser to each header with a disable class
    $("th[class*='csuci.sortable.false']").each(function(){
    $(this).removeClass("csuci.sortable.false").addClass("{sorter: false}");
});

Basically, I want it setup so that, unless the user specifies otherwise, by default or by a user defined string, a column will have sorting disabled. So to recap, I have sorting disabled for columns that have no class at all and that have a class that specifies a disable string. I would also like a third condition where a user can have any other class in a header, in this example the user specifies a class to make the background of the column blue. But, I'm not sure what syntax I need to add the disable parser to this example header. Help is appreciated, thanks in advance.

UPDATE: Here is my complete function which adds parsers based on whatt's in, or isn't in, the th class attribute:

$(function(){
    //Add disabling parser to each header with a disable class
    $("th[class*='csuci.sortable.false']").each(function(){
        $(this).removeClass("csuci.sortable.false").addClass("{sorter: false}");
    });

    //th without a class automatically get sorting disabled
    $("th[class='']").each(function(){
        $(this).addClass("{sorter: false}");
    });

    //Add parser to each table that has a class="csuci.sortable."
    $("th[class*='csuci.sortable.date']").each(function(){
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'usLongDate'}");
    });

    $("th[class*='csuci.sortable.percent']").each(function(){
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'percent'}");
    });

    $("th[class*='csuci.sortable.ip']").each(function(){
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'ip-address'}");
    });

    $("th[class*='csuci.sortable.url']").each(function(){
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'url'}");
    });

    $("th[class*='csuci.sortable.money']").each(function(){
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'currency'}");
    });

    $("th[class*='csuci.sortable.time']").each(function(){
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'time'}");
    });
});
+1  A: 

So what are the cases you want to enable the sorter with? It sounds like the only case the sorter is enabled by is a direct true flag. You may just need to use not.

$("th:not([class*='csuci.sortable.true'])").each(function(){
    $(this).addClass("{sorter: false}");
});

If sorting is only enabled by the true flag and you need to set the sorter for all elements then you could match all columns and use a conditional to set the flag.

$("th").each(function(){
    var sortEnabled = $this.hasClass('csuci.sortable.true');
    $(this).addClass("{sorter: "+sortEnabled+"}");
});
gradbot
Hey gradbot, really there are no special boolean classes I'm adding, just arbitrary strings that I hard code each traversal function to look for. It then adds a sort parser based on what string is in the class.
kingrichard2005
Have you considered using jQuery data? http://docs.jquery.com/Core/data#namevalue
gradbot
A: 

I managed to find a way around this, I simply add a traversal function at the beginning that adds the disable parser to every table header, then each succeeding traversal removes the disable parser, checks for a hard-coded string and adds the necessary parser:

$(function(){

    //Add disable parser to all headers
    $("th").each(function(){
        $(this).addClass("{sorter: false}");
    });

    //Add disable parser to each header with a disable class
    $("th[class*='csuci.sortable.false']").each(function(){
        $(this).removeClass("csuci.sortable.false").addClass("{sorter: false}");
    });

    //th without a class automatically gets sorting disabled
    $("th[class='']").each(function(){
        $(this).addClass("{sorter: false}");
    });

    //Add parser to each table that has a class="csuci.sortable.date"
    $("th[class*='csuci.sortable.date']").each(function(){
     $(this).removeClass("{sorter: false}");
        $(this).removeClass("csuci.sortable.date").addClass("{sorter: 'usLongDate'}");
    });
    //Add parser to each table that has a class="csuci.sortable.percent"
    $("th[class*='csuci.sortable.percent']").each(function(){
     $(this).removeClass("{sorter: false}");
        $(this).removeClass("csuci.sortable.percent").addClass("{sorter: 'percent'}");
    });
    //Add parser to each table that has a class="csuci.sortable.ip"
    $("th[class*='csuci.sortable.ip']").each(function(){
     $(this).removeClass("{sorter: false}");
        $(this).removeClass("csuci.sortable.ip").addClass("{sorter: 'ip-address'}");
    });
    //Add parser to each table that has a class="csuci.sortable.url"
    $("th[class*='csuci.sortable.url']").each(function(){
     $(this).removeClass("{sorter: false}");
        $(this).removeClass("csuci.sortable.url").addClass("{sorter: 'url'}");
    });
    //Add parser to each table that has a class="csuci.sortable.money"
    $("th[class*='csuci.sortable.money']").each(function(){
     $(this).removeClass("{sorter: false}");
        $(this).removeClass("csuci.sortable.money").addClass("{sorter: 'currency'}");
    });
    //Add parser to each table that has a class="csuci.sortable.time"
    $("th[class*='csuci.sortable.time']").each(function(){
     $(this).removeClass("{sorter: false}");
        $(this).removeClass("csuci.sortable.time").addClass("{sorter: 'time'}");
    });
});

Probably not the cleanest way to do, but it works. Thanks to all here at StackOverflow who've provided me invaluable insight into the workings of jQuery, it truly is a magnificent library.

kingrichard2005