How to create a specific sorting function in jQuery Tablesorter?

孤街浪徒 提交于 2019-12-19 04:03:38

问题


I am using this great version of jQuery Tablesorter: http://mottie.github.com/tablesorter/docs/index.html

Everything is working well but now I have this problem: in my table, I have one column which contains positions of basketball players. Therefore I want that column to be sorted logically like this: PG-SG-SF-PF-C.

I tried to create this custom sorting function - look at my script, column 2:

$(document).ready(function() { 

    $(".stats").tablesorter({
            sortInitialOrder: 'desc',
            sortRestart: true,
            // Enable use of the characterEquivalents reference 
            sortLocaleCompare: false, 
            // if false, upper case sorts BEFORE lower case 
            ignoreCase: true,
            headers: { 
                0: { 
                        sortInitialOrder: 'asc'
                }, 
                1: { 
                        sortInitialOrder: 'asc'
                },
                2: { 
                        textSorter: function(a, b){ 
                            var positions = {
                                "PG": 0,
                                "SG": 10,
                                "SF": 20,
                                "PF": 30,
                                "C": 40
                            };
                            return ((positions[a] < positions[b]) ? -1 : ((positions[a] > positions[b]) ? 1 : 0)); 
                        },
                        sortInitialOrder: 'asc'
                }
            }
        }
    ); 

});

However, the column is still being sorted alphabetically like a normal text string (C-PF-PG-SF-SG).

Where am I making a mistake? I am not particularly strong in Javascript so it is probably somewhere in the sorting function. Thank you.


回答1:


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'
                }
            }
        }
    );

});


来源:https://stackoverflow.com/questions/13238795/how-to-create-a-specific-sorting-function-in-jquery-tablesorter

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