Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Reinitialize Datatable in Ajax

Tags:

jquery

ajax

What I Need

*When Ajax is loaded datable is reintailized.

  • i would explain in steps

    1. Here is the output of first step :http://postimg.org/image/c6p8jwp3b/.
    2. Here is the output of second step :http://postimg.org/image/6fm1z253h/
    3. Here is the output of third step:http://postimg.org/image/5btny60xf/.
  • No Reinitialize Of datatable.

  • I just want paging and search should be reintialized.

  • i have taken help from this url: http://datatables.net/forums/discussion/256/fnreloadajax/p1.

Ajax call code:

   if ($('#teamTable').size() > 0)
    {

        $('#teamTable').dataTable({
            "sPaginationType": "bootstrap"
        });
    }

      $("#save_team").click(function() {
            $.ajax({
            type: "POST",
            url: "asana_team.php",
            data: { people_name: $('#e2').val(), team_name:$('#teamname').val() },
            beforeSend : function(){
                $("#team_table").remove();
                $("#team_table_div").append("<center id=\"loading\" style=\"margin-top:25%;margin-bottom:25%\"><img src='../common/images/ajax-loader.gif' /></center>");
            },
            contentType: "application/x-www-form-urlencoded"
            }).done(function(data) {
                $("#loading").remove();
                $('#team_table_div').append(data);
                $('#teamTable').dataTable({
                    "sPaginationType": "bootstrap"
                });                    
            });
        });

* working fine but i reintializing pagination in datatable no datable is loaded.

  • i have used this code to reinitailize table.

     function callBack()
     {
     var call= $('#teamTable');
    
    call.dataTable({
     "sPaginationType": "bootstrap",
     "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
     "oLanguage": {
     "sLengthMenu": "_MENU_ records per page"
     } });
    }
    $(document).ready(function() {
    callBack();
    });
    
like image 680
user3703097 Avatar asked Jun 27 '14 12:06

user3703097


People also ask

How to clear DataTable and reinitialize in jQuery?

Remove destroy:true option and instead of destroying and recreating the table use clear() to clear the table content, rows. add() to add the table data and then draw() to re-draw the table. In this case you would need to initialize DataTables once on page initialization. Save this answer.

How to resolve cannot reinitialise DataTable?

This error is triggered by passing in options to a DataTables constructor object when the DataTable instance for the selected node has already been initialised. For example: $('#example'). dataTable( { paging: false } ); $('#example').


2 Answers

This worked for me after a lot of research:- First check if dataTable exist or not, if it does then destroy dataTable and recreate it

if ($.fn.DataTable.isDataTable("#mytable")) {
  $('#mytable').DataTable().clear().destroy();
}

$("#mytable").dataTable({...
                       
                });
like image 84
Ravi.Dudi Avatar answered Sep 22 '22 15:09

Ravi.Dudi


This worked for me:

First I initalize the datatable, with

$(".myTable").DataTable({ "bDestroy": true, .... });

The "bDestroy" attribute was necessary in my case.

Then when I have something to append dynamically to my table I do the following:

$(".myTable").dataTable().fnDestroy();
//Append stuff to my table
$(".myTable").DataTable({ "bDestroy": true, ... });

Also notice that I use the dataTable() function when destroying the table and the DataTable() function when initalizing it.

like image 30
Eiiki Avatar answered Sep 20 '22 15:09

Eiiki