Jquery 表格排序

Jquery tablesort

我正在为 jquery 使用 tablesorter 并以此页面为例

http://mottie.github.io/tablesorter/docs/example-parsers.html

并使用此插件:

http://mottie.github.io/tablesorter/js/jquery.tablesorter.js

这是对“状态”进行排序的代码

$.tablesorter.addParser({
      id: 'status',
      is: function(s) {
        // return false so this parser is not auto detected
        return false;
      },
      format: function(s, table, cell, cellIndex) {
        // format your data for normalization
        //console.log(cellIndex);
        return s.toLowerCase()
          .replace(/Deleted/,4)
          .replace(/Finished/,3)
          .replace(/Cancelled/,2)
          .replace(/In Progress/,1)
          .replace(/New/,0);
      },
      // set type, either numeric or text
      type: 'numeric'
    });

$('#request').tablesorter(
        {
            debug:false, 
            widthFixed: false,
            headers: {
                0: { sorter: false },
                3: { sorter: false },
                4: { sorter: 'dates-desired' },
                6: { sorter: 'dates-projected' },
                7: { sorter: 'status' },
                8: { sorter: false },
            }
        }
    );

但是当我点击状态 thead 时,没有任何反应

当我设置调试选项时:是的,这是 o/p:

"Sorting on 7,0 and dir 0 time (1ms)" jquery.tablesorter.min.js:157:5

"Rebuilt table (4ms)" jquery.tablesorter.min.js:157:5 "Completed

applying 0 widgets (0ms)"

感谢任何帮助。

解析器代码中的问题...文本设置为小写,但正则表达式包含大写字符。试试这个更新的解析器:

var arry = [ 'new', 'in progress', 'cancelled', 'finished', 'deleted' ];
$.tablesorter.addParser({
    id: 'status',
    is: function() {
        // return false so this parser is not auto detected
        return false;
    },
    format: function( s, table, cell, cellIndex ) {
        var str = ( s || '' ).toLowerCase(),
            index = arry.indexOf( str );
        // return original text if index not found; this allows proper
        // sorting of empty cells
        return index < 0 ? s : index;
    },
    // set type, either numeric or text
    type: 'numeric'
});

如果您需要支持旧版本的 IE,请从使用 indexOf 更改为 jQuery's $.inArray() function

index = $.inArray( str, arry );