reset to default sorting in jquery datatable

故事扮演 提交于 2019-12-13 04:29:18

问题


I have a page on which one dropdown as Report1 and Report2 is there. If I select Report1 it will populate startdate and enddate. I will select startdate and end date then cliick on view report it will do ajax call and fetch the data and displaying in table, I am using datatable for this.

I have default sorting of the second coulmn and also I can able to sort other column. If I sort other column and then select the other report Report2 and then select startdate adn end date and then hit the view report button. Again If I select the Report1 and startdate and end date and hit the view report button, it is sorting the other column but not the default sorting column. Any help would be appreciable.

Below is my code

$('#userTable').dataTable({
                        "bProcessing" : false,
                        "bServerSide" : false,
                        "bFilter" : false,
                        "bSearchable" : false,
                        "oLanguage" : {
                            "sZeroRecords" : "No Active Users Found",
                            "sInfo" : "Total Records Found: _TOTAL_ "
                        },
                        "aaSorting" : [ [ 2, "asc" ] ],
                        "aoColumnDefs" : [ {
                            "mData" : "emailAddr",
                            "sWidth" : "20%",
                            "aTargets" : [ 0 ]
                        }, {
                            "mData" : null,
                            "sWidth" : "20%",
                            "mRender" : function(data, type, row) {
                                return row.firstNm + ' ' + row.lastNm;
                            },
                            "bSortable" : false,
                            "aTargets" : [ 1 ]
                        }, {
                            "mData" : "retailerId",
                            "sWidth" : "20%",
                            "aTargets" : [ 2 ]
                        }, {
                            "mData" : "role",
                            "sWidth" : "20%",
                            "aTargets" : [ 3 ]
                        }, {
                            "mData" : "contactPhone",
                            "bSortable" : false,
                            "sWidth" : "20%",
                            "aTargets" : [ 4 ]
                        } ],
                        "fnInfoCallback": function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) {       
                            return 'Displaying '+iStart+' to '+iEnd+' of ' +iTotal + ' entries';
                }

回答1:


Please use this way:

var DTable = $("<your table>").dataTable ({
...
..
}

setInterval( function () {
        DTable.fnClearTable( 0 );
        DTable.fnSort( [ 0, "desc" ] );
        DTable.fnDraw();

    }, 5000 );



回答2:


If I understood you correctly you want to a function that will return the default order? If that is the case I think that the following will be helpfull:

http://datatables.net/plug-ins/api/fnSortNeutral



来源:https://stackoverflow.com/questions/22839241/reset-to-default-sorting-in-jquery-datatable

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