javascript - 带连字符的 Jquery 表排序器数字

标签 javascript jquery gridview jquery-plugins tablesorter

Jquery 表排序器

我正在使用 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>

最佳答案

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

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

关于javascript - 带连字符的 Jquery 表排序器数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079329/

相关文章:

javascript - Node js require()[env] 未定义

android - 在 GridView 中显示空白项

php - AJAX 调用 PHP 文件的问题,mysql 不执行 - 有错误

jquery - 动态加载图像作为 Canvas 背景。我该怎么做?

Jquery数据表删除水平线

javascript - Javascript 中的parentNode 似乎不适合我

c# - GridView 数据绑定(bind)字段 - SQL 参数

javascript - 无法从 firebase 获取数据到我的 html 页面

javascript - 跨浏览器,跨框架下拉菜单的干净解决方案?

javascript - 从 JQuery 创建 iframe - 属性不起作用,为什么不呢?