是否可以在 YiII2 中 GridView 的数据列中设置相关模型的标签值


is it possible to set label value from related model in datacolumn in gridview in yii2

与我的问题有关,我在互联网上搜索了很多,但没有找到问题的答案。

我正在尝试在yii2的网格视图中完成一个简单的任务,详细信息如下。

<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii'grid'SerialColumn'],
        'id',
        'Name',
        'Email:email',
        'Phone',
        'Age',
        [
            'class' => yii'grid'DataColumn::className(),
            'label' =>'Custom',
            'attribute' => 'cusId',
            'format'=>'html',
            'value' => function ($model) {
                return Html::a($model->cus->Key, ['customobj/view', 'id' => $model->cusId],['class' => 'btn btn-success']);
            },
            'filter' => yii'helpers'ArrayHelper::map(app'models'Customobj::find()->all(), 'Id', 'Key')
        ],
        ['class' => 'yii'grid'ActionColumn'],
    ],
]);
?>

上面的代码工作正常。现在我想在下面从相关模型中制作标签动态。

[
        'class' => yii'grid'DataColumn::className(),
        'label' =>'Custom',
        'attribute' => 'cusId',
        'format'=>'html',
        'value' => function ($model) {
            return Html::a($model->cus->Key, ['customobj/view', 'id' => $model->cusId],['class' => 'btn btn-success']);
        },
        'filter' => yii'helpers'ArrayHelper::map(app'models'Customobj::find()->all(), 'Id', 'Key')
    ],

我已经尝试了下面的代码,但它给出了

块引用 htmlspecialchars() 期望参数 1 是字符串,对象给定 块引用

[
            'class' => yii'grid'DataColumn::className(),
            'label' => function ($model) {
                return $model->cus->Key;
            },
            'attribute' => 'cusId',
            'format'=>'html',
            'value' => function ($model) {
                return Html::a($model->cus->Key, ['customobj/view', 'id' => $model->cusId],['class' => 'btn btn-success']);
            },
            'filter' => yii'helpers'ArrayHelper::map(app'models'Customobj::find()->all(), 'Id', 'Key')
        ],

我正在使用 yii2 版本。任何帮助将不胜感激

尝试使用标题

$varHeader = yourFuntion();
[
        'class' => yii'grid'DataColumn::className(),
        'header' =>  $varHeader ,
        'attribute' => 'cusId',
        'format'=>'html',
        'value' => function ($model) {
            return Html::a($model->cus->Key, ['customobj/view', 'id' => $model->cusId],['class' => 'btn btn-success']);
        },
        'filter' => yii'helpers'ArrayHelper::map(app'models'Customobj::find()->all(), 'Id', 'Key')
    ],