Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 查找列并应用自定义解析器_Jquery_Sorting_Tablesorter - Fatal编程技术网

Jquery 查找列并应用自定义解析器

Jquery 查找列并应用自定义解析器,jquery,sorting,tablesorter,Jquery,Sorting,Tablesorter,我正在使用tablesorter插件进行一些自定义排序,效果非常好。我将此解析器设置为位于特定列上,但应用程序允许我通过某些设置打开和关闭列,因此此自定义排序的列的索引可以根据设置而有所不同 是否可以让它自动找到正确的列并使用我得到的解析器方法?而不是手动将其放置在标题索引上 编辑,现在我这样使用它(有时“Quarter”分拣机可能位于另一个索引,所以我需要代码来自动检测它) 和我的自定义解析器: $.tablesorter.addParser({ id: 'quarters',

我正在使用tablesorter插件进行一些自定义排序,效果非常好。我将此解析器设置为位于特定列上,但应用程序允许我通过某些设置打开和关闭列,因此此自定义排序的列的索引可以根据设置而有所不同

是否可以让它自动找到正确的列并使用我得到的解析器方法?而不是手动将其放置在标题索引上

编辑,现在我这样使用它(有时“Quarter”分拣机可能位于另一个索引,所以我需要代码来自动检测它)

和我的自定义解析器:

$.tablesorter.addParser({
        id: 'quarters',
        is: function (s) {
            return false;
        },
        format: function (s) {
            var match = s.match(/(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20\d\d)/);
            if (match)
            {
               return match[3] * 1000 - match[2] * 100 - match[1] * 10;                
            }
            else return 0;
        },
        type: 'text'
    });

如果
th
上有某个选择器要应用
quarters
分类器,则可以使用jQuery选择该标题,然后使用
prevUtil
获取其前面的所有
th
同级,并使用该大小确定所需列的索引

我的示例假设您所讨论的列的
id
quarters

var headerPosition = $("#quarters").prevUntil().size());
var headers = {};

headers[headerPosition] = "quarters";

$("table").tablesorter({
    headers: headers
});
下面是一个JSFIDLE:


您可以使用此小功能搜索要使用排序分析器的标题索引:

function get_index_by_text($list, text){ var searched = -1; $list.each(function(index){ if ($(this).text() == text) { searched = index; return(false); } }); return(searched); } var quarters_index = get_index_by_text($("table thead th"), "quarters"); 函数get_index_by_text($list,text){ var=-1; $list.each(函数(索引){ if($(this).text()==文本){ 搜索=索引; 返回(假); } }); 返回(已搜索); } var季度指数=按文本获取季度指数($(“表THAD th”),“季度”);
请注意并检查文本是否被找到,如果不是,函数将返回-1。

您能显示您当前使用的js代码吗?您介意我用您的问题将500个悬赏点数转移到Terseus(下面的第二个响应者)吗?对于这个答案,我有以下几点:。我的问题因为离题而关闭了,所以特修斯从来没有得到过那些赏金积分。如果你不反对,我将为你的问题悬赏500分,并奖励给特修斯的答案。 function get_index_by_text($list, text){ var searched = -1; $list.each(function(index){ if ($(this).text() == text) { searched = index; return(false); } }); return(searched); } var quarters_index = get_index_by_text($("table thead th"), "quarters");