寻找有关将fnSort放在此datatables代码中的位置的帮助,以使默认排序第二列而不是第一列。我有:

dataClones['keyword_table_<?php echo ceil($countCallsCounter/7); ?>']=document.getElementById('keyword_table_<?php echo ceil($countCallsCounter/7); ?>').cloneNode(true);

    $('#keyword_table_<?php echo ceil($countCallsCounter/7); ?>').dataTable( {
    //"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
    "sDom":"<<'span6'l><'span6'f>r>t<<'span6'i><'span6'p>>",
    "sPaginationType": "bootstrap",
    "oLanguage": {
    "sLengthMenu": "_MENU_ records per page"
    }

}
);


我知道我需要添加http://datatables.net/api#fnSort,但是我对JS不太熟悉,因此在可能的情况下寻求一些快速帮助。

谢谢

最佳答案

您所要做的就是将变量=设置为$(selector).datatable()调用。然后,您可以调用variable.fnSort()。

var oTable; // Make sure this is within scope of your function, or where you want to execute the sort.

dataClones['keyword_table_<?php echo ceil($countCallsCounter/7); ?>']=document.getElementById('keyword_table_<?php echo ceil($countCallsCounter/7); ?>').cloneNode(true);

    oTable = $('#keyword_table_<?php echo ceil($countCallsCounter/7); ?>').dataTable( {
    //"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
    "sDom":"<<'span6'l><'span6'f>r>t<<'span6'i><'span6'p>>",
    "sPaginationType": "bootstrap",
    "oLanguage": {
    "sLengthMenu": "_MENU_ records per page"
    }

}
);

function onSort(){
    oTable.fnSort([0,'asc']);
}


另外,如果您知道要在数据表的初始化时进行排序的列,则可以:

$('#example').dataTable( {
    "aaSorting": [[ 4, "desc" ]]
} );


http://datatables.net/api#fnSort
http://datatables.net/release-datatables/examples/basic_init/table_sorting.html

关于javascript - 数据表fnSort建议,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14787519/

10-09 22:19