无法指定默认列类型的数据表javascript

时间:2014-04-01 17:16:47

标签: javascript ajax sorting datatables

我需要按页面加载的特定列进行排序,在这种情况下,让它在“RecordDate”上以降序排序显示初始结果。

问题是,我认为服务器端阻止任何排序规范,我在下面做的任何修改都不会加载结果(asSorting [[列数,尝试过的名称,desc]等)。

我是否可以编写一个特殊功能来强制此列排序?好奇,如果在修改存储过程或其他cs文件之前可以在此处理。

function GetRecords( DTO ) {
var grpid = ApplyGroupingOnTable();
console.log( grpid );
oTable = $( "#SearchTable" ).dataTable( {
    "oLanguage": {
        "sZeroRecords": "No records to display"//,
        //"sSearch": "Search on UserName"
    },
    "aLengthMenu": [[10, 25, 50, 100, 150, 250, 500], [10, 25, 50, 100, 150, 250, 500]],
    "iDisplayLength": 10,
    "sScrollX": "1300px",
    "bSortClasses": false,
    "bStateSave": false,
    "bFilter": false,
    "bLengthChange": true,
    "bPaginate": true,
    "bAutoWidth": false,
    "bProcessing": false,
    "bServerSide": true,
    "bDestroy": true,
    "sAjaxSource": "/Data/SearchRecords",
    "aoColumns": [
        { "mData": "SelectID", "sClass": "alternatingCenterAlign", fnRender: CreateSelectCaseViewerButton, "bSortable": false, "bSearchable": false, "sWidth": "18px" },
        { "mData": "ViewID", "sClass": "alternatingCenterAlign", fnRender: CreateCaseViewerButton, "bSortable": false, "bSearchable": false, "sWidth": "18px" },
        { "mData": "TagID", "sClass": "alternatingCenterAlign", fnRender: CreateTagCaseButton, "bSortable": false, "bSearchable": false, "sWidth": "18px" },
        { "mData": "ID", "sClass": "alternating", "sType": "string", "bSortable": true, "bSearchable": false, "sWidth": "50px" },
        { "mData": "ClientName", "sClass": "alternating", "sType": "string", "bSortable": true, "sWidth": "120px" },
        { "mData": "RecordDate", "sClass": "alternating", "sType": "string", "bSortable": true, "bSearchable": false, "sWidth": "70px" },

    "bJQueryUI": false,
    "sPaginationType": "full_numbers",
    "bDeferRender": true,
    "bRetrieve": false,
    "fnServerParams": function ( aoData ) {
        aoData.push( { "name": "iParticipant", "value": $( "#participant" ).val() } );
        aoData.push( { "name": "iSearch", "value": JSON.stringify( DTO ) } );
        aoData.push( { "name": "iId", "value": cliid } );
        aoData.push( { "name": "iOrder", "value": grpid } );
    },
    "fnDrawCallback": function ( oSettings ) {
        if ( grpid ) FinalGrouping( oSettings, grpid );
        $( ".overflow" ).each( function () { $( this ).attr( 'title', $( this ).text() ); } );
        $( '#tablediv select' ).chosen();
    },
    "fnServerData": function ( sSource, aoData, fnCallback ) {
        $( '.grid-loading' ).show();
        $.ajax( {
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            type: "GET",
            url: sSource,
            data: aoData,
            cache: false,
            success: function ( msg ) {
                if ( msg.Exception != undefined ) {
                    alert( msg.Exception );
                    $( '.grid-loading' ).hide();
                    return false;
                }
                lastId = ( msg.aaData.length > 0 ) ? msg.aaData[msg.aaData.length - 1][0] : 0;
                fnCallback( msg );
                $( '.grid-loading' ).hide();
            }
        } ); //End Ajax Call
    }
} );
}

1 个答案:

答案 0 :(得分:1)

假设您可以访问dataTable的数据数组,其格式如下:

var array = [ 
               { name: "somename", value: "somevalue" },
               { name: "somename2", value: "somevalue2" }
           ];

您可以根据以下值进行简单的降序排序:

array.sort(SimpleCompareDescOnValue);

鉴于功能:

function SimpleCompareDescOnValue(a, b) {
        // Use caution so as not to define 'undefined'.  It's not a great practice but it works.
        var c = a.length != undefined ? Math.max.apply(Math, a.map(function (o) { return o.value; })) : a.value;
        var d = b.length != undefined ? Math.max.apply(Math, b.map(function (o) { return o.value; })) : b.value;

        if (c > d)
            return -1;
        if (c < d)
            return 1;
        return 0;
    }

不确定这对你有帮助吗?我从来没有使用过dataTable库(尽管它看起来很酷)但是我不得不假设你可以在从服务器中提取数据之后访问数据,只有在某种程度上将它存储在数组中才有意义。