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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Adding custom action in view page of modeladmin


Go to End


2 Posts   3330 Views

Avatar
lozhowlett

Community Member, 151 Posts

11 April 2014 at 11:33pm

I need to extend the grid field list page with a custom button.

Please see attached image, I want to add a "Delete All" records button in my ModelAdmin extension of products, that I can control by VendorID (A product has a vendor). So that when vendors are logged in they can select delete all and upload a new set of products.

I cant use replace data on the import CSV, becuase it will replace all vendors. So i have disabled that and instead want to create a custom button that will only delete their products.

Thanks!

Attached Files
Avatar
Phat

Community Member, 8 Posts

7 May 2014 at 12:43pm

First, you need to create a custom GridFieldComponent for 'Delete All' button - something like below

class GridFieldDeleteAllButton implements GridField_HTMLProvider, GridField_ActionProvider {
	protected $targetFragment;

	public function __construct($targetFragment = "before") {
		$this->targetFragment = $targetFragment;
	}

	public function getHTMLFragments($gridField) {
		$button = new GridField_FormAction(
			$gridField, 
			'deleteall', 
			_t('TableListField.DELETEALL', 'Delete All'),
			'deleteall', 
			null
		);
		$button->setAttribute('data-icon', 'cross-circle');
		$button->addExtraClass('no-ajax');
		return array(
			$this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>',
		);
	}

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

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

	public function handleDeleteAll($gridField, $request = null) {
		//Add your own DELETE logic here !
		$items=$gridField->getList();
		foreach ($items as $item) {
			$item->delete();
		}
		Controller::curr()->redirectBack();
	}
}

You then can add this new component to your gridfield under your ModelAdmin extension

    public function updateEditForm(&$form) {
        $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->owner->modelClass));
        $gridField->getConfig()->addComponent(new GridFieldDeleteAllButton('before'));        
    }