Thursday, 18 December 2014

Relating Drop Downs in YII

Drop Down 
selection of sub_section based on section :
 In Views

Section :
<?php echo $form->dropDownList($model,'sec_code',CHtml::listData(Sections::model()->findAll(),'sec_code','sec_name'),
                        array(
                                'prompt'=>'Select Section',
                                'ajax'=> array(
                                'type' => 'POST',
                                'url'=>CController::createUrl('Inward/Sec'),
                                'update'=>'#'.CHtml::activeId($model,'sub_scode'),
        )
                        )
                    );
 ?>
Sub_section :
 <?php echo $form->error($model,'Name of the Section Asst.'); ?>
        
 <?php echo $form->labelEx($model,'sub_scode'); ?> 

 <?php echo $form->DropDownList($model,'sub_scode',CHtml::listData(SubSections::model()->findAllByAttributes(array('sec_code'=>$model->sec_code)),'sub_scode','sub_section'),array('prompt'=>'Select Sub Section')); ?>
                <?php echo $form->error($model,'sub_scode'); ?> 



Defining action sec(Inward/Sec) in Inward controller 

 public function actionSec()
    {
        $data = SubSections::model()->findAll('sec_code=:sec_code',
                        array(':sec_code'=>(int) $_POST['Inward']['sec_code']));
        $data = CHtml::listData($data,'sub_scode','sub_section');
            foreach($data as $id => $value)
            {
                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
            }
    }

No comments:

Post a Comment