2016-10-23 50 views
2

數據表1.10.12如何在DataTables中啓用排序(延遲加載)?

排序不適合我DataTables實施工作。我使用延遲加載的數據。

如何使分揀工作?

var hash_table = $('#hash_table'); 
var data_table = hash_table.DataTable({ 
    processing: true, 
    serverSide: true, 
    deferLoading: 100, 
    ordering: true, 
    order: [[ 0, 'asc' ]], 
    ajax: { 
    url: 'get_hashes/', 
    type: 'POST', 
    }, 
    columns: [ 
    {'title': 'BRC ID', 'data': 'brc_id'}, 
    {'title': 'HASH', 'data': 'hash'} 
    ], 
    dom: 'Brtip', 
    buttons: [ 
    { 
     extend: 'excel', 
     title: 'report', 
     text: 'Export', 
     extension: '.xlsx' 
    } 
    ] 
}); 
data_table.draw(); 

如果我點擊列標題中的排序圖標,結果不會改變。

截圖: enter image description here

回答

0

對於推遲的DataTable排序排序,排序必須在服務器端執行。 Datatables插件僅將指令發送到服務器,通知排序順序(asc, desc)和要排序的列的索引。 datatables protocol爲此定義了兩個特殊鍵:order[i][dir]order[i][column]

下面是發現哪個用戶點擊和排序順序列的名稱Python函數的例子:

def get_sortable_coll_name(message): 
    colls = { 
     '1': 'date', 
     '3': 'project', 
     '4': 'project_id', 
     '5': 'total_matches', 
     '6': 'successful_matches', 
     '7': 'failed_matches', 
     '8': 'status' 
    } 

    if message['order[0][dir]'] == 'asc': 
     return '-' + colls[message['order[0][column]']] 
    else: 
     return colls[message['order[0][column]']] 

然後就可以進行排序,例如,從Django模型獲取對象中:

sortable_coll = get_sortable_coll_name(message) 
objects = Model.objects.filter(task=task).order_by(sortable_coll) 
1

嘗試的按鈕元素之後添加此,我用它來我們的數據表數據表覆蓋分貝用JavaScript

"aaSorting": [ [1,'desc'], [2,'desc'], [0,'desc'] ], 
+0

這不提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/14091825) – VDWWD

+0

這確實提供了一個答案,因爲這是我用來排序在我們的數據表 –

+0

您確定您使用數據表來延遲加載數據嗎? https://datatables.net/examples/server_side/defer_loading.html – trex