D
D
Dauren S2017-03-10 07:55:39
Yii
Dauren S, 2017-03-10 07:55:39

Yii2 in grid view to make a text filter on the associated field?

Made a connection in the model. In grdiview the data comes from the linked table but there is no filter. How to make a filter appear?

[
            'label' => 'Полное имя',
            'attribute'=>'fullname',
            'filter'=>workers::getFullname(),
            'format' => 'text',
            'value' => 'workerInfo.fullname',
        	],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dauren S, 2017-03-10
@dauren101

Solved the problem
If you need a dropdownlist, then do the following
In the model, add a link

public function getWorkerInfo()
  {
    	return $this->hasOne(workersInfo::className(), ['userid' => 'userid']);
  }

Then in the grid
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'userid',
'login',
[
'label' => 'Full name',
'attribute' =>'fullname',
'value' => 'workerInfo.fullname',
'format' => 'text',
// esli nujen select
'filter'=>workersInfo::find()->select(['fullname' , 'fullname'])->indexBy('fullname')->column(),
],
['class' => 'yii\grid\ActionColumn'],
],
If just a text field,
then remove the filter
and in the Search model
1) add a field to the rules
2) public function search($params)
{
$query = workers::find();
$query->joinWith(['workerInfo']);
3) $query->andFilterWhere(['like', 'login', $this->login])
->andFilterWhere(['like', 'workers_info.fullname', $this->fullname]);

A
Alexey, 2017-03-10
@masterfreelance

YII2: SORT AND FILTER GRIDVIEW BY RELATED AND ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question