ADD SEARCH OR FILTER FOR RELATED ATTRIBUTE IN KARTIK GRID VIEW IN YII 2

 

Make sure you the class has relationship with the other class.

For example, at model application.php
public function getListofvenues()
 {
 return $this->hasOne(Venues::className(), ['id' => 'listofvenues_id']);
 }

Don’t forget to configure gridview at config/mainphp
'gridview' => [
 'class' => '\kartik\grid\Module',
 // your other grid module settings
 ],
 'gridviewKrajee' => [
 'class' => '\kartik\grid\Module',
 // your other grid module settings
 ],

Define gridcolumn variable at views\index.php before calling widget. Add use for the other class.
<?php
use backend\modules\exam\models\Venues;

 $gridColumns = [
 //['class' => 'yii\grid\SerialColumn'],
 ['class' => 'kartik\grid\SerialColumn'],
 
 'studentuser_studentNo',
 'studentLastName',
 'studentFirstName',
'semester',
 'schoolYear',
'coursesWithProctoredExam',
 'typeOfExam',
[
 'attribute' => 'location',
 'value' => function($model) {
 return $model->location == 1 ? 'Local' : 'Abroad';
 },
 'filter' => [
 1 => 'Local',
 2 => 'Abroad'
 ]
 ],
[
 'attribute' => 'listofvenues_id',
 'value' => function($model) {
 $venue = Venues::findOne($model->listofvenues_id);
 return $venue->name;
 },
 'filterType' => GridView::FILTER_SELECT2,
 'filter' => ArrayHelper::map(
 Venues::find()->orderBy('id')->asArray()->all(),'id', 'name'
 ),
 'filterWidgetOptions' => [
 'pluginOptions' => ['allowClear' => true],
 ],
 'filterInputOptions' => ['placeholder' => 'Venues'],
 'format' => 'raw'
 ],
 'nominatedExamVenue',

['class' => 'kartik\grid\ActionColumn'],
 
];

?>

Add the gridview widget
<?= GridView::widget([


 'dataProvider' => $dataProvider,
 
 'filterModel' => $searchModel,
 'columns' => $gridColumns, 
 'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
 'headerRowOptions' => ['class' => 'kartik-sheet-style'],
 'filterRowOptions' => ['class' => 'kartik-sheet-style'],
 'pjax' => true,
 // 'export' => [
 // 'fontAwesome' => true,
 // ]
 'floatHeader'=>true,
 'floatHeaderOptions'=>['scrollingTop'=>'50'],
 'showPageSummary' => true,

'toolbar' => [
 [
 'content'=>
 Html::button('<i class="glyphicon glyphicon-plus"></i>', [
 'type'=>'button', 
 'title'=>Yii::t('kvgrid', 'Apply for Exam'), 
 'class'=>'btn btn-success'
 ]) . ' '.
 Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['grid-demo'], [
 'class' => 'btn btn-default', 
 'title' => Yii::t('kvgrid', 'Reset Grid')
 ]),
 ],
 '{export}',
 '{toggleData}'
 ],

'panel' => [
 'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i>Exam Application</h3>',
 'type'=>'success',
 'before'=>Html::a('<i class="glyphicon glyphicon-plus"></i> Apply for an Exam', ['create'], ['class' => 'btn btn-success']),
 'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset Grid', ['index'], ['class' => 'btn btn-info']),
 'footer'=>false
 ],

]); ?>

Post a Comment

0 Comments