无法重新初始化DataTable - 数据表的动态数据

Pra*_*mod 23 ajax datatable jquery dynamic destroy

我有一个显示所有员工的数据表.它对所有员工都很好document.ready.我有一个包含员工类型的选择标签'project_manager' & 'team_leader',在更改员工类型时,我正在调用一个函数get_employees(emp_type)并传递所选的员工类型.

它在ajax响应中得到了理想和适当的数据,但却发出了警告

DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3
Run Code Online (Sandbox Code Playgroud)

我试图摧毁它,但没有运气.

也尝试过

$('#example').dataTable().fnClearTable();
$('#example').dataTable().fnDestroy();
Run Code Online (Sandbox Code Playgroud)

它是清除表并显示新附加的数据,但每次都添加具有列名的新排序图像.

这是我的代码片段.

$(document).ready(function() {
            get_employees('all');
        });

        function get_employees(emp_type)
        {
            $.ajax({
                url: '../ajax_request.php',
                type: "POST",
                data: {
                    action: "admin_get_all_employees",
                    type: emp_type
                },
                success: function(response) {
                    var response = jQuery.parseJSON(response);

                    // $('#example').destroy(); tried this but haven’t worked

                    $('#example').dataTable({
                        "aaData": response.data,
                    });
                }
            });
        }
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Coi*_*_op 61

在当前版本的DataTables(1.10.4)中,您可以简单地添加destroy:true到配置中,以确保在重新初始化之前已删除任何已存在的表.

$('#example').dataTable({
    destroy: true,
    aaData: response.data
});
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是最好的!正如Coin_op所说:只是添加"毁灭:真实" (4认同)