jquery - how to update a dependent dropdown like create data in yii2 -
i add code, code select sub category dropdown notwork on upadte page worked on create page. how this
<?php $coursescat = arrayhelper::map(coursescat::find()->all(),'id', 'cat_name'); $form = activeform::begin(['options' => ['enctype' => 'multipart/form-data']]); if($model->isnewrecord) { echo $form->field($model, 'cat_id')->dropdownlist($coursescat, ['prompt'=>'-choose category-', 'onchange'=>' $.post( "'.urldecode(yii::$app->urlmanager->createurl('coursedetail/lists&id=')).'"+$(this).val(), function( data ) { $( "select#subcat_id" ).html( data ); }); ']); echo $form->field($model, 'subcat_id') ->dropdownlist( ['prompt'=>'-choose sub category-'], ['id'=>'subcat_id'] ); } else { echo $form->field($model, 'cat_id')->dropdownlist($coursescat, ['prompt'=>'-choose category-', 'onchange'=>' $.post( "'.urldecode(yii::$app->urlmanager->createurl('coursedetail/lists&id=')).'"+$(this).val(), function( data ) { $( "select#subcat_id" ).html( data ); }); ']); echo $form->field($model, 'subcat_id') ->dropdownlist( ['prompt'=>'-choose sub category-'], ['id'=>'subcat_id'] ); } } ?> <div class="form-group"> <?= html::submitbutton($model->isnewrecord ? 'create' : 'update', ['class' => $model->isnewrecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php activeform::end(); ?>
in controller
public function actionlists($id) { echo $sql = "select * courses_subcat cat_id='$id' "; $models = coursessubcat::findbysql($sql)->asarray()->all(); //echo "<pre>";print_r($model);exit; if(sizeof($models) >0){ echo "<option>-choose sub category-</option>"; foreach($models $model){ echo "<option value='".$model['id']."'>".$model['name']."</option>"; } } else{ echo "<option>-choose sub category-</option><option></option>"; } }
you using yii 2. can use kartik dependent dropdown. easy implement. please read docs here http://plugins.krajee.com/dependent-dropdown
here demo :- http://plugins.krajee.com/dependent-dropdown/demo
Comments
Post a Comment