简体   繁体   English

DataTables无法添加行

[英]DataTables cannot add row

I'm trying to add data to a table dynamically through JavaScript, and it returns this: 我正在尝试通过JavaScript动态地向表中添加数据,并返回:

Uncaught TypeError: Cannot read property 'add' of undefined. 未捕获的TypeError:无法读取未定义的属性“add”。

EDIT: The code works perfectly without the row.add line. 编辑:代码完美无需row.add行。

Relevant code: 相关代码:

<html>
<head>
<link rel="stylesheet" type="text/css" href="./css/jquery.dataTables.min.css">

<script type="text/javascript" src="./lib/jquery.min.js"></script>
<script type="text/javascript" src="./lib/jquery.dataTables.min.js"></script>

<script>
var dataSet = [
    ['1.1','2.1'],
    ['1.2','2.2'],
];

$(document).ready(function() {
    $('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
    t = $('#example').dataTable( 
    {
        data: dataSet,
        columns: [
            { "title": "Col 1" },
            { "title": "Col 2" },
        ],
    });

    t.row.add(['1.3', '2.3']) // <-- Fails
});
</script>
</head>

<body>
 <div id="demo" style="width:500px"> </div>
</body>
</html>

You are very close. 你很近。 Here is the change I have done to your code: 以下是我对您的代码所做的更改:

a. 一个。 While initializing DataTable, used capital D. b. 在初始化DataTable时,使用了资本D. b。 Used .draw(); 二手.draw(); while adding row. 同时添加行。

 var dataSet = [ ['1.1','2.1'], ['1.2','2.2'] ]; $(document).ready(function() { $('#demo').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>'); var t = $('#example').DataTable({ data: dataSet, columns: [ { "title": "Col 1" }, { "title": "Col 2" } ] }); t.row.add(['1.3', '2.3']).draw(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet"/> <script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script> <div id="demo" style="width:500px"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM