本文介绍了无法重新初始化 DataTable - 数据表的动态数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a datatable showing all employees.It is working fine for all employees on document.ready.I have a select tag containing the type of employees like 'project_manager' & 'team_leader' , and on change of employee type I am calling a function get_employees(emp_type) and passing the selected employee type.

它正在 ajax 响应中获得所需和正确的数据,但抛出警告

It is getting desired and proper data in ajax response, but throwing warning

DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

我试图摧毁它,但没有成功.

I tried to destroy it, but no luck.

也试过了

$('#example').dataTable().fnClearTable();
$('#example').dataTable().fnDestroy();

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

it is clearing table and showing newly appended data, but adding new sort images with column name every time.

这是我的代码片段.

$(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,
                    });
                }
            });
        }

提前致谢.

推荐答案

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

In the current version of DataTables (1.10.4) you can simply add destroy:true to the configuration to make sure any table already present is removed before being re-initialised.

$('#example').dataTable({
    destroy: true,
    aaData: response.data
});

这篇关于无法重新初始化 DataTable - 数据表的动态数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:15