douhuiyan2772
2017-02-21 18:41 阅读 40
已采纳

带有Blade Framework和Laravel的动态复选框+表

I need to organize my checkbox fields in table lines.

I want every 10 items the blade breaks the table row.

Here is my code:

<table>

  <div class="btn-group" data-toggle="buttons">
    {{$i = 0}}

    @foreach($sintese as $s)
        <tr>
            <td>
                <label class="btn btn-primary">
                    <input type="checkbox" autocomplete="off" name="chksintese" id="{{$s->cod_sintese_conversa}}">
                    <span class="glyphicon glyphicon-ok"></span>
                    {{$s->descricao}}
                </label>
            </td>

            @if ($i > 10)
                {{'</tr>'}}
                {{$i = 0}}  
            @else
                {{$i++}}
            @endif

        @endforeach
    </div>

</table>

And Here is My Result:

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongling2038 dongling2038 2017-02-21 19:36

    What about:

    <table>
        <div class="btn-group" data-toggle="buttons">
            <tr>
                @foreach($sintese as $s)
                    <td>
                        <label class="btn btn-primary">
                            <input type="checkbox" autocomplete="off" name="chksintese" id="{{$s->cod_sintese_conversa}}">
                            <span class="glyphicon glyphicon-ok"></span>
                            {{$s->descricao}}
                        </label>
                    </td>
    
                    @if ($loop->iteration % 10 == 0 && !$loop->last)
                        </tr><tr>
                    @endif
                @endforeach
            </tr>
        </div>
    </table>
    
    点赞 评论 复制链接分享
  • duanpie4763 duanpie4763 2017-02-21 19:04

    You're continually opening a new row tag, but only closing it every 10. You're also echoing the counter, which isn't needed. Instead, open it before the loop, then reset it every 10. Don't reset $i, but instead check it against the remainder operator, and make sure you're not going to create an empty row.

        <tr>
    @foreach($sintese as $s)
            <td>
                <label class="btn btn-primary">
                    <input type="checkbox" autocomplete="off" name="chksintese" id="{{$s->cod_sintese_conversa}}">
                    <span class="glyphicon glyphicon-ok"></span>
                    {{$s->descricao}}
                </label>
            </td>
    
            @if ($i % 10 == 0 && $i < count($sintese))
                <tr/><tr>
            @endif  
        <?php $i++ ?>
    
        @endforeach
        </tr>
    
    点赞 评论 复制链接分享

相关推荐