开发者

Finding the column and applying custom parser

开发者 https://www.devze.com 2023-02-15 19:20 出处:网络
I am using tablesorter plugin to do some custom sorting which works great. I set this parser to be on a specific column, but the application allows me to switch columns on and off though some settings

I am using tablesorter plugin to do some custom sorting which works great. I set this parser to be on a specific column, but the application allows me to switch columns on and off though some settings, so the index of the column of this custom sorting can be different d开发者_JAVA百科epending on the settings.

Is it possible to make it automatically finds the right column and use this parser method I got? Instead of manually placing it on the header index.

EDIT, right now I am using it like this (sometimes the "quarters" sorter can be at another index, so i need the code to auto detect it)

$("table").tablesorter({
                headers: {
                    0: { sorter: false },
                    1: { sorter: false },
                    5: { sorter: "quarters" }
                }
            });

And my custom parser:

$.tablesorter.addParser({
        id: 'quarters',
        is: function (s) {
            return false;
        },
        format: function (s) {
            var match = s.match(/(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20\d\d)/);
            if (match)
            {
               return match[3] * 1000 - match[2] * 100 - match[1] * 10;                
            }
            else return 0;
        },
        type: 'text'
    });


If there is some selector on the th that you want to apply the quarters sorter to then you could use jQuery to select that header, and then use prevUtil to get all of the th siblings before it and use that size to determine the index of the column you want.

My example assumes your column in question has an id of quarters:

var headerPosition = $("#quarters").prevUntil().size());
var headers = {};

headers[headerPosition] = "quarters";

$("table").tablesorter({
    headers: headers
});

Here is a jsfiddle with it:

http://jsfiddle.net/magicaj/7A3ZF/1/


You can use this small function to search the index of the header where you want to use the sort parser:

function get_index_by_text($list, text){
    var searched = -1;
    $list.each(function(index){
        if ($(this).text() == text) {
            searched = index;
            return(false);
        }
    });
    return(searched);
}

var quarters_index = get_index_by_text($("table thead th"), "quarters");

Just take care and check if the text is finded, if it's not the function will return -1.

0

精彩评论

暂无评论...
验证码 换一张
取 消