【问题标题】:Display a switch by a flag通过标志显示开关
【发布时间】:2020-02-26 07:30:15
【问题描述】:

如果我的数据库中的标志设置为正值,我需要显示一个开关。

<div class="form-group m-form__group row vertical-padding10">
                            <label class="col-form-label col-lg-2 col-sm-12">Switch</label>
                            <input id="switch" name="switch" data-switch="true" data-size="small" data-on-color="success"  type="checkbox" checked>   
</div>

如何执行查询并在我的视图中显示此开关?这是一个好习惯吗?

【问题讨论】:

    标签: javascript jquery html symfony twig


    【解决方案1】:

    在您的控制器中,您创建变量$displaySwitch 来指示具有真值的布尔值是否存在并将其传递给模板:

    $em = $this->getDoctrine()->getManager();
    $displaySwitch = $em->getRepository(<your entity class>)->findBy(['<your bool field>' => true]);
    return $this->render('<your template>',['displaySwitch' => $displaySwitch]);
    

    在模板中添加条件:如果没有找到为真的值,则不显示开关:

    {% if displaySwitch %}
    <div class="form-group m-form__group row vertical-padding10">
        <label class="col-form-label col-lg-2 col-sm-12">Switch</label>
        <input id="switch" name="switch" data-switch="true" data-size="small" data-on-color="success"  type="checkbox" checked>
    </div>
    {% endif %}
    

    【讨论】:

      【解决方案2】:

      你需要添加:

      {% if value >= 0 %}
          put your input here
      {% endif %}
      

      【讨论】:

      • 我需要在我的视图中执行一个查询,这就是问题
      • 你不能在 twig 文件中这样做
      • 你可以在视图中这样做,但你不应该
      • 你可以使用JS来显示你的价值检查这个链接stackoverflow.com/questions/10298737/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多