2014-11-25 30 views
1

在我的JQuery數據表中,row.add不起作用,並引發錯誤說add函數是未定義的。該錯誤消息是:將行添加到jQuery中的數據表的功能給TypeError

Uncaught TypeError: Cannot read property 'add' of undefined 

jsfiddle link

HTML

<table id="example" class="display" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th>Column 1</th> 
       <th>Column 2</th> 
       <th>Column 3</th> 
      </tr> 
     </thead> 

     <tfoot> 
      <tr> 
       <th>Column 1</th> 
       <th>Column 2</th> 
       <th>Column 3</th> 
      </tr> 
     </tfoot> 
    </table> 
<button id="addRow">Add new row</button> 

的javascript

$(document).ready(function() { 

    var counter = 1; 
    var prntTable = $('#example').dataTable({ 
     "aoColumns" : [ 
      {"bSearchable" : false}, 
      {"bSearchable" : true}, 
      {"bSearchable" : true} 
     ],           
     "sPaginationType" : "full_numbers" 
    }); 

    $('#addRow').on('click', function() { 
     prntTable.row.add([ 
      counter +'.1', 
      counter +'.2', 
      counter +'.3' 
     ]).draw(); 

     counter++; 
    }); 

    $('#addRow').click(); 
}); 

回答

2
$('#addRow').on('click', function() { 
     prntTable.row.add([ 
      counter +'.1', 
      counter +'.2', 
      counter +'.3' 
     ]).draw(); 

     counter++; 
    }); 

在這段代碼prntTable是undefined.Either把它定義爲一個全局變量或點擊功能

重新定義它添加

var prntTable = $('#example').DataTable(); 

點擊功能

Fiddle

+0

我得到錯誤遺漏的類型錯誤:無法讀取屬性未定義 – 2014-11-25 06:36:21

+0

「添加」小提琴工作正常me..Have你檢查了它..?? – Outlooker 2014-11-25 06:38:17

+0

是的小提琴工作正常.. – 2014-11-25 06:39:35

6

而不是:

var prntTable = $("#example").dataTable(); 

嘗試:

var prntTable = $("#example").DataTable(); 

它看起來像舊的DataTable API dataTable()不支持您所呼叫的功能。使用新的API:DataTable()。在這裏閱讀更多的信息:Datatable API

1

嘗試使用fnAddData它爲我工作

table.fnAddData(['1','1','2','3','4']); 
相關問題