0
votes

I am using Jquery Datatable for my plugin. Below is the code sample:

$(function() {
    $( "#tab-per" ).DataTable();
});

$(function() {
    var form = document.getElementById("postme");
    $('form#postme').on('submit', function(e) {
            $.post('cnp.php?hal=103.sv', $(this).serialize(), function (data) {
            alert('Data perusahaan sukses disimpan !');
            $( "#tab-per" ).load( "cnp.php?hal=103 #tab-per" );
            $( "#tab-per" ).DataTable();
            form.reset();
            // This is executed when the call to mail.php was succesful.
            // 'data' contains the response from the request
            }).error(function() {
            alert('Mohon maaf ada kesalahan')
            // This is executed when the call to mail.php failed.
           });
          e.preventDefault();                   
    });
});

The problem is, datatable pagination and search is not working anymore when I click the submit button to save data to my database after the table gets refreshed.

1
any error you are getting in console ? ?Punit Gajjar
you can try this if its works. $( "#tab-per" ).fnDraw(); instead of $( "#tab-per" ).DataTable(); after submitting the formPunit Gajjar
@PunitGajjar no there is no error in my console but when i change $( "#tab-per" ).fnDraw(); instead of $( "#tab-per" ).DataTable(); this is the error i got : cnp.php?hal=103:217 Uncaught TypeError: $(...).fnDraw is not a function(anonymous function) @ cnp.php?hal=103:217l @ jquery-2.0.3.min.js:4c.fireWith @ jquery-2.0.3.min.js:4k @ jquery-2.0.3.min.js:6(anonymous function) @ jquery-2.0.3.min.js:6Deny Rachmat
@DenyRachmat Check if your AJAX response is proper and all required JS files are includedsid
@sid i think what's wrong with my code when i using .load() function, it only executing #tab-per id of my table, so i think that code ignoring my datatable function :(Deny Rachmat

1 Answers

0
votes

you can do this:

 var tab-per-table;
 $(function() {
    tab-per-table=$(function() {
        $( "#tab-per" ).DataTable();
    });
 });
//when you post success,you can delete.$( "#tab-per" ).DataTable(),just do:
 tab-per-table.draw();