How do I have DataTables still redraw it's table after canceling on preCreate?

How do I have DataTables still redraw it's table after canceling on preCreate?

WIRTIJBWIRTIJB Posts: 3Questions: 1Answers: 0

Hello all,

I'm currently using a combination of DataTables and CodeIgniter to create a system that will allow the end user to manipulate the information in their database. Since I already have code for saving many hidden fields and code for saving a user change log record in my models, I am opting to make database edits through the models rather than DataTables and cancel the preCreate/preEdit/preRemove events.

It's working beautifully, but I haven't yet figured out how to have DataTables redraw the table with the changes made by the models, after I've cancelled these pre Events. From the examples I've seen, I was thinking I may need to run row.invalidate() or fnDraw() somewhere, but I'm not sure where. Around line 5604 of dataTables.editor.js v1.6.5? Any help would be appreciated.

This is the php I'm using to create the new record with the model. It is necessary to cancel so I don't get duplicate records.

        $editor->on( 'preCreate', function ( $editor, $values ) {
                //load and save from model to take advantage of pre-existing functionality
                //auto saves created_by, created_date, and sort_idx fields and archives change log record
                $data = $values['lookup_values'];
            $lookup = new Lookup_Value();
            $lookup->fromArray($data);
            $lookup->save();
            return false;
                
        } );

This is the php I'm using to edit existing records. I'm using rowReorder and skip the model if all we're doing is changing the sort order. Right now, I don't have it cancelling the event because I like all of the extra visuals during inline editing. (i.e. edited rows glow yellow, and are refreshed with the updated data.) I would prefer to be DRY and cancel the event but still have the inline visuals and updating of the client side data.

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

                   //first check if we are only reordering the fields
                   $data = $values['lookup_values'];
                   if(isset($data['sort_idx']) && count($data==1)){
                       //skip model archiving
                   } else {
                       //load and save from model to take advantage of pre-existing functionality
                       //auto saves updated_by and updated_date fields and archives change log record
                    $lookup = new Lookup_Value($id);
                    $lookup->fromArray($data);
                    $lookup->save();
                    // don't return false so we get the same visual when editing a row inline.
                    // return false;
                   }
                
            } );

Finally my php for deleting existing records. I need to be able to cancel this event because the model's delete function will check the table for an is_deleted field and change that rather than actually deleting the row. I have records where is_deleted=1 filtered out of the overall Editor instance and need the table re drawn with those filtered out.

        $editor->on( 'preRemove', function ( $editor, $id, $values ) {
                //load and delete from model to take advantage of pre-existing functionality
                    //auto archives change log records
                $lookup = new Lookup_Value($id);
                $lookup->delete();
                
                //prevent editor from deleting the record since we already did with the model
                return false;
            } );

Thanks in advance for the assistance.

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    If you are using different code to update the values, rather than the Editor PHP class, what I would suggest doing is just marking every field as not settable - e.g.

    Field::inst( 'myField' )-set( false )
    

    If no fields are settable, Editor won't ever write to the database itself - it will just read from it.

    Regards,
    Allan

  • WIRTIJBWIRTIJB Posts: 3Questions: 1Answers: 0
    edited September 2017

    That would work for everything except the sort_idx which I would still like to be settable with rowReorder. It still causes duplicate records on insert if that one field is editable.

    What about in the editor javascript? With some experimentation I've found that the php $editor->on( 'preCreate' ) is called first and then the jquery editor.on( 'preCreate') can be called. If I allow the php to return, it will trigger the jquery where I can easily reload the table, but can I then cancel the event from there?

    Could the following work?

          //php
          $editor->on( 'preCreate', function ( $editor, $values ) {
                   //save with the model
                    $data = $values['lookup_values'];
                $lookup = new Lookup_Value();
                $lookup->fromArray($data);
                $lookup->save();
                   //don't cancel the event here
          });
    
           //jquery
           editor.on( 'preCreate', function (e, mode, action) {
                //reload the table
                table.ajax.reload( null, false );
                 //cancel the event from here?
            } );
    
    
  • WIRTIJBWIRTIJB Posts: 3Questions: 1Answers: 0

    Success! I looked at which of the jquery editor events were triggered even when the preCreate in the php was cancelled and found both the submitSuccess and submitComplete firing. I added the table reload to the submitComplete and it's refreshing the table for me.

    I may tweak it down the road, but my immediate issue is fixed. Thanks for the reply, though. I'm enjoying learning my way around DataTables. It's a great library.

    //php
    $editor->on( 'preCreate', function ( $editor, $values ) {
             //save with the model
              $data = $values['lookup_values'];
              $lookup = new Lookup_Value();
              $lookup->fromArray($data);
              $lookup->save();
             //cancel the event here
             return false;
    });
     
     //jquery
     editor.on( 'submitComplete', function (e, mode, action) {
          //reload the table here
          table.ajax.reload( null, false );
      } );
    
    
This discussion has been closed.