mottie tablesorter:强制sortText(不是sortNatural)

时间:2015-11-13 19:55:34

标签: jquery tablesorter

我已经制作了一个自定义解析器:

    $.tablesorter.addParser({
        id: 'custom-sort-value',
        is: function(s) {
            // return false so this parser is not auto detected
            return false;
        },
        format: function(s, table, cell, cellIndex) {
            return $(cell).data('sort-value').toString();
        },
        type: 'text'
    });

但是,我注意到它最终使用了tablesorter函数sortNatural(因为type: 'text')。但是,我需要它只是做一个基本的字母排序,比如tablesorter的sortText(我不能让它分割字符串并比较每个分割)。有没有办法可以强迫它这样做?

1 个答案:

答案 0 :(得分:1)

因此,您不需要自定义解析器来从属性中获取文本。 tablesorter的fork尝试从data-text获取自定义排序字符串(可以由textAttribute option修改),但仅当textExtraction option设置为“basic”时(默认设置)

因此,要使用基本sortText排序,请使用textSorter option按列设置自定义排序功能:

$(function(){
  $("table").tablesorter({
    textSorter : {
      // replace INDIVIDUAL COLUMN text sorter functions
      0 : function(a, b, direction, columnIndex, table){
        // same as $.tablesorter.sortText (basic alphabetical sort)
        // direction: true = ascending; false = descending
        // columnIndex: zero-based index of the current table column being sorted
        // table: table DOM element (access options by using table.config)
        return a > b ? 1 : (a < b ? -1 : 0);
      },
      1 : $.tablesorter.sortText,    // same as the function in column 0 above (modified in v2.12)
      2 : $.tablesorter.sortNatural, // renamed v2.12 from $.tablesorter.sortText - performs natural sort
      3 : Array.AlphanumericSort     // alphanumeric sort from sugar (http://sugarjs.com/arrays#sorting)
    }
  });
});