Update fields in Data Table after Editor php postCreate event

Update fields in Data Table after Editor php postCreate event

minobuminobu Posts: 23Questions: 8Answers: 2

Same question posted in Free community support. I need an answer or some direction here asap. Thanks.

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

Below are 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. I would prefer to use Editors PHP library if possible.

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();

This question has an accepted answers - jump to answer

Answers

  • minobuminobu Posts: 23Questions: 8Answers: 2

    Sorry, this ended up in Free support again. Not sure how to change the category,

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Allan answered on your other thread, so hopefully that's got you going,

    Colin

  • minobuminobu Posts: 23Questions: 8Answers: 2

    Yea he nailed it, thanks. Can probably delete this thread.

This discussion has been closed.