【问题标题】:Jquery tablesorter numbers with hyphens带有连字符的 Jquery tablesorter 数字
【发布时间】:2016-09-01 22:29:46
【问题描述】:

jQuery 表格排序器

我正在使用 Jquery 表排序器,我需要对包含带有连字符标记的数字的 Gridview 列进行排序。以下格式的数据,无法按 ASC 顺序 onLoad 排序:17-143,17-162,12-144,17-45, 18-12,17-65,18-2 .一些值是 NULL。来自 sql 的空白值。 以上数据默认排序为:12-144,17-45,17-65,17-143,17-162,18-2,18-12

请帮忙。这是我尝试使用的 sn-p:

<script src="https://github.com/christianbach/tablesorter/blob/master/jquery.tablesorter.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $("#main_gvResource").tablesorter.addParser({ 
        // set a unique id 
        id: 'lblIDSort',
        is: function(s) {
            return false;
        },
        format: function(s) {
            return s.replace('$','').replace(/-/g,'');
        }, 
        type: 'numeric' 
    }); 
    $(function() {
        $("#main_gvResource").tablesorter({
           widgets: ['zebra'],
            headers: {
                1: {//zero-based column index
                    sorter:'gdMobileID'
                }
            }
        });
    });
});
</script>

【问题讨论】:

  • 您知道您的 document.ready 中有一个 document.ready 吗?

标签: javascript jquery gridview jquery-plugins tablesorter


【解决方案1】:

为什么会有这些空白?您是否害怕,您可能一次看到太多代码? ;)

$(function() {
    $.tablesorter.addParser({ 
        _regex: /^\d+(?:-\d+)+$/,
        _formatNumber: function(nr){
            //return "0".repeat(5-nr.length) + nr;
            while(nr.length < 5) nr = "0" + nr;
            return nr;
        },

        // set a unique id 
        id: 'gdMobileID',
        is: function(s) {
            //to auto-recognize the column-type
            return this._regex.test(s); 

            //to apply only on columns you defined
            return false;
        }, 

        format: function(s){
            return s.replace(/\d+/g, this._formatNumber);
        }, 
        type: 'text',
    }); 

    $("#main-gvResource").tablesorter({
        widgets: ['zebra'],
        //no need, if you let the parser auto-recognize the columns to apply
        /*headers: {
            1: {//zero-based column index
                sorter:'gdMobileID'
            }
        }*/
    });
});

【讨论】:

  • 汤姆,谢谢你的 sn-p。我是堆栈溢出的新手,最重要的是,jQuery 也是新手 :) 我收到错误“对象不支持属性或方法‘重复’”
  • 错误:对象不支持属性或方法“重复”
  • 好的,那是 ES6。修好了。
【解决方案2】:

我的fork of tablesorter 默认使用自然排序算法。您无需添加任何额外代码即可使其与该数据集一起使用 (demo)

$(function () {
    $('table').tablesorter({
        theme: 'blue'
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多