Last active
December 4, 2019 16:26
-
-
Save karser/b6e49602d12c9d3d58da to your computer and use it in GitHub Desktop.
APYDataGridBundle: Custom mass action with parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'APYDataGridBundle::blocks_js.jquery.html.twig' %} | |
{# --------------------------------------------------- grid_actions ------------------------------------------------- #} | |
{% block grid_actions %} | |
<div class="mass-actions"> | |
<span class="grid_massactions_helper"> | |
<a href="#" onclick="return {{ grid.hash }}_markVisible(true);">{{ 'Select visible'|trans }}</a> | | |
<a href="#" onclick="return {{ grid.hash }}_markVisible(false);">{{ 'Deselect visible'|trans }}</a> | | |
<a href="#" onclick="return {{ grid.hash }}_markAll(true);">{{ 'Select all'|trans }}</a> | | |
<a href="#" onclick="return {{ grid.hash }}_markAll(false);">{{ 'Deselect all'|trans }}</a> | |
<span class="mass-actions-selected" id="{{ grid.hash }}_mass_action_selected"></span> | |
</span> | |
{% spaceless %} | |
<div style="float:right;" class="grid_massactions"> | |
{{ 'Action'|trans }} | |
<input type="hidden" id="{{ grid.hash }}_mass_action_all" name="{{ grid.hash }}[{{ constant('APY\\DataGridBundle\\Grid\\Grid::REQUEST_QUERY_MASS_ACTION_ALL_KEYS_SELECTED') }}]" value="0"/> | |
<select id="{{ grid.hash }}_mass_action_select" name="{{ grid.hash }}[{{ constant('APY\\DataGridBundle\\Grid\\Grid::REQUEST_QUERY_MASS_ACTION') }}]"> | |
<option value="-1"></option> | |
{% for key, massAction in grid.massActions %} | |
<option value="{{ key }}">{{ massAction.title|trans }}</option> | |
{% endfor %} | |
</select> | |
{% for key, massAction in grid.massActions %} | |
{% if massAction.form is defined and massAction.hasForm %} | |
{% set form = massAction.getFormView %} | |
{% form_theme form '@YourBundle/Grid/fields.html.twig' %} | |
<span id="{{ grid.hash }}_mass_action_form_{{ key }}" style="display: none;"> | |
{{ form_widget(form) }} | |
</span> | |
{% endif %} | |
{% endfor %} | |
<input type="submit" value="{{ 'Submit Action'|trans }}"/> | |
</div> | |
{% endspaceless %} | |
</div> | |
{% endblock grid_actions %} | |
{% block grid_scripts %} | |
{{ parent() }} | |
<script type="text/javascript"> | |
{{ grid_scripts_mass_actions(grid) }} | |
</script> | |
{% endblock grid_scripts %} | |
{% block grid_scripts_mass_actions %} | |
var {{ grid.hash }}_mass_action_forms = {}; | |
{% for key, massAction in grid.massActions %}{% if massAction.form is defined and massAction.hasForm %} | |
{{ grid.hash }}_mass_action_forms["{{ key }}"] = document.getElementById("{{ grid.hash }}_mass_action_form_{{ key }}"); | |
{% endif %}{% endfor %} | |
document.addEventListener('DOMContentLoaded', function () { | |
var hideAll = function (show_key) { | |
for (var key in {{ grid.hash }}_mass_action_forms) { | |
if ({{ grid.hash }}_mass_action_forms.hasOwnProperty(key)) { | |
if (key === show_key) { | |
$({{ grid.hash }}_mass_action_forms[key]).show(); | |
} else { | |
$({{ grid.hash }}_mass_action_forms[key]).hide(); | |
} | |
} | |
} | |
}; | |
hideAll(); | |
$("#{{ grid.hash }}_mass_action_select").change(function (e) { | |
var key = $(this).val(); | |
hideAll(key); | |
}); | |
}); | |
{% endblock grid_scripts_mass_actions %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'form_div_layout.html.twig' %} | |
{% block form_widget_compound %} | |
{% spaceless %} | |
<span {{ block('widget_container_attributes') }}> | |
{% if form.parent is empty %} | |
{{ form_errors(form) }} | |
{% endif %} | |
{{ block('form_rows') }} | |
{{ form_rest(form) }} | |
</span> | |
{% endspaceless %} | |
{% endblock form_widget_compound %} | |
{% block form_row %} | |
{% spaceless %} | |
<span> | |
{{ form_label(form) }} | |
{{ form_errors(form) }} | |
{{ form_widget(form) }} | |
</span> | |
{% endspaceless %} | |
{% endblock form_row %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use APY\DataGridBundle\Grid\Action\MassAction; | |
use Symfony\Component\Form\FormInterface; | |
class FormMassAction extends MassAction | |
{ | |
/** @var FormInterface */ | |
protected $form; | |
public function getFormView() | |
{ | |
return $this->hasForm() ? $this->form->createView() : null; | |
} | |
public function hasForm() | |
{ | |
return is_object($this->form); | |
} | |
public function getForm() | |
{ | |
return $this->form; | |
} | |
public function setForm(FormInterface $form = null) | |
{ | |
$this->form = $form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$request = $this->container->get('request'); | |
$form = $this->container->get('form.factory')->createBuilder('form')->add('custom_param', 'text', ['required' => false])->getForm(); | |
$action = new FormMassAction('Form mass action', function($ids, $selectAll, $session, $parameters) use ($form, $request) { | |
$data = []; | |
if (is_object($form)) { | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$data = $form->getData(); | |
} | |
} | |
}); | |
$action->setForm($form); | |
$grid->addMassAction($action); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment