APYDataGridBundle
APYDataGridBundle copied to clipboard
Custom mass action with parameters
Hi there, For starters thanks for excellent bundle!
I have struggling with one thing for some time though.. What I want to accomplish is to create mass action that would consist of two select fields, one would allow you to choose which mass action to apply on grid data, and the second one which will allow to select parameters required by mass action. For example:
Action [assign to group action] [group # 1] Submit action
I could alter blocks.html.twig, but how to intercept parameters for mass action, and pass them to callback that handles particular mass action.
How can this be done?
I think that you can do it with a forward controller callback like AcmeHelloBundle:Hello:fancy
. Indeed the doc isn't full updated on this part.
You are in a controller so you have access to the request and normally you can get the Group #1
input from the submit action. And the first input assign to group action
will be your mass actions.
Do you understand my idea?
@Abhoryo Not fully i don't understand forward controller callback part...
You mean passing controller and his method as mass action callback? $callback = array($controllerInstance, 'handleMassaction'); $massAction = new MassAction($title, $callback, $confirm, $parameters, $role);
I build grids in services so I don't have access to controller. When I need to create grid, I execute from controller something like $this->get('some_entity.grid.builder')->build(); and get fully configured grid object.
It would be great if instead of callback i could just give mass action a route to forward to, after executing mass action, but i understand that it is not possible?
The MassAction object accept a callback (see PHP doc) or the name of an controller action (AcmeHelloBundle:Hello:fancy) like the forward function of symfony.
Bundle name: AcmeHelloBundle Controller Class: HelloController Action Method: fancyAction
so this is the same that define a route because a route is associated to a controler action.
@Abhoryo Yeah, I confirm this work - thanks. I didn't know that there is such a option, it is not in the docs. So this will allow me to run controller action that handles mass action, get access to container, request, session etc.
Now I have got yet one question, how is to best create this additional field with parameters for mass action, completely override mass action block and write there custom form? Adding field only? Manually searching in Request posted parameter field, seems to be not so elegant approach.
Like you want.
you can also use javascript to change the action url of the mass action form with the additionnal field.
It's good for you ?
Yeah, I created custom blocks.html.twig with overrided mass actions section, created additional form field for passing extra parameter to mass action callback - controller route. Next I pulled this parameter from Request inside controller action.
Nice.
This is my implementation of mass actions with custom form fields that depend on the action. https://gist.github.com/karser/b6e49602d12c9d3d58da This feature it is often useful, maybe it worth to include to the project.
First, can we avoid jquery? Second, does it work only with a callback or a controller works too ?
Then, I think we can merge this feature in the MassAction class but we need to put the form in a modal dialog.
A user select rows:
Select a mass action and submit the action:
A form dialog is displayed.
- APYDataGridBundle already uses jQuery. Nevertheless, we can no use it.
- A form handles data from request by Symfony way so doesn't matter is it a callback or a controller. Which modal component do you want to use? Pure JS, like this? http://jsmodal.com/ Btw, what the prototype tool did you use?
I use moqups. It's lighter than Balsamiq Mockups
- The bundle use Jquery only for ajax grid. So yes, I hope we can do this without jquery or other extern libraries. But jsmodal seems good.
- Ok, great
In fact, you could already use a form if you put the form object in the parameter of the massAction. You juste need to modiify the template.
a useful feature :+1:
@karser If you have time, propose a PR. We would like to merge to the project. https://github.com/APY/APYDataGridBundle/issues/328#issuecomment-41939142