How To Create Multilevel Dependent Dropdownlist In Yii

Hi, I want to create multilevel dependent DropDownList.

This are my tables form database.


class{id,class_name}

subjects{id,class_id,name}

sub_section{id,class_id,sub_id,section_name}

sub_topic{id,class_id,sub_id,sub_section_id,topic_name}


While creating records for sub_topic table,

I want to display available classes from class table in a DropDownlist (I am able to do this) and then on selecting a class, I want to display the corresponding subjects(name) from subjects table in second DropDownList ,where subjects.class_id = selected id in the class dropdown (I am also able to do this),

Now on selecting a subject, I want to display the corresponding section(section_name) from sub_section table in third DropDownList ,

where sub_section.sub_id = selected id in the subjects dropdown.

here i am getting third dropdown empty.

This is my code

[size="2"]View file[/size]




	<div class="row">

		<?php echo $form->labelEx($model,'class_id'); ?>

		<?php

                  $classArray = CHtml::listData(Classes::model()->findAll(),'id','class_name');

                   echo $form->DropDownList($model,'class_id',$classArray,

                            array(

							    'prompt'=>'Select Class',

                                'ajax' => array(

                                'type'=>'POST',

                                'url'=>CController::createUrl('SubTopic/dynamicSubjects'),

                                'update'=>'#'.CHtml::activeId($model,'sub_id')

                                 )));

		?>

        <?php echo $form->error($model,'class_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'sub_id'); ?>

		<?php echo $form->dropDownList($model,'sub_id',array(

																	'prompt'=>'Select Subject',

	    															'ajax' => array(

																	'type'=>'POST',

																	'url'=>CController::createUrl('SubTopic/dynamicSubSection'),

																	'update'=>'#'.CHtml::activeId($model,'sub_section_id')	

																))); ?>

		<?php echo $form->error($model,'sub_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'sub_section_id'); ?>

		<?php echo $form->dropDownList($model,'sub_section_id',array()); ?>

		<?php echo $form->error($model,'sub_section_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'topic_name'); ?>

		<?php echo $form->textField($model,'topic_name',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'topic_name'); ?>

	</div>



[size="2"]My Controller file SubTopicController.php[/size]





public function actionDynamicSubjects() {

            $class_id = $_POST['SubTopic']['class_id'];

            $data=Subjects::model()->findAll('class_id=:class_id',

                    array(':class_id'=> $class_id));


            $data=CHtml::listData($data,'id','name');

            foreach($data as $value=>$name)  {

                echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($name),true);

            }

        }

		

	public function actionDynamicSubSection() {

            $sub_id = $_POST['SubTopic']['sub_id'];

            $data=SubSection::model()->findAll('sub_id=:sub_id',

                    array(':sub_id'=> $sub_id));


            $data=CHtml::listData($data,'id','section_name');

            foreach($data as $value=>$section_name)  {

                echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($section_name),true);

            }

        }



Have you checked if your SubTopic/dynamicSubSection url is actually called through Ajax? If you haven’t checked, you can do so through the Firebug console.

If it’s called correctly upon selection of the subsection dropdown, what’s the response code & contents?

These may help you!

http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

http://www.yiiframework.com/extension/cascadedropdown/

http://www.yiiframework.com/wiki/24/

thanx for your reply.

my SubTopic/dynamicSubSection url was not getting called through ajax, there was one parameter missing, when i added the missing parameter it worked for me.

this is my working code of view where i made changes




<?php echo $form->dropDownList($model,'sub_id',array(),array(

																	'prompt'=>'Select Subject',

	    															'ajax' => array(

																	'type'=>'POST',

																	'url'=>CController::createUrl('SubTopic/dynamicSubSection'),

																	'update'=>'#'.CHtml::activeId($model,'sub_section_id')	

																))); ?>



thanx for your reply.

http://www.yiiframework.com/forum/index.php/topic/44230-display-the-drop-down-list-value-automatically-in-update-by-ajax/page__p__209595__fromsearch__1#entry209595

check this too

http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

thanx for your help.