2014-09-03 71 views
3

我試了幾個小時,並不能找出這一個...... 我在DataTables中添加了一些jQuery-UI的accordion-style-effect來在子行內顯示div。所以,當你點擊一行時,孩子被添加,div展開,如果你點擊同一行,div會隱藏一個動畫,並且孩子被刪除。如果您在小孩打開時單擊另一行,將隱藏第一個子div,然後刪除第一個子項,然後添加新子項並顯示div。DataTables刪除另一頁上的孩子

這對每個頁面都很好。但如果我切換頁面,第一個孩子不會被刪除。

我有這樣的代碼:

var openschoolingschild = "nothing"; //Variable to store css-id of row which has an open child 
var openschoolingsrow = ""; //Variable to store which row has a child 


$('#training_management_schoolings_items_table tbody').on('click', 'tr', function() { 
    var tr = $(this).closest('tr'); 
    var row = oTable.row(tr); 
    var thisid = $(this).attr('id'); 
    $('#training_management_schoolings_items_table tbody tr').removeClass('shown'); 
    if (row.child.isShown()) { 
     $('#'+thisid+'_details').hide("blind",300,function(){ 
      row.child.remove(); 
      tr.removeClass('shown'); 
     }); 
     openschoolingschild = "nothing"; 
     openschoolingsrow =""; 
    } 
    else { 
     if (openschoolingschild != "nothing") { 
     $('#'+openschoolingschild+'_details').hide("blind",300,function(){ 
      $("#"+openschoolingschild).removeClass('shown'); 
      openschoolingsrow.child.remove(); 
     }); 
     row.child(training_management_details(thisid) , "training_management_schoolings_table_child").show(); 
     tr.addClass('shown'); 
     $('#'+thisid+'_details').show("blind",300,function(){ 
      openschoolingschild = thisid; 
      openschoolingsrow = row; 
      preventcollapse(); 
      }); 
     } 
     else { 
     row.child(training_management_details(thisid), "training_management_schoolings_table_child").show(); 
     tr.addClass('shown'); 
     $('#'+thisid+'_details').show("blind",300,function(){ 
      preventcollapse(); 
      openschoolingschild = tr.attr('id'); 
      openschoolingsrow = row; 
     }); 
     } 
    } 
    }); 

jQuery的上部分是數據表init.DT回調函數中執行,當你看到我存儲其中有一個孩子在一個全局變量的行。

我想我不能刪除孩子,因爲它在我切換頁面時不存在於DOM中。但DataTables仍然保存數據,根據DataTables手冊和論壇,您可以訪問「隱藏」部分。我沒有使用表的服務器端處理。

我錯過了什麼?

+0

當你切換頁面並點擊行時,你有任何控制檯錯誤嗎?@Braco – 2014-09-09 08:10:30

+0

@SmartKiller不,不幸的是沒有。 – Broco 2014-09-09 21:39:25

回答

1

那麼,愚蠢的我。我剛剛找到一個解決方案。更多的解決方法,但沒關係,因爲當我更改頁面或重新繪製表格時,我並不需要保留子項。

"fnInfoCallback": function(){ 
     if (row != undefined) { 
     row.child.remove(); 
     } 

我將行指定爲全局變量,因此它是永久的。