⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Add_action and the default edit action



HumbleMonk
  • profile picture
  • Member

Posted 19 April 2012 - 18:56 PM

I am trying to add a custom action button that will change the 'status' field of a row. This works as expected. However, as a side effect the edit form now also changes the 'status' field of the row (whether clicking 'cancel' or 'update').


Here is the code for a CRUD that shows only items that have a 'status' of 'red':

try{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');

$crud->where('status', 'red');
$crud->set_table('items');

//add custom action that will call the change the change_status function
$crud->add_action('Move', '', '','', array($this,'change_status'));

//stop status from showing up in the field lists
$crud->unset_fields('status');
$this->output($output);

}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}


Here is the change_status function that changes the status of the item from red to green and then goes to a new page that shows only green items.

 public function change_status($primary_key)
{
$update_status = array(
"status" => "green"
);
$this->db->where('id', $primary_key);
$this->db->update('items', $update_status);
return site_url('showitems/green');
}


When clicking the 'move' button the status of the item changes from red to green and the page changes to show all green items. However when clicking on the edit button if I press 'update' or 'cancel' (even without actually changing anything) and then go back to the list (the red list) I see that the item is no longer in the red list - its status has been changed to green and is now in the green list.

Is there any reason for this, or any workaround to prevent this behaviour from happening?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 19 April 2012 - 20:28 PM

Yes, this is happening because you use with a wrong way the add_action callback. It is not an action callback for the click operation. It is a callback that returns a different url that the default.

So for example try this simple thing: Put all your rows from the database (for example with phpmyadmin) to red and then go to the list page. There are all red right? Now just refresh the page? There are all green.

I hope I didn't make things more complicated for you.

HumbleMonk
  • profile picture
  • Member

Posted 19 April 2012 - 22:52 PM

Thank you, I feel silly now :)

I changed it to:

$crud->add_action('Move', '', 'showitems/green');


and handle the move when the page is loaded and now it works.