how to destroy and reinitialize the datatable

how to destroy and reinitialize the datatable

SriRishithaSriRishitha Posts: 42Questions: 4Answers: 0

i created a datatable dynamically like this

var table = $('#equictntbl').DataTable({

                    retrieve: true,
                    columns: cols,
                    ServerSide: true,
                    dom: 'Bfrtip',
                    buttons: [
                       {
                           extend: 'print',
                           title: 'Report   ',
                           //text: '<i class="fa fa-print"></i> Print',
                           //title: $('h1').text(),
                           exportOptions: {
                               //columns: ':not(.no-print)'
                               columns: ':visible', stripNewlines: true
                           },

                           orientation: 'landscape',
                           pageSize: 'C2',
                           footer: false
                       },
                        {
                            extend: 'excelHtml5', title: 'Report', footer: true, exportOptions: {
                                columns: ':visible', stripNewlines: false
                            },
                            orientation: 'landscape',
                            pageSize: 'LEGAL',
                        },
                        {
                            extend: 'pdfHtml5', title: 'Report',
                            columns: ':visible',
                            orientation: 'landscape',
                            pageSize: 'C2',
                            text: '<i class="fa fa-file-pdf-o"> PDF</i>',

                            footer: false
                        },
                        {
                            extend: 'colvis',
                            columnText: function (dt, idx, title) {
                                return (idx + 1) + ': ' + title;
                            },
                            postfixButtons: ['colvisRestore']
                        },
                         {
                             extend: 'pageLength', footer: false, exportOptions: {
                                 stripNewlines: false
                             }
                         },
                    ],
                    iDisplayLength: 25,
                    lengthMenu: [[10, 25, 50, -1],
                        ['10', '25', '50', ' all']
                    ],
                    pagingType: "full_numbers",
                    deferRender: true,

                });
                table.rows.add(resp).draw();
                debugger

            }
            else if (resp.length == 0) {
                debugger
                toastr.error("No data found");
                $('#reportdiv').hide();
        }
            else {
                debugger
                $('#reportdiv').hide();
                toastr.error("Please fill all the details");
            }
        }
    });

if ($.fn.DataTable.isDataTable('#viewtbl')) {

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

                }

Replies

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    edited September 2017

    Use .DataTable() (with a capital D) and then you can use functions like clear and destroy

    See the first FAQ - https://datatables.net/faqs/index#Most-common-FAQs

  • SriRishithaSriRishitha Posts: 42Questions: 4Answers: 0

    $('#equictntbl').DataTable().clear();
    $('#equictntbl').DataTable().destroy();
    i write like this but my table not reloading with new columns data.

  • SriRishithaSriRishitha Posts: 42Questions: 4Answers: 0

    discussion closed

  • pepsi7959pepsi7959 Posts: 1Questions: 0Answers: 0

    //Destroy the old Datatable
    $('#equictntbl').DataTable().clear().destroy();

    //Create new Datatable
    $('#m_device_table').DataTable({...})

    Ref

  • badbytebadbyte Posts: 31Questions: 6Answers: 0

    I'm using DataTables 1.10 and did a clear before destroying the Datatables but when I'd want to reinitialize the table again I but I get in the console output of the browser the following:
    ```TypeError: h is undefined

  • colincolin Posts: 5,893Questions: 0Answers: 1,020

    Hi @badbyte,

    It's working as expected here. Can you modify that test case to demonstrate the problem, please.

    Cheers,

    Colin

Sign In or Register to comment.