简体   繁体   English

无法指定数据表的默认列排序javascript

[英]Cannot specify a default column sort of datatables javascript

I need to sort by a specific column on page load, in this case, have it show initial results with a descending sort on "RecordDate". 我需要按页面加载时的特定列进行排序,在这种情况下,要使它以“ RecordDate”的降序显示初始结果。

The problem is, I believe the server side is blocking any sort specification, and any modification I made below will not load results (asSorting [[number of column, and tried name, desc]] etc). 问题是,我相信服务器端会阻止任何排序规范,并且我在下面进行的任何修改都不会加载结果(asSorting [[列数,并尝试过的名称,desc]]等)。

Is there a special function I can write to force this column sort? 我可以编写一个特殊的函数来强制此列排序吗? Curious if this can be handled here before modifying a stored procedure or another cs file. 好奇是否可以在修改存储过程或另一个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
    }
} );
}

Assuming you have access to the dataTable's data array and it is of the form: 假设您有权访问dataTable的数据数组,并且其格式为:

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

You can do a simple descending sort based on the value with the following: 您可以使用以下值基于值进行简单的降序排序:

array.sort(SimpleCompareDescOnValue);

Given the function: 给定功能:

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;
    }

Not sure if this helps you at all. 不知道这是否对您有帮助。 I've never used the dataTable library (although it looks cool) but I have to assume you can access the data after pulling it from the server and it only makes sense that it would be stored in arrays at some level. 我从未使用过dataTable库(尽管它看上去很酷),但我必须假定您可以在从服务器中提取数据后访问数据,并且仅在某种程度上将其存储在数组中才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM