Jquery Data table Destroying and Recreation

Today i have come up with the very useful technique for the people who are using jquery datatable.
In our case we have a html table ,we are going to change it as jquery datatable.Because jquery data table has lot of options like filtering,exporting and so on.This is the very daunting task most of the time .So here i have a method to do this operation.I checked this.It works fine.



Necessary scripts
 <script src="~/Datatable/script/jquery.dataTables.min.js"></script>
    <script src="~/Datatable/script/dataTables.buttons.min.js"></script>
    <script src="~/Datatable/script/jszip.min.js"></script>
    <script src="~/Datatable/script/pdfmake.min.js"></script>
    <script src="~/Datatable/script/vfs_fonts.js"></script>
    <script src="~/Datatable/script/buttons.html5.min.js"></script>
    <script src="~/Datatable/script/buttons.print.min.js"></script>
Necessary css
    <link href="~/Datatable/css/jquery.dataTables.min.css" rel="stylesheet" />

    <link href="~/Datatable/css/buttons.dataTables.min.css" rel="stylesheet" />
Jquery
   //---------datatable destroy------------------
                    if ($.fn.DataTable.isDataTable("#test")) {
                        $("#test").dataTable().fnDestroy();
                    }
              
   //------------------------------

 //----------------datatable recreation----------------------
                            $('#test').dataTable({
                                "displayLength":10,
                                  "dom": 'Bfrtip',
                                "columnDefs": [{
                                    "defaultContent": "-",
                                    "targets": "_all"
                                }],
                              buttons: [{
                                    extend: 'collection',
                                    text: 'Export',
                                    buttons: ['copy', 'excel','csv','pdf','print' ]
                                }]

                            });
 //-------------------


HTML

<table id ='test'> 
<thead>
<tr><th>Name</th><tr>
</thead>
<tbody>
<tr><td>ssn</td></tr>
<tr><td>test</td></tr>
<tr><td>Hello</td></tr>
</tbody>
</table>


I hope this will be helpful for the people who are using HTML table.



Comments

Popular posts from this blog

Types of Architects

Basic measurements

Search html table contents based on its td using Jquery