Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery DataTables, refresh grid after update (server-side processing)

Ok, I've been working on a small project for use with DataTables. Its a jQuery grid plugin and Ive got most the functionality working as intended now. The only thing I cant seem to wrap my head around is making the grid refresh on AJAX Inline edit.

<script type="text/javascript" charset="utf-8">
    $(document).ready( function () {

       var oTable = $('#example').dataTable({

            "bJQueryUI": true,
            "bProcessing": true,
            "sAjaxSource": "/test/server_processing.php",
            "sPaginationType": "full_numbers",
            "aoColumns": [ { "bVisible":    false },
        null,
        null,
        null
    ]
        }).makeEditable({
            sAddURL: "AddData.php",
            sAddHttpMethod: "GET",
            sDeleteHttpMethod: "GET",
            sDeleteURL: "DeleteData.php",
            sUpdateURL: "UpdateData.php",

            oAddNewRowButtonOptions: {  label: "Add...",
                icons: {primary:'ui-icon-plus'} 
            },
            oDeleteRowButtonOptions: {  label: "Remove", 
                icons: {primary:'ui-icon-trash'}
            },

            oAddNewRowFormOptions: {    
                title: 'New Toll Free number',
                show: "blind",
                hide: "explode",
                modal: true
            },
            sAddDeleteToolbarSelector: ".dataTables_length"                             
        });
} );
</script>

This is my updatedata.php file

$editedColumn = $_POST['columnId'];
$editedValue = $_POST['value'];
$id = $_POST['id'];

if ($editedColumn == '1') {
    $sql = "update Main set name='$editedValue' where id='$id'";                    
} elseif ($editedColumn == '2') {
    $sql = "update Main set dn='$editedValue' where id='$id'";                  
} elseif ($editedColumn == '3') {
    $sql = "update Main set dn1='$editedValue' where id='$id'";                 
}
/* Update a record using information about id, columnName (property
 of the object or column in the table) and value that should be
 set */ 
$sql2 = "select name from Main where id = '$id';";

mysql_query($sql) or die(mysql_error());

echo "Update ok, reload to see changes";

I have the echo at the end because it seems to pop a alert() some where and the echo fills that alert with info.

I know of the functions for redrawing the grid like fnDraw but am clueless as how to implement.

like image 723
Kelso Avatar asked Feb 13 '12 19:02

Kelso


1 Answers

If you need to reload the AJAX data of your Datatable based on an event or something, just use the fnReloadAjax method. You can find the documentation here.

like image 85
Amir Avatar answered Oct 12 '22 23:10

Amir