Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

SOLVED 3.1 GridField_FormAction confirmation before delete


Reply


4 Posts   357 Views

Avatar
martbarr

Community Member, 53 Posts

2 March 2014 at 12:55am

Hi,

Have a new form action button which does a calculated delete, and I need an "are you sure" before running.
Is there an agreed way to do this ?

Thanks
Martin

Avatar
martbarr

Community Member, 53 Posts

2 March 2014 at 12:55pm

SOLVED - I added a MyModelAdmin init function which required some JS in which I copied an action function from gridfield,js and added the test there.

.... easy when you know how !!!!!

Avatar
camfindlay

Forum Moderator, 181 Posts

2 March 2014 at 1:14pm

Would be useful if you can show the actual code here too for others attempting the same :)

Also if you can can add [solved] to the initial forum post it help people find it while searching... just some friendly pointers to help fellow developers.

Avatar
martbarr

Community Member, 53 Posts

2 March 2014 at 9:51pm

Edited: 03/03/2014 10:19am

Ok sorry - thought a description would suffice....

Steps to implement this:-

a) get config and add component using an extension

<?php

class MyModelAdminExtension extends Extension {

   function updateEditForm(&$form)
      {
      $f = $form->Fields()->fieldByName('ComingSoonItem'); // this is the DataObject name managed in MyAdmin.php
      // as MyAdmin manages more than one oage I need to be sure am on right page
      // so add try to add component if field exists
      if ($f) $f->getConfig()->addComponent(new GridFieldBigDelButton('before'));

      }
}

b) then turn extension on in config.yaml

ModelAdmin:
extensions:
- MyModelAdminExtension

c) Write the new component added in a) - I started off with a copy of GridFieldExportgButton.php and amended the action handler
(Sorry can't give you the actual delete routine as it's sensitive ... but it does nothing clever - just deletes some rows in the gridfield

...
public function getHTMLFragments($gridField) {
      $button = new GridField_FormAction(
         $gridField, //grid on the form
         'DelHist', //name
         'Delete Historic', //title
         'delhistaction', //action name
         null
      );
      $button->setAttribute('data-icon', 'delhistoric');
// add my special class here
      $button->addExtraClass('deleteWithConfirm');
      return array(
         $this->targetFragment => '<p class="grid-delH-button">' . $button->Field() . '</p>',
      );
   }

   public function getActions($gridField) {
      return array('delhistaction');
   }

   public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
      if($actionName == 'delhistaction') {
         return $this->handleBigDelete($gridField);
      }
   }
...


d) Add an init function for js require to myadmin.php

<?php

class MyAdmin extends ModelAdmin {
private static $managed_models = array('ComingSoonItem','ServiceTypeItem');
private static $url_segment = 'comingsoon';
private static $menu_title = 'Manage coming Svcs';

public function init()
{
   parent::init();
   Requirements::javascript("themes/stmarts2014/js/ModelAdminExtra.js");
   
}

}

e) write the js file for d) - this is a copy of the no-ajax one from gridfield.js

// to add a confirm to delete button
(function($){
   
      $.entwine('ss', function($) {
      
      $('.action.deleteWithConfirm').entwine({
onclick: function(e){
            if(!confirm('Are you sure you want to delete ALL old records')) {
               e.preventDefault();
               return false;
            } else {
               this._super(e);
            }
         }      });

   });

}(jQuery));

f) That should be enough to get you started ....

Cheers
Martin