Master Detail View - Symfony Twig - symfony

I am a total beginner in Symfony. I would like to implement a master detail view in Symfony. So I have a table with user entries shown in a twig template. If a user clicks one of the rows, in the lower part of the page the users details should be shown. How is this best done with Symfony?
Should I use a fragment with hidden content if the parameter from controller for details is empty?
e.g. {% if .... }
Should I use javascript to call the controller action?
Thank you.

Related

Render forms in sonata admin listview

I my application i have a listview representing News-items. On each row end i want to add a plus-sign button and by clicking it, a little inline form should pop up in a bubble (via bootstrap dropdown). To render this form, my actually approach is to render this via the render(controller(...)) mechanism in the field template.
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{{ render(controller(...)) }}
{% endblock %}
The render controller call renders a form according to the official symfony documentation ...
The rendered form sends it data back to the commentAction in the same controller.
In theory it works fine, but practical its slow as hell. My site runs in timeout after 60 seconds just because in the listview are default 64 items shown and each item calls this render controller function ...
Is there a clean way to render a little extra form not by invoking a expensive render controller call?
I think it could work with manipulating the listAction method in the controller (injecting here the extra form?) ... but this does not seem a clean solution.
Any ideas?
Thanks
Edit:
Ok, i think i found out, where the bottleneck is ... the example above was simplified. In reality i load a collection into the form for every row ... in the form you can choose, which author created the news. There are at least 500 Authors in that list ... so displaying these authors for a single new, everything is fine. But rendering the choose options 64 times is too much. Is there a way to make this faster? Some caching mechanism?
I think in this case, instead of rendering all forms at once, just load the form via AJAX once user click on the button. Another solution could be to replace collection for some autocomplete somyou dont need to render all data at once.

Editable field action in sonata list view

I have a list view in sonata admin. I want to edit integer field directly in list view. New data for this field must change other fields in my entity. How can I add action function to this editable field in which I may do some actions with other fields depending on entering integer value?
Please check documentation: https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_custom_action.html
Basically, what you need is a custom route and and a controller, same way as you handle any page/request. Than you can do any logic you like making requests to this routes (even with AJAX). Documentation will help you the best with this.
And to add an "editable field" or a link or a button in List view, just create custom template piece: https://sonata-project.org/bundles/admin/master/doc/reference/templates.html

Passing current page url to control with Twig in Symfony

Hi I have a modal contact form on my site that is available on each page of my site. When I render the contact controller on each page I was trying to use this
{% render controller('CorporateSiteBundle:Forms:contactUs', { 'contactOpen': contactOpen is defined, route: app.request.requestUri}) %}
Then on my controller page from all my contact form I wanted to grab that 'route' value that I was sending over. This is what I used:
$request->query->get('route');
But the form just seems to fail when it submits. Can anyone help with where I'm going wrong? Thanks
you should try with
$request->get('contactOpen');

Custom message display page?

In an Asp.Net Mvc 4 site, there may be various messages needed be shown to end users (e.g. "Thank you. The document has been submitted.", "The item and its depended items have been deleted.", "Cannot find the row with ID of xxx.", "Cannot delete this row because it's xxx is depended on it.", ... etc).
How to (and what's the best approach) define a generic page accept a message and display it? The page should be able to be used by all the controllers in the site.
Added:
How about create a ShowMessage controller and view which accept Query string/ViewData/TempData and display it. Other controllers redirect the page if needed. Is this a good solution?
I would also recommend this:
toastr
It was created by John Papa, who frequents SO on a fairly regular basis.
You may want to store message in ViewBag/ViewData or TempData and render it to your particular view. You can create extension method in baseController class and hence it will be accessible from all the controller. Check out below blog post that discusses the same.
http://blogs.taiga.nl/martijn/2011/05/03/keep-your-users-informed-with-asp-net-mvc/
You could use a client side notification plugin such as jGrowl or noty.

Asp.Net MVC Not Duplicate forms when Edit/Add

When we have anything that requires user input (Eg adding a product to a database) the Edit screen looks the same as the add screen. When using MVC .Net how do you handle this? Do you return the same view? Just adjust the model to reflect the change?
Same (partial)view
You create only one but strong typed view (depending on the UI it can of course be a partial view as well). When adding new data return this view from controller action with default model object instance (usually just a new instance without any properties being set), but when you edit, return it with the object instance that you'd like to edit.
Controller part
Regarding controller actions you can have four of them:
Add GET
return View("SomeView", new Customer());
Add POST
Edit GET
return View("SomeView", new CustomerRepository().GetCustomer(id));
Edit POST
Bot GET actions return the same view but with different model as described earlier. POST actions both store submitted data, but return whatever they need to. Probably some RedirectToAction()...
You can use the same view for display and Edit, simply call it from your controller
return View("ViewName")
You could have the form fields in a partial view and have two separate views using the same partial view, one posting to the edit controller action method and the other posting to the add controller action method.
Partial views are used to remove duplicity. You could read an example of this in the Nerd Dinner tutorial.

Resources