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 historic data?')) {
e.preventDefault();
return false;
}
var self = this, btn = this.closest(':button'), grid = this.getGridField(),
form = this.closest('form'), data = form.find(':input.gridstate').serialize();
data += "&" + encodeURIComponent(btn.attr('name')) + '=' + encodeURIComponent(btn.val());
if(window.location.search) {
data = window.location.search.replace(/^\?/, '') + '&' + data;
}
var connector = grid.data('url').indexOf('?') == -1 ? '?' : '&';
window.location.href = $.path.makeUrlAbsolute(
grid.data('url') + connector + data,
$('base').attr('href')
);
return false;
}
});
});
}(jQuery));
f) That should be enough to get you started ....
Cheers
Martin
Posted to: SOLVED 3.1 GridField_FormAction confirmation before delete | Show Thread | Post Reply