Search code examples
jquerytablesorter

JQuery tablesorter custom parser to all headers?


I'm currently working with a tablesorter table in which I need to use a custom parser for each header. I'd like to know if there's an easy way to do this, such as:

table.tablesorter({ 
        headers: { 
            0-20: { 
                sorter:'CareerLast' 
            }, 

        } 
    }); 

I know that the above code doesn't work, but I'm just wondering if there's a more readable way of applying the custom parser, other than manually placing it on each column by index.


Solution

  • Well, I think you have three choices:

    1. Define each header, 0 through 20 in the initialization options.

      header : {
          0 : { sorter : 'CareerLast' },
          1 : { sorter : 'CareerLast' },
          2 : { sorter : 'CareerLast' },
          // etc
          20 : { sorter : 'CareerLast' }
      }
      
    2. Use the meta data plugin and add the sorter definition in the header class:

      // untested, but I think this will work
      $('table').find('thead th').addClass("{sorter:'CareerLast'}");
      $('table').tablesorter();
      
    3. Try out my forked version of tablesorter and just add the sorter as a class name

      $('table').find('thead th').addClass('sorter-CareerLast');
      $('table').tablesorter();