Update fields in Data Table after Editor php postCreate event

Update fields in Data Table after Editor php postCreate event

minobuminobu Posts: 20Questions: 7Answers: 1

Sorry, I am unable to link a test case at this time, if it is required i can provide a login via email.

Below is my dataTables Instance and custom postCreate function. This all works great in the back end. The problem is when the popup box disappears and the new row added, the fields that are updated using the update query do not update in the table. They appear empty. After a refresh, they appear as they should or after ajax.reload. Doing ajax reload is not really an option however as this is a large table and the extra load time would be annoying for the end-user. The image below shows the empty fields after the new row has been submitted.

Before I commit any more work to this I would like to determine the best way forward. Is there some simple existing way dataTables/Editor can handle this with its api? This would be the best solution but another way around it would be if i could create hidden fields in my form use getJSON to set the value with an onchange event. This way would cut out the need for the postCreate function.

So is it possible to do this using the code below? Or should I create hidden fields and do it all server-side with getJSON?

// DataTables PHP library and database connection
include( "../../php/lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;



// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'lease_units', 'id' )
    ->fields(
        Field::inst( 'lease_id' ),
        Field::inst( 'lease' ),
        Field::inst( 'row_sec' ),
        Field::inst( 'row_num' ),
        Field::inst( 'row' ),
        Field::inst( 'start_pos' ),
        Field::inst( 'end_pos' ),
        Field::inst( 'total_units' ),
        Field::inst( 'grade' ),
        Field::inst( 'grade_id' ),
        Field::inst( 'unit_type' ),
        Field::inst( 'unittype_id' ),
        Field::inst( 'batch_id' ),
        Field::inst( 'parent_batch' ),
        Field::inst( 'avg' ),
        Field::inst( 'description' ),
        Field::inst( 'extra6' ),
        Field::inst( 'farm' ),
          Field::inst( 'extra2' ),
                Field::inst( 'extra1' )
    )


    ->on('postCreate', function ($editor,$id, $values, $row ) {


        //select row number query
        $rownumber_result = $editor
            ->db()
            ->query( 'select' )
            ->get( 'number' )
            ->table( 'lease_rows' )
            ->where( 'id', $values['row'] )
            ->exec()
            ->fetchAll();

        $the_number = $rownumber_result[0]['number'];

        //gets lease name
        $leasename_result = $editor
            ->db()
            ->query( 'select' )
            ->get( 'name' )
            ->table( 'leases' )
            ->where( 'id', $values['lease_id'] )
            ->exec()
            ->fetchAll();

        $leasename = $leasename_result[0]['name'];




        //update query
        $editor->db()
            ->query('update', 'lease_units')
            ->set( 'lease_units.row_num', $the_number)
            ->set('lease_units.lease', $leasename)
            ->where('id', $id )
            ->exec();
    })


    ->process( $_POST )
    ->json();

Replies

  • allanallan Posts: 52,661Questions: 1Answers: 8,058 Site admin

    Hi,

    Thanks for your question. What to do here is to actually use the writeCreate event rather than postCreate.

    The sequence of execution that Editor uses is:

    • Write to the database
    • Trigger writeCreate
    • Get the data from the db, to show on the client-side
    • Trigger postCreate

    This is the code if you are interested.

    So if you want Editor to see your changes, use writeCreate. You'd use postCreate to modify the data that has been read from the database (if needed).

    Allan

  • minobuminobu Posts: 20Questions: 7Answers: 1

    Thanks, @allan for the explanation and the code example

    That fixed it. I knew I it had to be something simple I was missing, Cheers.

Sign In or Register to comment.