【问题标题】:Yii2: how to display an attribute in view outside GridViewYii2:如何在 GridView 之外的视图中显示属性
【发布时间】:2015-02-26 12:52:46
【问题描述】:

我知道在 GridView 中可以通过在 columns 中命名来显示属性:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'emptyCell' => '-',
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'myAttribute',
        'myRelation.myRelatedAttribute',

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

但是有没有这样一种直接的方法可以在 GridView 之外不求助于类方法呢?例如,在视图标题而不是标题中:

<h1><?= Html::encode($this->title) ?></h1>

提前致谢。

【问题讨论】:

    标签: gridview attributes yii2


    【解决方案1】:

    你可以像这样得到模型列表:

    $dataProvider->models;
    

    所以如果你想得到列表中第一个模型的标题:

    <h1><?= !empty($dataProvider->models) ? $dataProvider->models[0]->title : '' ?></h1>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多