Jump to:

23371 Posts in 18161 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » SOLVED 3.1 GridField_FormAction confirmation before delete

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

Page: 1
Go to End
Author Topic: 259 Views
  • martbarr
    Avatar
    Community Member
    46 Posts

    SOLVED 3.1 GridField_FormAction confirmation before delete Link to this post

    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

  • martbarr
    Avatar
    Community Member
    46 Posts

    Re: SOLVED 3.1 GridField_FormAction confirmation before delete Link to this post

    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 !!!!!

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: SOLVED 3.1 GridField_FormAction confirmation before delete Link to this post

    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.

  • martbarr
    Avatar
    Community Member
    46 Posts

    Re: SOLVED 3.1 GridField_FormAction confirmation before delete Link to this post

    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

    259 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.