symfony表单集合:如何确定allow_delete是否设置为true

时间:2015-01-02 20:23:13

标签: javascript forms symfony

我已经构建了一个javascript宏和一个表单主题,用symfony2在我的网站上呈现表单集。

{{如果定义了原型}},我添加了一个“添加”按钮'。 到目前为止,我还有一个删除按钮。

我想删除此删除按钮,如果' allow_delete'未设置为true但我无法弄清楚如何在树枝中找到它。

当我查看我的field.vars时,没有allow_delete选项。 field.vars.attr也没有。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

allow_delete选项是您表单字段的子项。

{% for widget in form.YOURFIELD.children %}
    {% if widget.get('allow_delete') %}
        //Do your stuff
    {% endif %}
{% endfor %}

答案 1 :(得分:0)

对于那些可能感兴趣的人,这是我的完整解决方案:

  • 集合呈现为表格。标题是标签或字符串 如果是复选框,则为“选项”。

  • 如果在表单中设置了属性data-label,则选项为
    取而代之的是data-label的值。

  • 如果allow_add设置为true,则添加添加按钮

  • 如果allow_delete设置为true,则添加删除按钮。

    {% block collection_widget %}
        {% spaceless %}
            <div class="collection">
                {% if prototype is defined %}
                    {% set attr = attr|merge({'data-prototype': block('collection_item_widget') }) %}
                {% endif %}
    
            {% if form.vars.allow_delete is defined and form.vars.allow_delete %}
                {% set allow_delete = true %}
            {% else %}
                {% set allow_delete = false %}
            {% endif %}
    
            <div {{ block('widget_container_attributes') }} class="protoype">
                {% if prototype is defined %}
                <a href="#" class="add_button btn btn-default btn-sm">Ajouter&nbsp;&nbsp;<i class="fa fa-plus"></i></a>
                {% endif %}
                {{ form_errors(form) }}
                <table class="subtable table">
                    <thead>
                        <tr class="headers" style="display: none;">
                            {% if form.children|length > 0 %}
                                {% if form.children[0]|length > 0 %}
                                    {% set header = form.children[0] %}
                                    {{ block('collection_header') }}
                                {% endif %}
                            {% endif %}
                        </tr>
                    </thead>
                    <tbody class="container_rows">
    
                    {% for rows in form %}
                    {% spaceless %}
                        {% if rows.children|length > 0 %}
                            <tr class="row_to_delete child_collection">
                                {% set body = rows %}
                                {{ block('collection_body') }}
                            </tr>
                        {% endif %}
                    {% endspaceless %}
                    {% endfor %}
    
                    </tbody>
                </table>
                {#{{ form_rest(form) }}#}
            </div>
        </div>
    {% endspaceless %}
    

    {%endblock collection_widget%}

    {%block collection_item_widget%}     {%set header = prototype%}     {%set body = prototype%}     {%spaceless%}                      {{block('collection_body')}}                  {%endspaceless%}     {%endblock collection_item_widget%}

    {% block collection_header %}
        {% for field in header %}
            <th>
                {% if 'checkbox' not in field.vars.block_prefixes %}
                    {{ form_label(field)|raw }}
                {% else %}
                    {% if field.vars.attr['data-label'] is defined %}
                        {{ field.vars.attr['data-label'] }}
                    {% else %}
                        Options
                    {% endif %}
                {% endif %}
            </th>
            {% endfor %}
            {% if allow_delete %}
                <th class="fmu_table_center">Supprimer</th>
            {% endif %}
        {% endblock %}
    
        {% block collection_body %}
    
        {% set fieldNum = 1 %}
        {% for field in body %}
            <td class="field{{ fieldNum }} data-label">
                {% if field.vars.attr['class'] is defined %}
                    {% set class = field.vars.attr['class'] ~ ' input-sm' %}
                {% else %}
                    {% set class = 'input-sm' %}
                {% endif %}
                {% set attr = field.vars.attr|merge({'class': class }) %}
                {{ form_widget(field, {'attr' : attr}) }}
                {{ form_errors(field) }}
            </td>
            {% set fieldNum = fieldNum + 1 %}
        {% endfor %}
        {% if allow_delete %}
            <td class="fmu_table_center fmu_table_middle"><a href="#" class="fmu_delete_button"><i class="fa fa-times"></i></a></td>
        {% endif %}
    {% endblock %}
    

    和我的javascript:

    {% macro javascript_form_collection() %}
        {#ass the necessary javascript to handle a collection#}
        <script>
            $(function() {
                $('.collection').each(function(){
    
                    var $container = $(this).children('div:first-child');
    
                    var $addButton = $container.children('.add_button').eq(0);
    
                    $addButton.on('click', function(e) {
                        addElement($container,index);
                        count++;
                        index++;
                        showHeaders($container, count);
                        e.preventDefault();
                        return false;
                    });
    
                    var count = $container.find('.child_collection').length;
                    var index = $container.find('.child_collection').length;
    
                    if (count == 0)
                    {
                        populateHeaders($container)
                    }
    
                    $container.on('click','.fmu_delete_button', function(e){
                        e.preventDefault();
                        var $row = $(this).closest('.row_to_delete');
                        $row.next('.sibling_row_to_delete').remove();
                        $row.next('.sibling_row_to_delete_2').remove();
    
                        $row.remove();
                        count--;
                        showHeaders($container, count);
                    });
    
                    showHeaders($container, count);
    
                });
    
                function populateHeaders($container)
                {
                    var $headers = $container.find('.headers').eq(0);
                    var $prototype = $($container.attr('data-prototype'));
                    var $headerPrototype = $($prototype[0]).attr('data-label');
    
                    $headers.html($headerPrototype);
    
                }
    
                function showHeaders($container, count)
                {
                    if (count > 0)
                    {
                        $container.find('.subtable .headers').show();
                    }
                    else
                    {
                        $container.find('.subtable .headers').hide();
                    }
                }
    
                function addElement($container, index) {
                    var $prototype = $($container.attr('data-prototype')
                            .replace(/__name__label__/g, 'n°' + (index+1))
                            .replace(/__name__/g, index));
                    $container.find('.container_rows').eq(0).append($prototype);
                    $('.timepicker').timepicker();
                    $('.datetimepicker').datetimepicker();
                    $prototype.find('.select2').each(function(){
                        $(this).select2();
                    });
                    $('[data-toggle="tooltip"]').tooltip();
                    return $prototype;
                }
    
            });
        </script>
    
    
        <style type="text/css">
    
            .subtable {
                width: 100%;
            }
    
            .subtable th {
                font-weight: normal;
            }
    
        </style>
    {% endmacro %}