Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将全选选项添加到symfony中的复选框列表中_Php_Symfony_Checkbox_Forms - Fatal编程技术网

Php 将全选选项添加到symfony中的复选框列表中

Php 将全选选项添加到symfony中的复选框列表中,php,symfony,checkbox,forms,Php,Symfony,Checkbox,Forms,我对web开发和html表单比较陌生。在我的webapp中,我有一个列表(gpstracks),每个列表条目都有一个复选框,用户可以编辑、删除、下载等等。。。一次选中所有曲目。为了提高可用性,我想添加一个“全选”按钮或复选框,自动选中表单中的所有其他复选框(最好不用重新加载整个表单) 有没有可能这样做?我一直在尝试使用 $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($t

我对web开发和html表单比较陌生。在我的webapp中,我有一个列表(gpstracks),每个列表条目都有一个复选框,用户可以编辑、删除、下载等等。。。一次选中所有曲目。为了提高可用性,我想添加一个“全选”按钮或复选框,自动选中表单中的所有其他复选框(最好不用重新加载整个表单)

有没有可能这样做?我一直在尝试使用

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($tracks){

        $form = $event->getForm();

        foreach($tracks as $track)
        {
            $form->get($track->getId())->setData(array('checked'=>true));
        }
    }
与第二个“提交”按钮结合使用,该按钮的类型为“全部选择”。显然,这会重新加载整个表单。但是在重新加载之后,所有复选框都保持未选中状态,因此setData方法似乎没有任何效果

是否有任何选项可以通过编程方式选中表单中的复选框,最好不用重新加载整个表单

  • 为复选框设置html类(在表单生成器中设置选项attr['class'])
  • 使用其他类添加按钮
  • 单击按钮2添加要捕获的事件,并选中复选框
  • 使用jquery:

    // cerad.js
    Cerad = {};
    Cerad.checkboxAll = function(e)
    {   
        var nameRoot = $(this).attr('name'); // "refSchedSearchData[ages][]";
    
        nameRoot = nameRoot.substring(0,nameRoot.lastIndexOf('['));
    
        var group = 'input[type=checkbox][name^="' + nameRoot + '"]';
    
        var checked = $(this).prop('checked') ? true : false;
    
        $(group).prop('checked', checked);
    };
    
    {# searchform.html.twig #}
    {# form.dates is an array of form check boxes #}
    {% if form.dates is defined %}
      {% set items = form.dates %}
      <td>{% include '@CeradGame/Project/Schedule/Twig/ScheduleSearchCheckboxes.html.twig' %}</td>
    {% endif %}
    
    {# ScheduleSearchCheckboxes.html.twig #}
    {# render one set of check boxes as a table #}
    {# Setting a class on the first item #}
    <table border="1">
      <tr><th colspan="30">{{ items.vars.label }}</th></tr>
      <tr>
        {% set itemFirst = true %}
        {% for item in items %}
          <td align="center">{{ form_label(item) }}<br />
          {% if itemFirst %}
            {{ form_widget(item, { 'attr': {'class': 'cerad-checkbox-all' }}) }}
            {% set itemFirst = false %}
          {% else %}
            {{ form_widget(item) }}
          {% endif %}
          </td>
        {% endfor %}
      <tr>
    </table>
    
    // Grab all the cerad-checkbox-all elements and pass to Cerad.checkboxAll
    {% block javascripts %}
      <script type="text/javascript">
        $(document).ready(function()
        {
          // checkbox all functionality
          $('.cerad-checkbox-all').change(Cerad.checkboxAll);
        });
      </script>
    {% endblock %}
    
    //cerad.js
    Cerad={};
    Cerad.checkboxAll=功能(e)
    {   
    var nameRoot=$(this.attr('name');/“refSchedSearchData[ages][]”;
    nameRoot=nameRoot.substring(0,nameRoot.lastIndexOf('['));
    变量组='输入[type=checkbox][name^=“”+nameRoot+'”];
    var checked=$(this).prop('checked')?true:false;
    $(组).prop('checked',checked);
    };
    {#searchform.html.twig}
    {#form.dates是一个表单复选框数组#}
    {%如果定义了form.dates%}
    {%set items=form.dates%}
    {%include'@CeradGame/Project/Schedule/Twig/schedulesearchcheckbox.html.Twig%}
    {%endif%}
    {#ScheduleSearchCheckbox.html.twig}
    {#将一组复选框呈现为表#}
    {#在第一项上设置类#}
    {{items.vars.label}
    {%set itemFirst=true%}
    {items%%中的项的%s}
    {{form_label(item)}}
    {%if itemFirst%} {{form_小部件(项,{'attr':{'class':'cerad checkbox all'}}}} {%set itemFirst=false%} {%else%} {{form_widget(item)} {%endif%} {%endfor%} //抓取所有cerad复选框所有元素并传递给cerad.checkboxAll {%block javascripts%} $(文档).ready(函数() { //复选框所有功能 $('.cerad checkbox all').change(cerad.checkbox all); }); {%endblock%}
    这将是您需要的javascript(jquery)例如。我很害怕……从简单的javascript开始值得吗?或者jquerymake让一切变得更简单了吗?我不再写太多javascript了,我使用像jQuery这样的库,这让生活变得更简单了。如果你不介意重新加载页面,你就不需要使用javascript。这很好知道……在某一点上,这是真的nably易于使用/美观的前端应该添加到项目中,我想我将开始研究jquery。使用javascript/jquery,确实没有问题!感谢您的帮助