【问题标题】:Pre-check checkboxes in laravel based on data object基于数据对象的laravel中的预检查复选框
【发布时间】:2020-03-14 09:09:46
【问题描述】:

在 laravel 中,我有一个围绕包含名称、上下文和类型值的数据对象的 foreach 循环。在我的循环中,我用 2 种不同类型的复选框显示名称的行。一个是上下文复选框,另一个是类型复选框。

我需要将这些复选框中提交的值添加到 vue 组件中,但在执行此操作之前,我需要预先检查条件。

如果我的对象包含 Internal 上下文的值,那么我需要选中该复选框,如果它的类型为 Old,那么我需要选中该复选框。

我怎样才能做到这一点,以便它正确地预先检查detail->context与上下文复选框值匹配且类型相同的任何复选框

@foreach($details as $detail)
<h2 slot="header">{{$detail->name}}</h2>
<div slot="context" >
    <h4>Context</h4>   
    <hr>
    <input type="checkbox" value='direct'   ref="existingContextNames" data-md-icheck  />
    <label>Direct</label><br>
    <input type="checkbox"  value='internal'  ref="existingContextNames" data-md-icheck  />
    <label>Internal</label><br>
    <input type="checkbox"  value='customer'  ref="existingContextNames" data-md-icheck  />
    <label>Customer</label><br>
    <input type="checkbox"  value='third party'  ref="existingContextNames" data-md-icheck  />
    <label>3rd Party</label><br>
</div>

<div slot="type" >
    <h4>Type</h4>   
    <hr>
    <input type="checkbox"  value='new' ref="existingTypeNames" data-md-icheck  />
    <label>New</label><br>
    <input type="checkbox"  value='old' ref="existingTypeNames" data-md-icheck  />
    <label>Old</label><br>
    <input type="checkbox"  value='mix' ref="existingTypeNames" data-md-icheck  />
    <label>Mix</label><br>
</div>
@endforeach

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    您应该能够使用内联三元运算符回显选中的标记属性:

    <input type="checkbox" {{  $detail->context === "thing_to_compare" ? "checked" : "" }} >
    

    给你

    <input type="checkbox" checked >
    

    如果$detail-&gt;context === "thing_to_compare" 解析为真

    【讨论】:

    • 但是我可以保持我的价值吗?
    • 这确实有道理,但内联代码破坏了我的 html
    • 嗯,它返回什么 html?原版 PHP 标签也可以像 &lt;?= ($detail-&gt;context === "thing_to_compare" ? "checked" : "") ?&gt; 一样工作
    • 谢谢,明白了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多