MedKostali MedKostali - 10 months ago 164
Ajax Question

reload datatable after ajax success

I use JQuery DataTable. I send data to datatable onclick in json file at ajax succes .the first click everything is good,But the next click I get only the right data ANd wrong value of dataTables_info it display always the first value of dataTables_info And paginatio AND row too from the first result.
This is the first display of data in datatable:

The first Click

ALL the next Click I get only right data:
For this exemple they are one result showing in picture below but everything else(info ,show,pagination) belong to first search showing in the first picture :
enter image description here

In the second Exemple When I click at any page of pagination I get the content of the first page result!!
This is my function ONclick:

$ ( '#ButtonPostJson' ).on('click',function() {

$("tbody").empty();
var forsearch = $("#searchItem").val();

$.ajax({
processing: true,
serverSide: true,
type: 'post',
url: 'searchData.json',
dataType: "json",
data: mysearch,
/* bRetrieve : true,*/

success: function(data) {
$.each(data, function(i, data) {
var body = "<tr>";
body += "<td>" + data.name + "</td>";
..........................
..........................
body += "</tr>";
$('.datatable-ajax-source table').append(body);

})
;
/*DataTables instantiation.*/
$('.datatable-ajax-source table').dataTable();
},

error: function() {
alert('Processus Echoué!');
},
afterSend: function(){
$('.datatable-ajax-source table').dataTable().reload();
/* $('.datatable-ajax-source table').dataTable({bRetrieve : true}).fnDestroy();
$(this).parents().remove();
$('.datatable-ajax-source table').dataTable().clear();*/
}
});
});


I try everything and i dont know what I miss.
I use this jquery for datatable:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>


Thanks.

Answer Source

This link Refreshing data in jQuery DataTables

is verry helpfful ,I inspire from it to resolve my problem .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download