2

我在 Laravel 的 Blade 视图中有这段代码:

@foreach ($questions as $question)
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
  @if(isset($question->task))
   <a href="{{URL::action('showTask', $question->task_id)}}"><h4> <span class='glyphicon glyphicon-file'></span> {{$question->Task->name}}</h4></a>
  @endif

  
  <blockquote >
    <a class="nostyle" href="{{URL::action('showQuestion',$question->id)}}" ><p

        @if($question->status==1)
          class="text-success"
        @endif
      >{{$question->text}}</p></a>
  <footer>Gevraagd door <strong>{{$question->Author->getFullName('mij') }}</strong> op <strong>{{DateConverter::dateToString($question->created_at)}}</strong></footer>
  
  @if($question->status==1)
    <span class="label label-success">Beantwoord</span>
  @endif 
</blockquote>

</div>
  @endforeach

显示所有问题。我希望它们以3 的漂亮行显示。但是,有些文本($question->text)比其他文本更长,因此它们不会总是完美地开始新行,而是附加到最短的先前网格,如您在屏幕截图中所见。

在此处输入图像描述

我想要的更像是一张桌子,三列,然后是相同高度的新行,包含三个新项目。

所以我正在寻找一种方法

  • 要么将所有列返回相同的高度

  • 或者在每三列之后自动添加和关闭一个行 div。

    最好的方法是什么?

4

1 回答 1

13

你可以尝试这样的事情:

@foreach(array_chunk($questions->all(), 3) as $threeQuestions)
    <div class="row">
        @foreach($threeQuestions as $question)
            // Do everything here
            @if(isset($question->task))
                <a href="{{URL::action('showTask', $question->task_id)}}"><h4> <span class='glyphicon glyphicon-file'></span> {{$question->Task->name}}</h4></a>
            @endif

            // ...

        @endforeach
    </div>
@endforeach

在这里,$threeQuestions每次迭代包含三个项目,因此.row将每三个项目保持在div这样的范围内:

<div class='row'>
    item-1
    item-2
    item-3
</div>
<div class='row'>
    item-4
    item-5
    item-6
</div>
于 2014-03-16T01:21:23.000 回答