2016-01-24 48 views
3

與我的問題相關我在網上搜索了很多,但沒有找到我的問題的答案。是否可以在gridview的datacolumn中設置相關模型的標籤值yii2

我嘗試在yii2中的gridview中完成一個簡單的任務,細節如下。

<?= 
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') 
    ], 

我曾嘗試下面的代碼,但它給

Blockquote htmlspecialchars() expects parameter 1 to be string, object given Blockquote

[ 
      '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版本。 任何幫助將不勝感激

回答

2

嘗試用頭

$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') 
    ], 
+0

感謝您的回覆@scaisEdge。 我會在這裏檢查和更新 – Hikmat

+0

得到以下錯誤: trim()期望參數1是字符串,給定的對象 – Hikmat

+0

只是一個好奇心...你需要逐行更改標題或行標題固定爲每行和只有在您打開頁面或模型集合時才更改? – scaisEdge

相關問題