首页 > 解决方案 > 在刀片中水平对齐表格 td

问题描述

在我看来,我现在正试图让上学,有没有一种方法可以在不重复学生姓名的情况下水平对齐天数和状态:

在此处输入图像描述

这是我的刀片视图:

<table class="table table-hover table-bordered">
    <tr>
        <th>Student</th>
        <th>Days</th>
        <th>Status</th>
    </tr>
    @foreach($atnd as $atnds)
    <tr>
        <td>{{$atnds->lead->student_name}}</td>
        <td>{{$atnds->days}}</td>
        <td>{{$atnds->status}}</td>
    </tr>
    @endforeach
</table>

这是我的控制器

public function get_attendance(){
    $atnd = Attendance::with(['level','lead'])->where(['level_id'=> 11])->get();
    return view('attendance.index',compact('atnd'));
}

标签: htmllaravelhtml-tablelaravel-blade

解决方案


您可以做的是按 student_id 分组,->get()然后它将为您提供集合集合,然后在标题中显示日期和学生姓名(假设您对所有学生都有相同的日期)像这样

在控制器中

$atnds=Attendance::with(['level','lead'])->where(['level_id'=> 11])->get()->groupBy('student_id');

在视图中

<table class="table table-hover table-bordered">
    <tr>
        <th>Student</th>
        @foreach($atnds->first() as $row)
            <th>{{$row->days}}</th> //assuming you have dates same for all student
        @endforeach
    </tr>
    @foreach($atnds as $group)
        <tr>
            <td>{{ $group->first()->lead->student_name}}</td>
            @foreach($group as $row)
                <td>{{$row->status}}</td>
            @endforeach
        </tr>
    @endforeach
</table>

编辑

对于日期过滤器,您可以添加->whereMonth('days', '07')条件

要在状态中显示下拉列表,您可以像这样添加它(替换 $row->status)

<table class="table table-hover table-bordered">
    @if($atnds->count() > 0){
        <tr>
            <th>Student</th>
            @foreach($atnds->first() as $row)
                <th>{{$row->days}}</th>
            @endforeach
        </tr>
        @foreach($atnds as $studentId => $group)
            <tr>
                <td>
                   {{ $group->first()->lead->student_name}} 
                </td>
                @foreach($group as $index => $row)
                    <td>
                        <input type="hidden" name="student[{{ $studentId }}][{{ $index }}][day]" value = "{{ $row->days }}" />
                        <select name="student[{{ $studentId }}][{{ $index }}][status]">
                            <option {{ $row->status == 'P' ? 'selected' : '' }}  value = "P">P</option>
                            <option {{ $row->status == 'L' ? 'selected' : '' }}  value = "L">L</option>
                        </select>
                    </td>
                @endforeach
            </tr>
        @endforeach
    @else
        <tr><td>No Record</td></tr>
    @endif
</table>

推荐阅读