reloading ajax table after edit

reloading ajax table after edit

kokotkokot Posts: 7Questions: 0Answers: 0
edited May 2012 in General
Hi there,
i can't manage how to reload table after data has been changed with php.

Here is my editor :
[code]
var editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "php/table.test.php",
"domTable": "#zoznam",
"fields": [
{
"label": "Hostname",
"name": "hostname",
"type": "hidden"
},
{
"label": "IP",
"name": "ip",
"type": "text"
}
],
} );
[/code]

When I edit this fields and submit, data from html fields are parsed to table.test.php that looks like :

[code]
<?php

$db = mysql_pconnect( " ", " ", " " ) or
die( 'Could not open connection to server' );

mysql_select_db("hostname", $db ) or
die( 'Could not select database '. $sql_details['hostname'] );

include( "include/DTEditor.mysql.class.php" );

extract($_POST); // here i've deloy POST
$hostname=$data['ip'].".domain.com";
//here i modify hostname
$data['hostname']=$hostname;
$stack=array(
"action" => $action,
"table" => $table,
"id" => $id,
"data" => $data
);

$editor = new DTEditor(
$db,
'zoznam',
'id',
'row_',
array(
new DTField( array(
"name" => "hostname",
"dbField" => "hostname",
"dynamicGet" => true,
"validator" => 'DTValidate::none',
"opts" => array(
"required" => true
)
) ),
new DTField( array(
"name" => "ip",
"dbField" => "ip",
"dynamicGet" => true,
"validator" => 'DTValidate::none',
"opts" => array(
"required" => true
)
) )
)
);
echo json_encode( $editor->process($stack) );

?>
[/code]

All working but there is a problem with refresh of table. When table.test.php returns JSON data with new hostname, table will not refresh automaticaly. I am new to JS and JQ, please help me to solve it. I just need to refresh table with fresh data after editing table.

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi - hope you are enjoying the Editor trial :-).

    What you want to do is use the onSetData event to pass the information that is being returned in your JSON into the data object that will be given to DataTables, so DataTables can then process that data accordingly. There is an example of how to do that shown here: http://editor.datatables.net/release/DataTables/extras/Editor/examples/tableOnlyData.html

    I'm presuming here that your server-side script is updating the values in some way (like in my example for dates).

    Regards,
    Allan
  • kokotkokot Posts: 7Questions: 0Answers: 0
    edited May 2012
    Hi,
    thanks a lot! It works..!
    Is there available any documentation for new DTEditor() that DTField array options? My next steps will be to find how to restrict diacritics characters / some regex's like IP address in DataTable Editor fields ?

    Regards,
    Mark
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > Is there available any documentation for new DTEditor() that DTField array options?

    Those options aren't actually documented on the web-site yet - I want to add an ASP server-side version first, and then provide documentation in a way that will be useful for both of them. However, they are documented in the source files if you have a look at them.

    In particular, if you want to do custom validation have a look at the DTValidate class - it has functions in it which can be used to do input validation (it actually has an IP address option already - DTValidate::ip.

    These functions can then be used with the 'validator' option of the DTField class - for example:

    [code]
    new DTField( array(
    "name" => "ip",
    "dbField" => "ip",
    "validator" => "DTValidate::ip"
    ) );
    [/code]

    One of the things I often do, if I don't directly want to add another static function to the validation class is just use a closure function for the "validator" in DTField:

    [code]
    new DTField( array(
    "name" => "ip",
    "dbField" => "ip",
    "validator" => function ( $val, $data, $field ) {
    return true; // or false depending on logic
    }
    ) );
    [/code]

    That's useful for one off validation functions, but of course, adding functions to the DTValidate class is good for code reuse :-)

    Regards,
    Allan
This discussion has been closed.