Code:

<?= GridView::widget([ 'dataProvider' => $dataProvider, 'tableOptions' => [ 'class' => 'table table-striped table-bordered table-hover' ], 'columns' => [ 'id', [ 'attribute'=>'name', 'label'=>'Заголовок', 'content'=>function($data){ return \common\models\Helper::titleFormat($data['id']); } ], [ 'attribute'=>'username', 'label'=>'Пользователь', 'content'=>function($data){ return \frontend\models\Items::getPropLabel($data['id'], '_username_'); } ], [ 'attribute'=>'phone', 'label'=>'Телефон', 'content'=>function($data){ return \frontend\models\Items::getPropLabel($data['id'], '_phone_'); } ], [ 'attribute'=>'status', 'label'=>'Состояние', 'filter' => \yii\helpers\ArrayHelper::map(\backend\models\ItemStatus::find()->all(), 'id', 'name'), 'content'=>function($data){ return \backend\models\ItemStatus::findOne($data['status'])['name']; } ], [ 'attribute'=>'label', 'label'=>'Метка', 'content'=>function($data){ $l = \backend\models\Labels::findOne($data['label']); return '<span class="label '.$l['class'].'">'.$l['name'].'</span>'; } ], [ 'class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}', 'buttons' => [ 'update' => function ($url,$model,$key) { return '<a class="btn btn-info" onclick="item.edit( '.$key.');"><i class="fa fa-pencil"></i></a>'; }, 'delete' => function ($url,$model,$key) { return '<button class="btn btn-danger" data-toggle="delete" data-id="'.$key.'" data-table="items"><i class="fa fa-trash"></i></button>'; }, ], ] ], ]); ?> 

By the id , status and label fields, sorting was automatically added, but not for the rest.

How to make sorting appear for the remaining fields? And how to add filtering for each displayed column?

If you have a link to detailed documentation, I will be grateful, because what I find in Google is not clear.

  • I would look at SearchModel, it is quite possible that these fields are virtual. We must look in detail. - fedornabilkin
  • Yes, they are not in the model itself. if not - sorting is not possible? - Diefair pm
  • one
    Anything is possible, do you have a searchModel? generated it in gii? If yes, then put it here, and also lay out the model itself for which dataProvider is looking for - pa3py6aka

0