AliDatatableBundle icon indicating copy to clipboard operation
AliDatatableBundle copied to clipboard

Customize table fields with links (show actions)

Open sentenza opened this issue 9 years ago • 1 comments

I need to customize the table rendering in order to add the proper actions to show the associate record. I think the best way is to use the custom renderer approach, but I need some help to effectively implement it.

sentenza avatar Feb 23 '16 17:02 sentenza

Warning: following code breaks searching functionality Currently working on fixing it (and see if it's worthwhile), let me know if a PR is wanted for this functionality

Update @ 18-01-2017: I have a complete fix for searching and a working version, if anyone is interested i could send the code or create a PR

Not sure if you still need anything for this but you can do this:

_datatable->setFields function in controller (replace the 'q' by whatever you bound your entity to):

$key    => 'q.entity',

_datatable->setRenderer function in controller:

->setRenderer(
                function(&$data) use ($controller_instance)
                {
                    foreach($data as $key => $value)
                    {
                        if ($key === 0)
                        {
                            $data[$key] = $controller_instance
                                ->get('templating')
                                ->render(
                                    'Main/Datatable/Renderers/entity.html.twig',
                                    array(
                                        'entity' => $value,
                                        'path_name' => 'entity_show_route'
                                    )
                                );
                        }
                    }
                }
            )

custom renderer twig:

{# app/Resources/views/Main/Datatable/Renderers/entity.html.twig #}
{% spaceless %}
    {% if entity is not defined %}
        missing parameters for rendering entity ({% if entity is not defined %}entity{% endif %})
    {% else %}
        {% if entity is defined and entity is not null and path_name is not defined %}
            {{ entity }} {# calls the __toString() function of entity #}
        {% elseif entity is defined and entity is not null %}
            <a href="{{ path(path_name, {'id': entity.id}) }}">{{ entity }}</a> {# calls the __toString() function of entity #}
        {% endif %}
    {% endif %}
{% endspaceless %}

WickedSilver avatar Aug 11 '16 15:08 WickedSilver