简体   繁体   English

如何在Sonata Admin中显示/隐藏某些行的操作

[英]How to show/hide an action for some rows in Sonata Admin

I am using Sonata Admin. 我正在使用Sonata Admin。 Is it possible to have an action that is only displayed for some of the rows, but not other rows, all in the same list view? 是否可以在同一列表视图中显示仅针对某些行而不是其他行显示的操作?

For example, suppose I have a User entity with an "active" field. 例如,假设我有一个具有“活动”字段的用户实体。 I would like to show the "remove" action only for inactive users, not for active users. 我想仅针对非活动用户显示“删除”操作,而不是针对活动用户。 How can I do that? 我怎样才能做到这一点?

Yes you can, in your configureListFields admin class action, when adding _action column on $listMapper, you should add template option for delete action, and then you can implement some logic in that template: 是的,您可以在configureListFields管理类操作中,在$ listMapper上添加_action列时,您应该为删除操作添加模板选项,然后您可以在该模板中实现一些逻辑:

$listMapper
    ...
    ->add('_action', 'actions', array(
        'actions' => array(
            'delete' => array(
                'template' => 'YourAdminBundle:List:list_delete_action.html.twig',
            ),
        ),
    ));

And then in list_delete_action.html.twig: 然后在list_delete_action.html.twig中:

{% if admin.isGranted('DELETE', object) and admin.hasRoute('delete') and not object.isActive %}
<a href="{{ admin.generateObjectUrl('delete', object) }}" class="btn btn-sm btn-default delete_link" title="{{ 'action_delete'|trans({}, 'SonataAdminBundle') }}">
    <i class="glyphicon glyphicon-remove"></i>
    {{ 'action_delete'|trans({}, 'SonataAdminBundle') }}
</a>
{% endif %}

Here in this template you will have object defined, which is entity for each list row, so you can use its getters to get something from you entity. 在此模板中,您将定义object ,即每个列表行的实体,因此您可以使用其getter从您的实体获取内容。 Note that condition in if statement and not object.isActive , so this is where you apply your additional if logic. 请注意if语句中的条件and not object.isActive ,因此这是您应用附加if逻辑的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM