2012-11-24 46 views
2

我有一個使用數據表插件的表。我試圖動態地使用該從中取出一排:刪除數據表中的行

$(document).ready(function() { 

    var oTable = $("table#demo-dtable-02").dataTable({"aaSorting": []}); 

    $(".icon-remove").on('click',function(){ 
     var anSelected = fnGetSelected(oTable); 
     deleted=oTable.fnDeleteRow(anSelected[0]); 
     }); 

       }); 

我得到以下錯誤的ReferenceError:我嘗試使用fnGetSelected沒有定義 (從這裏開始:jQuery Data Tables plugin not removing table row

$(this).parent('tr').remove(); 

這當然刪除行,但不會重置頁腳中的文本,如 顯示17個條目中的第1至10個。 以這種方式去除並不是最好的解決方案。

我的DOM代碼如下所示:

<?PHP 
              $count=1; 
foreach($ledgers as $row) {?> 
    <tr > 
    <td><a onClick="viewDetails(<?PHP echo $row['id'];?>)" style="cursor:pointer"><?PHP echo $row['name'];?></a></td> 

    <td><?PHP echo $row['email'];?></td> 
    <td><?PHP echo $row['contact_number'];?></td> 

    <td> 
    <i class="icon-remove" rowcount=<?PHP echo $count;?> style="cursor:pointer; margin-right:5px" title="Remove Ledger" id="removeLedgerNow"></i> 

    <i class="icon-edit" style="cursor:pointer" title="Edit Ledger" onclick="viewDetails(<?PHP echo $row['id'];?>)"></i> 

    </td> 
    </tr> 
    <?PHP $count++; } ?> 

評論。

+0

的?這不是你如何設置點擊事件 – Abhilash

+0

看到我的編輯。它不工作。 – beNerd

+0

查看我的回答。它應該工作 – Abhilash

回答

4

更新時間:

試試這個代碼:

$('.icon-remove').on('click', function() { 
    // Get the position of the current data from the node 
    var aPos = oTable.fnGetPosition($(this).closest('tr').get(0)); 
    // Delete the row 
    oTable.fnDeleteRow(aPos); 
}); 

注:

  1. 還有就是API中沒有fnGetSelected。這可能是OP所具有的自定義功能,但不是在問題中產生的。
  2. fnGetPosition被接受tdth,或tr元素
  3. .get(0)是打破元素出來你爲什麼要使用`。每()`jQuery對象
+0

結果在這個錯誤:aPos是空的。您在fnGetPosition中使用了'this',但在這種情況下,它是類(.icon-remove)處理程序,它只是每行中的一個按鈕。只是猜測:) – beNerd

+0

只是爲了清楚起見,我使用的'.icon-remove'類被設置爲每個'tr'中的最後一個'td'。你的DOM如何? – Abhilash

+0

請參閱我的編輯問題 style =「cursor:pointer; margin-right:5px」title =「Remove Ledger」id =「removeLedgerNow 「>這條線是唯一的。 – beNerd