Editor not refreshing properly

Editor not refreshing properly

jimik1979jimik1979 Posts: 7Questions: 3Answers: 1

Hi,

I have an editor instance that correctly changes the data in the database, but doesn't refresh the datatable on the page correctly.
The initial page load calls all the rows of data from the php ajax file (which also displays all of the changes that had been made before the page was refreshed), but when I click on the 'update' button of the editor modal only the first row the select statement gave initially and not the row that has been edited is passed back to the browser. I'm not sure why that should be the case and would like suggestions as to where I've gone wrong.

The js for the datatable is

editor2 = new $.fn.dataTable.Editor( {
    ajax: "../../includes/ajax/load_drivers.php",
    table: "#driver-list",
    fields: [{
      label: "Sub Account",
      name: "driver_fleet_level.fleetLevelID",
      type: "select",
      placeholder: "Please select a sub account"
    }, {
      label: "driver ID",
      name: "driver_fleet_level.driverFleetLevelID",
      type: "hidden"
    }]
  });
    
  $('#driver-list').DataTable( {
    dom: "Bfrtip",
    ajax: {
      url: "../../includes/ajax/load_drivers.php",
      type: "POST",
      data: function ( d ) {
        return $('#hidden-data-form').serialize()
      },
    },
    columns: [
      { data: "user.firstName" },
      { data: "user.lastName" },
      { data: "fleet_level.name" },
      { 
        data: null,
        defaultContent : '<a href="" class="editor2_edit">Edit</a>'
      },
      
    ],
    select: true,
    buttons: [
      { extend: "edit", editor: editor2 }
    ],
    columnDefs: [
      {
        orderable: false,
        targets: 3
      }
    ]
  });
  
  $('#driver-list').on('click', 'a.editor2_edit', function (e) {
    e.preventDefault();
    editor2.edit( $(this).closest('tr'), {
      title: 'Edit entry',
      buttons: 'Update'
    });
  });

and the ajax file is:

  $data = Editor::inst( $db, 'driver_fleet_level', 'driver_fleet_level.driverFleetLevelID' )
  ->fields(
    Field::inst( 'driver_fleet_level.userID' ),
    Field::inst( 'driver_fleet_level.driverFleetLevelID' ),
    Field::inst( 'user.firstName' ),
    Field::inst( 'user.lastName' ),
    Field::inst( 'driver_fleet_level.fleetLevelID' )
      ->options( function () use ($db) {
         ...
        }
      ),
    Field::inst( 'fleet_level.name' ),
    Field::inst( 'fleet_level.fleetID')
  )
  
  ->leftJoin( 'user', 'user.userID', '=', 'driver_fleet_level.userID' )
  ->leftJoin( 'fleet_level', 'driver_fleet_level.fleetLevelID', '=', 'fleet_level.fleetLevelID' )
  
  ->where( 'fleet_level.fleetID', $_SESSION['fleet_id'] )
  ->where( 'user.firstName', 'No Default', '!=')
  ->where( function ( $q ) use ( $fleet ) {
     
      if(isset($_POST['flLevID'])) {
        $fleet_level_id = $_POST['flLevID'];
        $_SESSION['flLevID'] = $fleet_level_id;
      } else {
        $fleet_level_id = $_SESSION['flLevID'];
      }
        
      if ($fleet_level_id != 0) { // fleet level specified in POST
        $q->where('driver_fleet_level.fleetLevelID', $fleet_level_id, '=');

        $sub_levels = array ();
        $fleet->all_sub_levels($fleet_level_id, $sub_levels);
        
        foreach ($sub_levels as $sub_level) { // get all the sub levels
          $q->or_where('driver_fleet_level.fleetLevelID', $sub_level, '=');
        }
        
      } else { // no fleet level specified - get all fleet levels
        $q->where('driver_fleet_level.fleetLevelID', '[0-9]+', 'REGEXP');
      }
      
  })

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

  // allow late additions to the data JSON object, eg error messages
  
  echo json_encode($data);

Thanks for your help.

This question has an accepted answers - jump to answer

Answers

  • jimik1979jimik1979 Posts: 7Questions: 3Answers: 1
    Answer ✓

    I think I've figured it out for myself, it appears that I wasn't passing the primary key properly. Tweaking the code to do this has resolved the issue

  • allanallan Posts: 64,331Questions: 1Answers: 10,623 Site admin

    Thanks for posting back. Great to hear you've got it figured out :smile:.

    Allan

This discussion has been closed.