table.destroy can not work

table.destroy can not work

YkillMYkillM Posts: 3Questions: 1Answers: 0

when I use table.destroy(),
I get
TypeError: headerCell[i] is undefined
And I can not find out how to deal
emmmm..., I am a Chinese user,so forgive my bad English.
Thanks

Answers

  • hotswaphotswap Posts: 6Questions: 1Answers: 1

    Hi,

    Please post part of your code so that we can see the context.

    Thank you!

  • YkillMYkillM Posts: 3Questions: 1Answers: 0

    I am sorry to forget to post my code.And her is my code.

    $.ajax({
            cache : false,
            type : "post",
            url : "../../userManage.do?action=all",
            async : false,
            error : function(request) {
                console.log("fail to submit ajax");
                console.log(request);
            },
            success : function(data) {
                console.log('得到的数据' + data);
                var userList = data.data;
                console.log('表格信息'+userList);
                $('#table_id_example').DataTable().clear();
                $('#table_id_example').DataTable().destroy();
                //console.log(mydataTable);
                mydataTable = $('#table_id_example').DataTable({
                    "jQueryUI" : true,
                    "scrollCollapse" : true,
                    "scrollY" : 600,
                    searching : false,
                    lengthChange : false,
                    pageLength : 15,
                    data : userList,
                    columns : [ {
                        data : 'id',
                        width : '30px'
                    },
                        {
                            data : 'username'
                        },
                        {
                            data : 'name'
                        },
                        {
                            data : 'sex',
                            width : '30px'
                        },
                        {
                            data : 'department'
                        },
                        {
                            data : 'role'
                        },
                        {
                            data : 'createtime'
                        },
                        {
                            data : 'creatpeople'
                        },
                        {
                            targets : 8,
                            width : '120px',
                            render : function(data, type, row) {
                                return '<div class="tableBtn">' +
                                    '<div class="item" style="float:left">' +
                                    '<a onclick="alterUser(this)" style="text-decoration:none"><img src="/IDCIM/images/m_user/modify.png">&nbsp;<span style="color:#bebebe">编辑</span></a></div>' +
                                    '<div class="item">' +
                                    '<a onclick="deleteUser(this)" style="text-decoration:none"><img src="/IDCIM/images/m_user/delete.png">&nbsp;<span style="color:#bebebe">删除</span></a></div>' +
                                    '</div>';
                            }
                        },
                        {
                            data : 'password',
                            visible : false
                        },
                        {
                            data : 'state',
                            visible : false
                        },
                    ],
                });
            }
        });
    

    After I get new data,I want to flash the table.
    But when it run to $('#table_id_example').DataTable().destroy();
    I get the error.The error and the detail is behind.

    TypeError: headerCells[i] is undefined userManage.html:5603:5
    _fnCalculateColumnWidths() userManage.html:5603
    _fnAdjustColumnSizing() userManage.html:2150
    <匿名> userManage.html:8648
    .iterator() userManage.html:7029
    <匿名> userManage.html:8647
    _Api.extend/methodScoping/<() userManage.html:7197
    <匿名> userManage.html:8631
    _Api.extend/methodScoping/<() userManage.html:7197
    <匿名> userManage.html:9300
    .iterator() userManage.html:7029
    <匿名> userManage.html:9278
    _Api.extend/methodScoping/<() userManage.html:7197
    init/<.success() userManage.html:50
    r.Callbacks/i() userManage.html:2
    r.Callbacks/j.fireWith() userManage.html:2
    A() userManage.html:4
    .send/c/<() userManage.html:4
    .send() userManage.html:4
    .ajax() userManage.html:4
    init() userManage.html:36
    .success() userManage.html:187
    r.Callbacks/i() userManage.html:2
    r.Callbacks/j.fireWith() userManage.html:2
    A() userManage.html:4
    .send/c/<() userManage.html:4

    I know it is to long to read.So thank you a lot!

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    I think we'd actually need a link to a page showing the issue please.

    Allan

  • YkillMYkillM Posts: 3Questions: 1Answers: 0

    Sorry,I can not give you a link to a page because my work computer can not connect to the Internet.
    Can you tell me how to debug it or how to find out the way to solve it,maybe I can find the bug by myself.
    Thanks a lot!

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Are you checking to see if the table was created before you try to destroy it? You will get an error if you try the destroy before creating it.

    Use https://datatables.net/reference/api/%24.fn.dataTable.isDataTable() see if the table exists if it returns true, then use the destroy

This discussion has been closed.