How to create a specific sorting function in jQuery Tablesorter?

梦想的初衷 提交于 2019-11-30 22:42:04
user1763581

I figured it out by adding my own parser like it is shown in this question: Sorting Image and hyperlink columns in a table using JQuery Sorter plugin

I will copy my script that works as I want it to work, hope it helps someone:

$(document).ready(function() { 

    $.tablesorter.addParser({
            // set a unique id 
            id: 'positions',
            is: function(s) {
                    // return false so this parser is not auto detected 
                    return false;
            },
            format: function(s) {
                    // format your data for normalization 
                    return s.toLowerCase()
                            .replace("pg", "d")
                            .replace("sg", "h")
                            .replace("sf", "m")
                            .replace("pf", "r")
                            .replace("c", "v");
            },
            // set type, either numeric or text 
            type: 'text'
    });     

    $(".stats").tablesorter({
            sortInitialOrder: 'desc',
            sortRestart: true,
            headers: { 
                0: { 
                        sortInitialOrder: 'asc'
                }, 
                1: { 
                        sortInitialOrder: 'asc'
                },
                2: { 
                        sorter: 'positions',
                        sortInitialOrder: 'asc'
                }
            }
        }
    );

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