2

I need to load whole table into div with $.ajax, but after that, Datatables won't work. Can i do something about it?

3 Answers 3

1

Probably, the liveQuery plugin could help in that case.

Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe that this could be dont with .live() instead, isnt' it?
@Nik: The live() doesn't support plugins' cutom events/methods.
Hei. I figured it out. It was much simpler. I just had to put datatables code after filling result div like that: $.post(url, posts, function(data){ $("#results").empty().append(data); $('.datatables').dataTable(); });
0
$(function(){
  $.get("script.php",function(data){
    $.each(data.items, function(i,item){
      $("#anydiv").prepend(item.element);          
    });
  })
})

In script.php read array of database results into array and :

echo json_encode($results_array);

Comments

0

The Datatable jQuery call needs to be made after the table exists - if you have it in document ready, the table won't exist yet, until the AJAX call is made - so put the data table initialisation in your AJAX function and you're home and hosed.

$.ajax({
  type: "POST", 
  url: "page.php", 
  data: { variable: variable }, 
  success: function(data) {
    $("#your_div").html(data);
    $('#yourtable').DataTable({
      "iDisplayLength": 10,
      dom: 'Bfrtip',
      etc...
    });                        
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.