Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Customize table fields with links (show actions) #169

Open
sentenza opened this issue Feb 23, 2016 · 1 comment
Open

Customize table fields with links (show actions) #169

sentenza opened this issue Feb 23, 2016 · 1 comment

Comments

@sentenza
Copy link

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.

@WickedSilver
Copy link
Contributor

WickedSilver commented Aug 11, 2016

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 %}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants