带有连字符的Jquery tablesorter数字

时间:2016-05-06 18:41:24

标签: javascript jquery gridview jquery-plugins tablesorter

Jquery tablesorter

我正在使用Jquery tablesorter,我需要一个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

请协助。以下是我尝试使用的代码段:



<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

为什么所有这些空白?你害怕,你可能会立刻看到太多的代码? ;)

$(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'
            }
        }*/
    });
});

答案 1 :(得分:0)

我的fork of tablesorter默认使用自然排序算法。您不需要包含任何额外的代码来使其适用于该数据集(demo

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