Здравствуйте! Подскажите, пожалуйста, или натолкните на мысль: есть gridview и есть 2 выпадающих списка(данные из разных бд) в 2 колонках. Данные из таблицы Service(sid, service) и таблицы Post(pstid, post). Как сделать зависимый dropdownlist (service-> post)?
Во views :
Подменяю числовые данные на текcтовые из других таблиц:
<?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(); ?>
В контроллере:
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,
]);
}