Datatables插件错误Uncaught TypeError:无法读取属性' mData'未定义的

时间:2016-03-13 10:57:04

标签: jquery datatables

我正在使用jQuery Datatables plugin。分页不起作用所以我尝试在使用ajax / jQuery追加我的行之后初始化数据表,希望一旦添加了所有行并初始化了数据表,它就会起作用。但是我收到了一个错误:

 "Uncaught TypeError: Cannot read property 'mData' of undefined."

我只是想让分页工作,所以当通过jQuery添加行时,我甚至不确定这是否是正确的方法。

HTML

<table id="table" class="table-striped" style="width:100%">
    <thead>
        <tr>
        <td>Date</td>
        <td>Route</td>
        <td></td>
        </tr>
    </thead>
    <tbody id="tablebody">
    </tbody>
</table>

的jQuery

function loadfromDB(event) {
var uid = $('#uid').val();

        $.ajax({
            type: "POST",
            url: "getplans.php",
            data: {uid:uid},
            dataType: 'json',
            cache: false,
        })
        .success(function(response) {
            $('input').removeClass('error').next('.errormessage').html('');
            if(!response.errors && response.result) {
                 $.each(response.result, function( index, value) {
                   $("#tablebody").append('<tr><td>'+value[4]+'</td><td>'+value[2]+'</td><td>'+value[3]+'</td></tr>');

                  }); 
             $('#table').DataTable();
            } 

            else {
                $.each(response.errors, function( index, value) {
                    // add error classes
                    $('input[name*='+index+']').addClass('error').after('<div class="errormessage">'+value+'</div>')
                });
            }
        });

}   

2 个答案:

答案 0 :(得分:0)

  1. 你可以使用ajax作为数据源构建你的数据表(我不会写它,它在数据表siet中有详细记录,请在那里查看)。

  2. 在init之后添加数据。很简单。我举个例子:

  3. var myTable = $('#myTableId')。DataTable({/ * ...此处设置选项* /});

    a)确保你从服务器返回一个有效的JSON,通过数据输入所需的输入进行CORRESPONDING b)修改。 ajax请求重新接收和解析JSON c)在响应的SUCCESS回调中:

        // this 2 lines to recive and parse JSON
        dataType: 'json',
        contentType: "application/json",
    
        success: function(json) {
    
         // first, empty the table:
         myTable.clear();
    
         // add the data and redraw the table:
         myTable.rows.add(json).draw();
    
    
        }
    

答案 1 :(得分:0)

检查表结构,确保表头与表格单元格相同。

<table>
    <thead>
        <tr>
            <th>header 1</th>
            <th>header 2</th>
            <th>header 3</th>
        </tr>
    <thead>
    <tbody>
        <tr>
            <td>table cell 1</td>
            <td>table cell 2</td>
            <td>table cell 3</td>
        </tr>
    </tbody>
<table>