jquery datatables sorting neglect null value

前端 未结 2 1239
感动是毒
感动是毒 2021-01-06 20:31

I\'m using datatables and jQuery for making nice sortable tables. I now want to sort the rows an a value. This value is a numeric value. But it can also be not available, so

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 20:50

    See this: http://jsfiddle.net/CYubV/

    The first column in the table works like a normal column; the second column works like you ask.

    Try custom sorting, something like this:

    $.fn.dataTableExt.oSort['nullable-asc'] = function(a,b) {
        if (a == '-')
            return 1;
        else if (b == '-')
            return -1;
        else
        {
            var ia = parseInt(a);
            var ib = parseInt(b);
            return (ia ib) ? 1 : 0);
        }
    }
    
    $.fn.dataTableExt.oSort['nullable-desc'] = function(a,b) {
        if (a == '-')
            return 1;
        else if (b == '-')
            return -1;
        else
        {
            var ia = parseInt(a);
            var ib = parseInt(b);
            return (ia>ib) ? -1 : ((ia < ib) ? 1 : 0);
        }
    }
    
    $('#table').dataTable( {
        "bPaginate": false,
        "bFilter": false,
        "aoColumns": [
                null,
                {"bSortable": true, "sType": "nullable"}
                    ],
    } );
    

提交回复
热议问题