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.

Data Model Questions /

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

GridField: AddComponent button to existing GridField


Go to End


1207 Views

Avatar
ee

Community Member, 7 Posts

9 September 2016 at 4:20am

Edited: 09/09/2016 4:23am

I want to add a "Delete All" action button to the Userforms Submissions Gridfield. I have created the code for the button in another file.

On the page that extends UserDefinedForm I am trying to get the existing inherited Submissions gridfield and add another component (the button) to it:

class CompetitionFormPage extends UserDefinedForm  {

	static $db = array(
		'Details' => 'HTMLText'
	);

	public function getCMSFields() {
            $fields = parent::getCMSFields();

            $DetailsField = new HtmlEditorField('Details', 'Details');

            $fields->addFieldsToTab('Root.Main', array(
        	new HtmlEditorField('Details', 'Details')
             ));

        
             //get exisitng Submissions gridfield
            $gridField = $fields->fieldByName('Submissions');
            $gridField->getConfig()->addComponent(new GridFieldDeleteAllButton());

             return $fields;
	}


}

class CompetitionFormPage_Controller extends UserDefinedForm_Controller {

}

This results in a Fatal error: Call to a member function getConfig() on a non-object

For reference here is the GridFieldDeleteAllButton

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();
	}
}

Any help, much appreciated.