2017-05-30 45 views
0

我正在爲我的餐桌使用Datatable。一旦創建了DataTable,我正面臨着關於更新數據的幾個問題。如何在HTML-JS中獲取DataTable的更改?

我有8條記錄。分頁是5.意味着有2頁。我有複選框的列。當我在table.DataTable()中轉換表格時,我檢查了數據,使用DataTable.rows()也沒問題。

但是,當我執行任何更改,如取消選中任何複選框,更改文本框的值,它不反映。 更改後我使用console.log檢查DataTable的值。它在初始化時顯示相同的數據。

Steps to reproduce 
1- Create table and convert into DataTable 
2- Console.log table data 
3- uncheck few checkbox and add some text in textbox 
4- Console.log. It will show same data of Step-2. 

代碼:

HTML 
<table id="products" style="width: 100%; font-family: Segoe UI,Tahoma,Arial;" cellspacing="0" cellpadding="0" border="0"> 
    <caption class="caption-title">Title</caption> 
    <tbody id="ptbody"></tbody> 
    <thead> 
     <tr> 
      <th>Select Product</th> 
      <th>Product Name</th> 
      <th>Qty</th> 
      <th style="display:none"></th> 
     </tr> 
    </thead> 
</table> 

JS 
var ProductList = []; 
ProductList.push("A"); 
ProductList.push("B"); 
ProductList.push("C"); 
ProductList.push("D"); 
ProductList.push("E"); 
for (var j = 0; j < 5; j++) { 
    strTableStructure = strTableStructure + "<tr>"; 
    strTableStructure = strTableStructure + "<td><input id='chk_" + ProductList[j] + "' checked='checked' type='checkbox'></td>"; 
    strTableStructure = strTableStructure + "<td>" + ProductList[j] + "</td>"; 
    strTableStructure = strTableStructure + "<td><input id='txtQty_" + j + "' type='text' maxlength='110' ></td>'"; 
    strTableStructure = strTableStructure + "</tr>"; 
} 

$("#ptbody").html(strTableStructure); 
var table = $('#products').DataTable(); 

可有人建議我如何體現更改時用戶選擇/取消或文本框的變化值?

回答

0

我按照類似於MyTasks按鈕的做法here.請記住,如果您附加事件的元素是由datatables API創建的,那麼您需要以不同的方式引用事件。

$(document).on('event', 'element', function(){ 
    //do stuff 
} 

如果你正在嘗試刷新數據使用table.ajax.reload();或搜索基於該事件中使用table.search('value').draw()

+0

我提交按鈕。我想在提交點擊時獲得最新的複選框和文本框的值。 'console.log(objDataTable.rows())'應該列出更新的值。它始終保持初始階段的價值。你能舉個小例子嗎? –

+0

@NanjiMange在我編輯我的答案之前,你可以發佈一些你的標記嗎?我想我明白了,但在這裏糾正我。當您使用objectDataTable.rows()獲取這些值時,它不會/不會提供更新的值。 – Eric

+0

我已經添加了問題。請建議 –