Datatable sort numbers dont work

流过昼夜 提交于 2019-12-11 14:58:43

问题


I have a datatable on my website and try to sort numbers with 1,999,999,999 but it doesn't work. I tried to fix the problem with a lot of tips on Google but it does not helped me.

Thats the javascript code for my table

$('.d3uitems').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "sDom": '<""l>t<"F"p>',
        'aaSorting': [[ 0, 'desc' ]]
    }).columnFilter({
            aoColumns: [ null,
                     { type: "text"},
                         null,
                     null,
                     { type: "text"},
                     null
                ]
    });

Here is the Datatable where i try to sort the numbers.

http://www.lootfinder.net/index.php?page=d3uitems


回答1:


You can try it, we overwrite DataTable sort function, and replace ",".

    <script type="text/javascript" charset="utf-8">

        jQuery.fn.dataTableExt.oSort['numeric-comma-asc']  = function(a,b) {
            var x = (a == "-") ? 0 : a.replace( /,/g,"" );
            var y = (b == "-") ? 0 : b.replace( /,/g,"" );
            alert( "x=" + x );
            x = parseFloat( x );
            y = parseFloat( y );
            return ((x < y) ? -1 : ((x > y) ?  1 : 0));
        };

        jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
            var x = (a == "-") ? 0 : a.replace( ",","" );
            var y = (b == "-") ? 0 : b.replace( ",","" );
            x = parseFloat( x );
            y = parseFloat( y );
            return ((x < y) ?  1 : ((x > y) ? -1 : 0));
        };


        $(document).ready(function() {
            $('#example').dataTable( {
                "sPaginationType": "full_numbers",
                "bPaginate": false,
                "aoColumns": [
                                null,
                                null,
                                null,
                                { "sType": "numeric-comma" },
                                null
                ]
            } );

        } );
    </script>


来源:https://stackoverflow.com/questions/17836229/datatable-sort-numbers-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!