如何在jQuery Tablesorter中创建特定的排序功能? [英] How to create a specific sorting function in jQuery Tablesorter?

查看:96
本文介绍了如何在jQuery Tablesorter中创建特定的排序功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Tablesorter的这一出色版本: http://mottie.github. com/tablesorter/docs/index.html

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

一切正常,但是现在我遇到了这个问题:在我的桌子上,我有一列包含篮球运动员的位置.因此,我希望对该列进行如下逻辑排序:PG-SG-SF-PF-C.

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.

我试图创建此自定义排序功能-查看我的脚本,第2列:

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

});

但是,该列仍然像普通文本字符串(C-PF-PG-SF-SG)一样按字母顺序排序.

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

我在哪里弄错了?我的Java语言不是特别强,所以它可能在排序功能中.谢谢.

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

推荐答案

我通过添加自己的解析器(如此问题所示)来解决了这个问题:

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

});

这篇关于如何在jQuery Tablesorter中创建特定的排序功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆