1

我在包含字符串的列上使用 TableSorter:

$.tablesorter.addParser(
    {
        id: 'positions',
        is: function(s) {
                return false;
            },
        format: function(s) {
                var abbr=s.replace(/[^a-zA-Z0-9]/g, '').slice(_sortpos);
                return abbr;
            },
        type: 'numeric'
    }
);

格式函数应返回一个取决于全局 _sortpos 变量的值。该值动态变化。该行:

var abbr=s.replace(/[^a-zA-Z0-9]/g, '').slice(_sortpos);

剥离所有非数字和非字母,然后删除一定数量的初始字符。目的是按 _sortpos-th 字符排序。

似乎发生的是,当解析器添加到表排序器时,格式函数只为每个字符串解析一次。但是,出于我的目的,每次排序时都必须重新运行格式功能。

到目前为止,我每次排序时都尝试添加解析器。但这没有用。我还尝试通过重新运行我的初始化来重新初始化表格排序器

$(".tablesorter").tablesorter(
    {
        sortReset: true,
        headers: {
            1: {
                sorter: 'positions'
            }
        }
    }   
); 

但无济于事。

我能做什么?另外:是否有一个比较器函数可以用来潜入全局参数?然后我可以在解析器中格式化字符串,然后在比较器中动态使用 slice() 函数中的 _sortpos。

4

1 回答 1

0

我自己找到了答案:

$(document).ready(function() {  
    $(".tablesorter").tablesorter(
        {
            textSorter: function(a, b, table, column){                  
                if (column==1)
                {
                    var c=parseInt(a.replace(/[^a-zA-Z0-9]/g, '').slice(_sortpos));
                    var d=parseInt(b.replace(/[^a-zA-Z0-9]/g, '').slice(_sortpos));                 
                    return ((c < d) ? -1 : ((c > d) ? 1 : 0));
                }
                return a.localeCompare(b);              
            }           
        }
    );  
});

它不使用解析器,仅使用自定义排序器对 column==1 进行排序。

于 2013-07-03T21:44:15.530 回答