updating json with values after posting

updating json with values after posting

vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
edited February 2013 in Editor
Hi Allan,
After processing POST the following code is used to get json for updating select field in client side
[code]
if ( !isset($_POST['action']) ) {
$out['schoolsList'] = $db
->select( 'schools', 'id as value, school_name as label' )
->fetchAll();
}
// Send it back to the client
echo json_encode( $out );
[/code]
I am using this json like this
[code]
"fnInitComplete": function ( settings, json ) {
editor.field('schools.id').update( json.schoolsList );
}
[/code]
It works well initially, but even after changes made by editor and db values changed, the option values and json values does not reflect these changes. Until the whole page is reloaded.
Let me know your suggestion please.

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    You need to do an `update` after each edit and create. You'll need to send the schoolsList information back with all requests, not just the non-action ones, and then update the available schools list using the callbacks / events in Editor in addition to fnInitComplete .

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Hi Allan,
    As you told i got back school list information after edit action. I was trying to update using in side editor events. but it gives a null out put.
    [code]
    "events": { "onInitEdit": function ( settings, json ) {editor.field('schools.id').update( json.schoolsList);
    [/code]

    When I tried to use it like this, it says that json is undefined.

    [code]
    editor.on('onInitCreate onInitEdit', function ( settings, json ) {editor.field('schools.id').update( json.schoolsList);

    [/code]

    Please let me know where I am going wrong.
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Hi allan,
    Or else, is there any function simlar to "fnInitComplete": to be used in .dataTable.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    > Or else, is there any function simlar to "fnInitComplete": to be used in .dataTable.

    fnInitComplete is a DataTable init function, so I don't understand why you would want a similar one?

    I don't know what is going wrong here, because I still don't really understand what you are trying to do. Please link to a test case with clear steps on how to reproduce the issue.

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Hi allan,
    [quote]allan said: still don't really understand what you are trying to do.[/quote]
    I am trying to change the options for a editor field using the json data. It works well when the data table is initialised. But after creating/editing/deleting the the field options should change according to new value of database. Hence json created accordingly and sending the schoolsList information back with all requests. But i m not able to update field with new json data using the above methods.
    Hope you have a solution for it, Thanking you.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Okay, so the first question is, do you have the new data set that you need to use to update the select lists? If not, you will need to make an Ajax call to the server to get it, or perhaps return it as part of the Ajax call for the create / edit actions as well.

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    edited March 2013
    Hi Allan,

    I have new data set to update as part of ajax call after each create /edit/delete actions
    [code]
    $out =$editor
    ->process($_POST)
    ->data();
    $out['schoolsList'] = $db
    ->select( 'schools', 'id as value, school_name as label' )
    ->fetchAll();
    echo json_encode( $out );

    [/code]
    but i could not use this new data set in editor instance.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    How did you try to use it? onPostEdit and onPostCreate would seem to be ideal: http://editor.datatables.net/docs/current/Editor.html#onPostEdit

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    [code]
    editor.on('onPostEdit', function ( settings, json ) {editor.field('schools.id').update( json.schoolsList);})
    [/code]
    should work, right? any modification required?
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Looks correct to me, as long as `json` as a `schoolsList` property.

    Allan
This discussion has been closed.