jQuery tablesorter plugin secondary “hidden” sorting

后端 未结 6 1810
粉色の甜心
粉色の甜心 2021-02-05 06:24

I\'m using the jQuery tablesorter plugin and I have a column that contains name of month and year like this

April, 1975
January, 2001

I would l

6条回答
  •  梦毁少年i
    2021-02-05 06:55

    It's a bit of a hack (OK, it's a total hack), but if you set the parser for the column to 'text', and pre-fix your pretty output with the string you really want to sort on within a hidden span it will sort correctly.

    You can set the parser on a column with the headers option, e.g. to set the parser on the first and second columns to 'text' you would set the following:

    headers: {0: {sorter: 'text'}, : {sorter: 'text'}
    

    To do this trick with dates, you can use the ISO8601 date format which sorts lexically. JS's Date objects can generate ISO8601 date strings via the toISOString() function.

    Given the CSS:

    span.hidden{
        display:none;
    }
    

    A sample cell in the table would look like this:

    19 April 2015
    

    Not the prettiest code in the world, but it does work.

提交回复
热议问题