简体   繁体   English

symfony2将标签属性添加到多选列表

[英]symfony2 add label attribute to multiple choice list

I want to add different attribute to difference choice list. 我想向差异选择列表添加不同的属性。

->add('list', 'choice_single_select', [
            'choices' => ['gender', 'female', 'newborn', 'oldborn'],
            'required' => false,
            'expanded' => true,
            'multiple' => true,
            'label_attr' => ['class' =>  'listClass'],
            'choice_attr' => function($val, $key, $index) {
                return [
                    'data-ng-click' => 'form.list = (form.list != '.$val.') ? '.$val." : '';",
                    'data-ng-show' => 'aggr.count_'.$val.'.count'
                ];
            },
        ])

I want to set ng-show="gender2" to gender and ng-show="female4" to female label 我想将ng-show="gender2"为性别,并将ng-show="female4"为女性标签

There is way to add attribute per choice_attr to the input fields, but not for label. 有一种方法可以将每个choice_attr的属性添加到输入字段,但不能为标签添加属性。 So I'm kind of confuse what would be easy way of adding it 所以我有点困惑添加它的简单方法

I have manage to solve this by using existing choice_attr with customising the twig form theme. 我已经设法通过使用现有的choice_attr和自定义树枝窗体主题来解决此问题。 It will loop through attributes and only allow title and ng-show to label attribute. 它将遍历属性,并且仅允许标题和ng-show标记属性。 (I have removed label_attr usage, but you can use it if you need it. (我已删除了label_attr用法,但是如果需要,可以使用它。

{% form_theme form _self %}

{% block checkbox_widget -%}
    {% set parent_label_class = parent_label_class|default('') %}

    <div class="checkbox">
        <label{% for attrname, attrvalue in attr %} {% if attrname in ['data-ng-show', 'title'] %}{{ attrname }}="{{ attrvalue }}"{%- endif -%}{% endfor %}>
            <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
            {{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}
        </label>
    </div>
{%- endblock checkbox_widget %}

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

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