0

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 :)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm using custom role table to get the rolename based on the userid , yes i have searchmodel with role attribute as safe.
Now i used this code in my filter parameter 'filter' => Html::activeDropDownList($searchmodel, 'ClientId',$func,['class'=>'form-control','prompt' => 'Select Role']), this one after selecting the role and selecting back to default prompt query not resetting why ?
I don't really like giving HTML to filter. You can give an array to this attribute and it will generate a dropdownlist. Try filter => \yii\helpers\ArrayHelper::map(\Yii::$app->authManager->getRoles(),'name','name')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.