【问题标题】:jQuery Tablesorter: Sort custom parser via link outside the tablejQuery Tablesorter:通过表外的链接对自定义解析器进行排序
【发布时间】:2015-03-31 19:55:59
【问题描述】:

对于此处的任何重复,我深表歉意,我已经尝试对此主题进行搜索,但没有成功。我有一个使用 jQuery TableSorter 排序的表,并且正在使用自定义解析器对 9 列中的 5 列进行排序。当通过表头文本触发时,自定义解析器运行良好,但我想通过表外的链接对表列进行排序。

对于那些不使用自定义解析器的列,我已经能够使用以下实现通过表外的链接对它们进行排序(请参阅http://tablesorter.com/docs/example-trigger-sort.html):

$(document).ready(function() { 
$("table").tablesorter(); 
$("#trigger-link").click(function() { 
    // set sorting column and direction, this will sort on the first and third column the column index starts at zero 
    var sorting = [[0,0],[2,0]]; 
    // sort on the first column 
    $("table").trigger("sorton",[sorting]); 
    // return false to stop default link action 
    return false; 
}); 
});

现在我需要某种方式对我的列进行排序,这些列通过表外的链接使用自定义解析器。例如。我需要某种方式使用上面的代码来触发下面的代码(见http://tablesorter.com/docs/example-parsers.html):

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'grades', 
    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(/good/,2).replace(/medium/,1).replace(/bad/,0); 
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: { 
                sorter:'grades' 
            } 
        } 
    }); 
});        

任何想法或建议将不胜感激。

干杯,

杰克

【问题讨论】:

    标签: jquery parsing sorting tablesorter


    【解决方案1】:

    tablesorter 的工作方式是在初始化时,它会遍历整个表格并保存每个单元格中的数据。该数据首先由解析器处理并存储在内部数组中。

    当您对列进行排序时,原始表排序器实际上是对内部数组进行排序,然后使用交叉引用将表行按排序顺序重新添加到表中。这甚至适用于外部链接。 Tablesorter 对内部数组进行排序并将行添加回表中。

    刷新内部缓存中数据的唯一方法是触发“更新”,这将使用解析器重新处理所有表格单元格,以重建内部数组。

    所以...如果您想对使用自定义解析器的列进行排序,请与没有自定义解析器的列完全相同。例如,如果自定义解析器在第 5 列,则使外部链接代码如下所示:

    $("#trigger-link").click(function() {  
        // apply an ascending sort on the 5th column (zero-based index)
        $("table").trigger("sorton",[ [[4,0]] ]); 
        return false; 
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      相关资源
      最近更新 更多