【问题标题】:Symfony Forms EntityType Select All using JSSymfony Forms EntityType 使用 JS 全选
【发布时间】:2021-08-09 18:45:11
【问题描述】:

在我的网页上,我正在使用带有 EntityType 类的 Symfony 表单。我想创建一个额外的按钮,单击时从该下拉列表中选择所有项目。这可能使用 JavaScript / JQuery 吗?简单地更改自动生成的 HTML 是行不通的。

FormType.php:

->add('item', EntityType::class, [
   'class' => Item::class,
   'choice_label' => function(Item $item) {
      return sprintf('%s', $item->getName());
    },
    'label' => 'Staff',
    'multiple' => true,

【问题讨论】:

    标签: javascript jquery symfony symfony-forms


    【解决方案1】:

    我认为这样的事情可以解决问题:

    <!-- Somewhere in your Twig file : -->
    <button id="check-all-options">Check all !</button>
    
    document.querySelector('#check-all-options').addEventListener('click', () => {
      // Select all the options of your <select> tag
      const options = document.querySelectorAll('#form_item option');
    
      // Loop on all the options and pass them to selected ✅
      options.forEach(option => option.selected = true);
    });
    

    如果有帮助请告诉我:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-02
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2021-11-08
      • 2021-10-24
      相关资源
      最近更新 更多