I have this code
SearchModel
public function searchSubject($params, $groupingId = null)
{
$query = (new \yii\db\Query())
->select(['sbt.subject_id', 'sbt.subject_title', 'grp.grouping_id', 'grp.grouping_name', 'sgp.subject_grouping_id'])
->from('grouping grp')
->join('INNER JOIN', 'subject_grouping sgp', 'sgp.subject_grouping_grouping_id = grp.grouping_id')
->join('INNER JOIN', 'subject sbt', 'sbt.subject_id = sgp.subject_grouping_subject_id')
->where([
'sbt.is_status' => '0',
'grp.grouping_id' => $groupingId
])
->orderBy('subject_title ASC');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
return $dataProvider;
}
Controller
public function actionCreate()
{
$model = new Exam();
$searchModel = new SubjectSearch();
$dataProvider = $searchModel->searchSubject(Yii::$app->request->queryParams);
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
$model->attributes = $_POST['Exam'];
$model->save();
return $this->redirect(['index']);
}
else {
return $this->render('create', [
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
View
<div class="col-xs-12 col-sm-4 col-lg-4">
<?= $form->field($model, 'exam_group_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(app\models\Grouping::find()->all(),'grouping_id','grouping_name'),
'language' => 'en',
'options' => ['placeholder' => '--- Select Grouping ---'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
<div class="col-xs-12" style="padding-top: 10px;">
<div class="box">
<div class="box-body table-responsive">
<div class="subject-index">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'summary'=>'',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'subject_title',
],
]); ?>
</div>
</div>
</div>
</div>
I only want to save the dropdwonlist into the database. I have a Dropdownlist and a Gridview. The Gridview is generated using QueryBuilder and it has a Parameter. I want it that when the Dropdownlist is onChange, it will determine what will be displayed on the Gridview. The Parameter, $groupingId from the searchModel of the Gridview must be equal to the grouping_id of the Dropdownlist.
How do I achieve this in making the gridview dependent on the dropdownlist using the $groupingId parameter?