Skip to content

Latest commit

 

History

History
150 lines (111 loc) · 3.34 KB

group-action.md

File metadata and controls

150 lines (111 loc) · 3.34 KB

Table of contents

Group action

Api

If you need to do some operations with multiple rows, there are group actions. There are zero, one or two level group actions.

Zero level

Group button action

When you want to show just one action button, do simply that:

$grid->addGroupButtonAction('Say hello')->onClick[] = [$this, 'sayHello'];

One level

Group action 1

$grid->addGroupAction('Delete examples')->onSelect[] = [$this, 'deleteExamples'];
$grid->addGroupAction('Something alse')->onSelect[] = [$this, 'doSomethingElse'];

This will create one select box (['Delete examples', 'Something alse']) and submit button. If you submit that form, your handler will be called. It will be called via ajax.

This is how your handler can look like:

public function deleteExamples(array $ids): void
{
	// Your code...

	if ($this->isAjax()) {
		$this['groupActionsGrid']->reload();
	} else {
		$this->redirect('this');
	}
}

Second level

Group action 2

There is also the two-level possibility of group action:

$grid->addGroupAction('Change order status', [
	1 => 'Received',
	2 => 'Ready',
	3 => 'Processing',
	4 => 'Sent',
	5 => 'Storno'
])->onSelect[] = [$this, 'groupChangeStatus'];

$grid->addGroupAction('Send', [
	'john'  => 'John',
	'joe'   => 'Joe',
	'frank' => 'Franta',
])->onSelect[] = [$this, 'groupSend'];

And some example handler:

public function groupChangeStatus(array $ids, $status): void
{
	$this->flashMessage(
		sprintf("Status of items with id: [%s] was changed to: [$status]", implode(',', $ids)),
		'success'
	);

	$this->exampleRepository->updateStatus($ids, $status);

	if ($this->isAjax()) {
		$this->redrawControl('flashes');
		$this['groupActionsGrid']->reload();
	} else {
		$this->redirect('this');
	}
}

Text input

Group action can also containe a text input instad of select (As show in example above - option called "Add note"). Example code:

$grid->addGroupTextAction('Add note')
	->onSelect[] = [$this, 'addNote'];

And the ::addNote() method:

public function addNote(array $ids, $value): void
{
	$this->flashMessage(
		sprintf('Note [%s] was added to items with ID: [%s]', $value, implode(',', $ids),
		'success'
	);

	if ($this->isAjax()) {
		$this->redrawControl('flashes');
		$this['groupActionsGrid']->reload();
	} else {
		$this->redirect('this');
	}
}

Textarea

User may also use a textarea:

$grid->addGroupTextareaAction('aaaa');

Attributes, classes

All group action inputs have optional class or other attributes:

$grid->addGroupTextareaAction('aaaa')
	->setAttribute('rows', 10)
	->setClass('fooo');

Happy inputs

DataGrid uses tiny library happy for those nice checkboxes. You can disable them:

$grid->useHappyComponents(false);