Vadym
Vadym

Reputation: 548

GridView search filters don't appear Yii2

I've created GridView widget in my view with following params:

<?= GridView::widget([
        'dataProvider' => $provider,
        'filterModel' => $searchModel,
        'summary' => '<br>',
        'showOnEmpty' => false,
        'columns' => [
            [
                'attribute' => 'name',
                'value' => function ($model) {
                    return substr($model->name, 0, 50);
                },
            ],
            [
                'label' => "Ім'я користувача",
                'attribute' => "user.name",
                'value' => function ($model) {
                    return substr($model->user->name, 0, 50);
                },
            ],
            [
                'label' => 'Назва предмету',
                'attribute' => "subjects.name",
                'value' => function ($model) {
                    return substr($model->subjects->name, 0, 50);
                },
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {delete}'
            ]
        ],
    ]) ?>

My controller has such code:

        $searchModel = new DocumentsSearch();
    $provider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'provider' => $provider,
        'searchModel' => $searchModel
    ]);

Also I'm using DocumentSearch model that has following description:

class DocumentsSearch extends Documents
{
    public $username;
    public $subject_name;

    public function rules()
    {
        return [
            [['username', 'subject_name'], 'safe']
        ];
    }

    public function search($params)
    {
        $query = Documents::find()
            ->joinWith(['user', 'subjects']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
            'pagesize' => 30,
        ],
    ]);

    $dataProvider->sort->attributes['user.name'] = [
        'asc' => ['user.name' => SORT_ASC],
        'desc' => ['user.name' => SORT_DESC],
    ];

    $dataProvider->sort->attributes['subjects.name'] = [
        'asc' => ['subjects.name' => SORT_ASC],
        'desc' => ['subjects.name' => SORT_DESC],
    ];


    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }


    $query->andWhere('user.name LIKE "%' . $this->name . '%" ');

    $query->andWhere('user.name LIKE "%' . $this->username . '%" ');

    $query->andWhere('subjects.name LIKE "%' . $this->subject_name . '%" ');


    return $dataProvider;

    }

}

DocumentSearch extends Document model. And I've next relations:

public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'owner_id']);
}

public function getSubjects()
{
    return $this->hasOne(Subjects::className(), ['id' => 'subject_id']);
}

All these things give me next result:

ScreenSh

Before asking I've tried to create inputs by myself using filter param for each column like 'filter'=>Html::input('text','DocumentSearch[subjects.name]') It triggers JS to send the request but if (!($this->load($params) && $this->validate())) method obvious returns false.

Upvotes: 2

Views: 4434

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

Try using the correspondenting name in rules

         [
            'label' => "Ім'я користувача",
            'attribute' => "username",
            'value' => function ($model) {
                return substr($model->user->name, 0, 50);
            },
        ],
        [
            'label' => 'Назва предмету',
            'attribute' => "subject_name",
            'value' => function ($model) {
                return substr($model->subjects->name, 0, 50);
            },
        ],

see this guide http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#filtering-data

 // only fields in rules() are searchable

Or you must extend you search model for calculated or related field adding eventually the proper value in rules

Upvotes: 4

Related Questions