【问题标题】:Jquery tablesortjQuery 表格排序
【发布时间】:2015-04-29 17:11:01
【问题描述】:

我正在为 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 },
            }
        }
    );

但是当我点击状态标题时,什么也没有发生

当我进行调试选项时:true,这是 o/p:

“在 7,0 和 dir 0 时间(1ms)上排序”jquery.tablesorter.min.js:157:5

"重建表 (4ms)" jquery.tablesorter.min.js:157:5 "完成

应用 0 个小部件 (0ms)"

任何帮助表示赞赏。

【问题讨论】:

  • 请格式化您的代码。

标签: jquery tablesorter


【解决方案1】:

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

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 );

【讨论】:

    猜你喜欢
    • 2011-03-10
    • 2018-05-10
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多