从刀片laravel中的表矩阵

时间:2016-03-29 20:45:03

标签: laravel laravel-blade

我有

  

表叫#34;矩阵",字段是(kriteria1,kriteria2,grade)

在我的mvc中我这样做了:

  

首先通过调用我的模型" matrix" in" AHPController" :

$alltable = Matrix::all(); return view('result', compact('alltable'));

  

并在"结果"中调用它观点:

@foreach($alltable as $r1) <tr> <td>{{ $r1->kriteria1 }}</td> <td>{{ $r1->kriteria2 }}</td> <td>{{ $r1->grade }}</td> </tr> @endforeach

  

我得到了这个结果

enter image description here

如何在刀片中回显它,所以它变成这样:enter image description here

只是忽略颜色,只需要那种格式的刀片中显示的矩阵。谢谢。

2 个答案:

答案 0 :(得分:1)

在这种情况下我要做的是首先构建一个二维数组,然后在视图中循环遍历该数组。

// In your controller method
$records = Matrix::orderBy('kriteria2')
    ->orderBy('kriteria1')
    ->get();

$rows = [];
$columns = [];
foreach($records as $index => $record) {
    // Create an empty array if the key does not exist yet
    if(!isset($rows[$record->kriteria1])) {
        $rows[$record->kriteria1] = [];
    }

    // Add the column to the array of columns if it's not yet in there
    if(!in_array($record->kriteria2, $columns)) {
        $columns[] = $record->kriteria2;
    }

    // Add the grade to the 2 dimensional array
    $rows[$record->kriteria1][$record->kriteria2] = $record->grade;
}

然后在您的视图中,您可以像这样循环:

<table>
    <thead>
        <tr>
            <th><!-- Empty for the left top corner of the table --></th>
            @foreach($columns as $column)
            <th>{{ $column }}</th>
            @endforeach
        </tr>
    </thead>
    <tbody>
    @foreach($rows as $kriteria1 => $columns)
        <tr>
            <td><strong>{{ $kriteria1 }}</strong></td>
            @foreach($columns as $kriteria2 => $grade)
            <td>{{ $grade }}</td>
            @endforeach
        </tr>
    @endforeach
    </tbody>
</table>

下面评论中的第二个问题更新:

// In your controller method
$records = Matrix::orderBy('kriteria2')
    ->orderBy('kriteria1')
    ->get();

// Build up a map like this from the data with the names
$names = [
    'kriteria1' => 'name1',
    'kriteria2' => 'name2',
    'kriteria3' => 'name3',
];

$rows = [];
$columns = [];
foreach($records as $index => $record) {
    $name1 = $names[$record->kriteria1];
    $name2 = $names[$record->kriteria2];

    // Create an empty array if the key does not exist yet
    if(!isset($rows[name1])) {
        $rows[name1] = [];
    }

    // Add the column to the array of columns if it's not yet in there
    if(!in_array(name2, $columns)) {
        $columns[] = name2;
    }

    // Add the grade to the 2 dimensional array
    $rows[name1][name2] = $record->grade;
}

答案 1 :(得分:0)

这应该让您接近,您只需要聪明地使用colspan元素上的rowspantd属性。

<tr>
    <td colspan="2"></td>
    <td colspan="{{ count(array_keys($alltable->first()->toArray())) }}">kriteria2</td>
</tr>
<tr>
    <td colspan="2"></td>
    @foreach(array_keys($alltable->first()->toArray()) as $i => $val)
        <td>{{ $i }}</td>
    @endforeach
</tr>
@foreach($alltable as $i => $r1)
    <tr>
        @if($i == 0)
            <td rowspan="{{ $alltable->count() }}"></td>
        @endif
        <td>{{ $i }}</td>
        <td>{{ $r1->kriteria1 }}</td>
        <td>{{ $r1->kriteria2 }}</td>
        <td>{{ $r1->grade }}</td>
    </tr>
@endforeach