【问题标题】:Yii2 bundle tables in a viewYii2 视图中的捆绑表
【发布时间】:2018-01-17 10:07:00
【问题描述】:

我是 Yii2 的新手,不,我试图在视图中显示数据。我以前做过这样的(使用GridView):

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
                'attribute' => 'car_id',
                'value' => 'car.state_num'
        ],
        [
            'attribute' => 'driver_id',
            'value' => 'driver.name'
        ],
        'status',
        'first_date',
         'second_date',
        // 'status',
        // 'foto:ntext',
        // 'description:ntext',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]);

(它的视图显示了与“Driver”和“Car”表连接的“contract”表)

但现在我不会通过常见的引导表来执行此操作,如下所示:

<table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Car</th>
            <th scope="col">Start</th>
            <th scope="col">Finish</th>
            <th scope="col">Status</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($dataProvider->getModels() as $item):?>
        <tr>
            <th scope="row">Order ID <?= $item->id ?> </th>
            <td><?= $item->car_id ?></td>
            <td><?= $item->first_date ?></td>
            <td><?= $item->second_date ?></td>
            <td><?= $item->status ?></td>
        </tr>
        <? endforeach; ?>
        </tbody>
    </table>

它也可以,但是 Car 的列显示 car_id.. 问题:我怎样才能将 car.state_num 用于在视图中显示它?

【问题讨论】:

    标签: php yii2 frameworks


    【解决方案1】:

    使用$item-&gt;car-&gt;state_num

    <table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Car</th>
            <th scope="col">Start</th>
            <th scope="col">Finish</th>
            <th scope="col">Status</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($dataProvider->getModels() as $item):?>
        <tr>
            <th scope="row">Order ID <?= $item->id ?> </th>
            <td><?= (!empty($item->car_id)) ? $item->car->state_num : null ?></td>
            <td><?= $item->first_date ?></td>
            <td><?= $item->second_date ?></td>
            <td><?= $item->status ?></td>
        </tr>
        <? endforeach; ?>
        </tbody>
    </table>
    

    【讨论】:

    • 太棒了!非常感谢。现在我知道 dataProvider 真是太好了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    相关资源
    最近更新 更多