简体   繁体   English

Yii2 Gridview小部件使下拉过滤器选项处于选中状态

[英]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, 嗨,我使用的是yii2 Gridview,并且我的gridcolumn中有一个filter dropdown选项,同时选择dropdown选项,我的网格结果正确更改,但是我需要保留选择的dropdown option filter,请参见下面的代码,

$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 :) 提前致谢 :)

Yeah. 是啊。 That is because roles in yii2 rbac are stored in items, and there is a separate table that is assigning roles to users. 这是因为yii2 rbac中的角色存储在项目中,并且有一个单独的表将角色分配给用户。 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(); 您需要Yii::$app->authManager->getRoles(); to get the array of roles. 获取角色数组。
  • You need to join the auth_assignment table in your searchModel 您需要在searchModel中加入auth_assignment表

    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. 确保您的searchModel包含角色并且该属性是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM