【问题标题】:Matrix from table in blade laravel来自刀片 laravel 中表格的矩阵
【发布时间】:2016-03-30 16:40:19
【问题描述】:

我有

表名为“矩阵”,字段为 (kriteria1, kriteria2,grade)

在我的 mvc 中我这样做了:

首先在“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

我得到了这个结果

如何在刀片中回显它,使其变成这样:

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

【问题讨论】:

  • 信息在第一个表中输出时是如何存储的,为了转换数据输出,我们需要知道它的结构。请包含您在其中创建第一个表的视图文件。

标签: laravel laravel-blade


【解决方案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;
}

【讨论】:

  • 如此接近,但我仍然有问题,在我的格式中它必须是矩阵类型,其中每次行和列具有相同的数字,例如 row2(kriteria1) 和 column2(kriteria2) 符合等级始终为 1。prntscr.com/alvr75 等级应为“5 1 5”
  • 您可以在抓取时添加一些排序。我已经更新了答案。
  • 我在 Ahp.php 第 49 行收到错误 FatalErrorException:访问未声明的静态属性:App\Matrix::$records 我使用 laravel 5.2
  • 您的回答有误,应该是 $records = Matrix::orderBy('kriteria2'),谢谢
  • 啊,我的错误在那里,我的编辑器复制/粘贴错误。 :)
【解决方案2】:

这应该让你接近,你只需要聪明地了解如何在 td 元素上使用 colspanrowspan 属性。

<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

【讨论】:

  • 我得到了像prntscr.com/alvp6y 这样的奇怪结果。它不被视为矩阵类型。
猜你喜欢
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 2021-02-11
  • 2020-03-20
相关资源
最近更新 更多