J
J
Julia2020-07-27 09:57:33
Yii
Julia, 2020-07-27 09:57:33

Dependent dropdownlist in gridview yii2?

Hello! Tell me, please, or give me an idea: there is a gridview and there are 2 drop-down lists (data from different databases) in 2 columns. Data from the Service(sid, service) table and the Post(pstid, post) table. How to make dependent dropdownlist (service->post)?
In views :
I replace numeric data with text data from other tables:

<?php Pjax::begin(['id' => 'comments', 'enablePushState' => false, 'linkSelector' => '.btn-custom']); ?>
<?php
    $u = Users::find()->select('sid, pstid')->where('unid=29 and status=1')->all();
    $a = ArrayHelper::map($u, 'sid', 'sid');
    $a1 = ArrayHelper::map($u, 'pstid', 'pstid');
    $f = Html::activeDropDownList($searchModel, 'sid', ArrayHelper::map(Service::find()->where(['in', 'sid', $a])->orderBy(['service' => SORT_ASC])->asArray()->all(), 'sid', 'service'),['class'=>'form-control', 'prompt'=>'']);
    $f1 = Html::activeDropDownList($searchModel, 'pstid', ArrayHelper::map(Post::find()->where(['in', 'pstid', $a1])->orderBy(['post' => SORT_ASC])->asArray()->all(), 'pstid', 'post'),['class'=>'form-control', 'prompt'=>'']);
    ?>
<div class="report-index card border-primary">
        <table>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'layout'=>"{pager}\n{summary}\n{items}\n{pager}",
            'tableOptions' => [
                'class' => 'table tableusers table-striped table-bordered table-warp'
            ],
            'columns' => [
                [
                    'class' => 'yii\grid\SerialColumn',
                    'contentOptions' => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
                    'header' => '№',
                ],
                [
                    'attribute' => 'name',
                    'label' => 'Сотрудник',
                    'contentOptions' => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
                    'headerOptions'   => ['class' => 'text-center'],
                ],
                [
                    'attribute' => 'services',
                    'label' => 'Отдел',
                    'value'=>'services.service',
                    'filter' => $f,
                    'contentOptions'  => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
                    'headerOptions'   => ['class' => 'text-center']
                ],
                [   'attribute' => 'posts',
                    'label' => 'Должность',
                    'value'=>'posts.post',
                    'filter' => $f1,
                    'contentOptions'  => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
                    'headerOptions'   => ['class' => 'text-center', 'scope'=>'col']
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{view}',
                    'contentOptions' => ['style' => 'text-align: center; vertical-align:middle;'],
                    'headerOptions'   => ['class' => 'text-center'],
                    'buttons' => [
                        'view' => function ($url,$model) {
                            Icon::map($this, Icon::FAS);
                            return Html::a(
                                '<span class="fas fa-eye" aria-hidden="true"></span>',
                                $url);
                        },
                    ],
                ],
            ],
        ]);
        ?>
        </table>
    </div>
    <?php Pjax::end(); ?>


In controller:
public function actionIndex()
    {
        $query = Users::find()->where(['status' => 1, 'unid' => 29]);

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

        $searchModel = new UsersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question