JQuery tablesorter custom parser to all headers?

北城余情 提交于 2019-12-11 01:45:00

问题


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.


回答1:


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();
    



回答2:


return true from 'is' in addParser

eg: this parser assigns 'N/A' a value of -1 $.tablesorter.addParser({ id: 'num-with-na', is: function(s) { //always use this return true; }, format: function(n){ return n === 'N/A' ? -1 : n; }, type: 'numeric' });



来源:https://stackoverflow.com/questions/9014962/jquery-tablesorter-custom-parser-to-all-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!