Manohar singh
Manohar singh

Reputation: 509

Yii2 Gridview widget make dropdown filter option selected

Hi i'm using yii2 Gridview and i have a filter dropdown option in my gridcolumn , while selecting the dropdown option my grid result changes correctly but i need to retain the selected dropdown option filter , see my code below,

$func = Model::get_rolename();

 GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchmodel,
            'rowOptions' => function($model) {

                return ['data-tt-id' => $model['UserId'], 'data-tt-parent-id' => $model['ManagerId']];
            },
            'options' => ['id' => 'sup_review'],
            'columns' => [

                [
                    'label' => 'First name',
                    'sortLinkOptions' => ['class' => 'desc'],
                    'format' => 'html',
                    'attribute' => 'FirstName',
                    'filterInputOptions' => [
                        'placeholder' => 'Search Name..',
                        'class' => 'form-control',
                    ],
                    'value' => function ($model, $key, $index, $column) {
                        return $model['FirstName'];
                    },
                ],
                [
                    'label' => 'Role test',
                    'sortLinkOptions' => ['class' => 'desc'],
                    'format' => 'ntext',
                    'attribute' => 'ClientId',
                    'filter' => $func,
                    'value' => function ($model, $key, $index, $column) {
                        return $model['RoleName'];
                    },
                ],

                [
                    'label' => 'Actions',
                    'content' => function($model) use ($from, $to) {
                        return Html::a('<span class="btn btn-sm btn-primary">View</span>', Yii::$app->request->baseUrl . '/controller/function/?userid=' . $model['UserId'] . '&from=' . $from . '&to=' . $to, [
                                    'title' => Yii::t('app', 'View'),
                                    'data-pjax' => '0',
                        ]);
                    }
                ],
            ],
        ]);

Thanks in advance :)

Upvotes: 0

Views: 1420

Answers (1)

HunWalk
HunWalk

Reputation: 82

Yeah. That is because roles in yii2 rbac are stored in items, and there is a separate table that is assigning roles to users. Your code is way too tight to guess what is going on at the back..but I'm guessing you're making a user index? Anyway:

  • You need Yii::$app->authManager->getRoles(); to get the array of roles.
  • You need to join the auth_assignment table in your searchModel

    if($this->role){ $query->join('LEFT JOIN','auth_assignment','auth_assignment.user_id = client_id') ->andFilterWhere(['auth_assignment.item_name' => $this->role]); }

  • Make sure your searchModel contains role and that attribute is safe.

Upvotes: 1

Related Questions