Yii Dynamic Dropdown Using Cformmodel

Hi Friends, Is there any tutorials or sample codes for a dynamic dropdown using CFormModel? Im having a hard time on how to apply jquery and ajax to yii,

For example: I have 2 dropdowns. Dropdown1 with values ‘1’=>‘Active’, ‘2’=>‘InActive’,

And for my 2nd dropdown, I want this code to execute to get its values,

and on my model: I have a function,

getStatus(){

if($Status == ‘1’){

SQL code where Status = 1;

}

else{

SQL code where status = 2;

}

}

I want to execute this after selecting data from my first dropdown.

Is there a way to do this, or any other related examples will be a big help, Thank you very much!

didi you read this wiki article?

Creating a dependent dropdown

Yes, I read that wiki but it uses CActiveRecord, Im using CFormModel on my project

there isn’t a big difference here if you look at code. all work has been done on view and controller just this part that retrieves data and creates list is related to active record




$data=Location::model()->findAll('parent_id=:parent_id', 

                  array(':parent_id'=>(int) $_POST['country_id']));

 

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



and an equivalent sql way for that example would be:




$data=Yii::app()->db->createCommand('select id,name from Location where parent_id=:parent_id')->query(array(':parent_id'=>(int) $_POST['country_id']));


foreach($data as $row)

    $list[$row['id']]=$row['name'];



so you can build your list due to your need

ahh ok here is my code, but my second dropdown is still not generating data,

My Controller: AccountController.php


    public function actionDynamiccities()

{

 

    $data=Yii::app()->db->createCommand('select AccountGroupID,AccountGroupName from ref_accountgroup where parent_id=:parent_id')->query(array(':parent_id'=>(int) $_POST['AccountTypeID']));


foreach($data as $row)

    $list[$row['AccountGroupID']]=$row['AccountGroupName'];

}

and my view which is: create.php


<?php

echo CHtml::dropDownList('AccountTypeID','', array(1=>'USA',2=>'France',3=>'Japan1',4=>'Japan2',5=>'Japan3'),

array(

'ajax' => array(

'type'=>'POST', //request type

'url'=>CController::createUrl('account/dynamiccities'), //url to call.

//Style: CController::createUrl('currentController/methodToCall')

'update'=>'#city_id', //selector to update

//'data'=>'js:javascript statement' 

//leave out the data key to pass all form values through

))); 

 

//empty since it will be filled by the other dropdown

echo CHtml::dropDownList('city_id','', array());

?>

So my table ref_accountgroup consists of 3 columns

AccountGroupID, AccountGroupName, AccountTypeID

I want to filter it by its AccountTypeID

just like select AccountGroupID, AccountGroupName from ref_accountgroup where AccountTypeID = ‘parent_id’;

I finally got it here is my view:


<div class="row">

            <?php echo $form->labelEx($account, 'AccountTypeName'); ?>

            <?php

            echo CHtml::dropDownList('AccountTypeName', '', array(1 => 'Administrator', 2 => 'Manager', 3 => 'Business', 4 => 'Finance', 5 => 'Customer Support'), array(

                'prompt' => '',

                'ajax' => array(

                    'type' => 'POST',

                    'url' => CController::createUrl('loadcities'),

                    'update' => '#AccountGroup',

                    'data' => array('AccountTypeName' => 'js:this.value'),

                    )));

            ?>

            <?php echo $form->error($account, 'AccountTypeName'); ?>

        </div>


        <div class="row">

            <?php echo $form->labelEx($account, 'AccountGroup'); ?>

    <?php

    echo CHtml::dropDownList('AccountGroup', '', array(), array('prompt' => ''));

    ?>

            <?php echo $form->error($account, 'AccountGroup'); ?>

        </div>

And my Controller:


public function actionLoadcities()

{

   $data=  RefAccountgroup::model()->findAll('AccountTypeID=:AccountTypeID', 

   array(':AccountTypeID'=>(int) $_POST['AccountTypeName']));

 

   $data=CHtml::listData($data,'AccountGroupID','AccountGroupName');

 

   echo "<option value=''></option>";

   foreach($data as $value=>$city_name)

   echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city_name),true);

}

It generates the second dropdown, but now my problem is I cant save the data inside my Chtml::dropdownlist, using function savedata(){

$connectionString[0] = Yii::app()->params[‘dbhandler’];

    &#036;dbhandler = new DBHandler(&#036;connectionString[0]);


    &#036;dbhandler-&gt;open();

$dbhandler->prepare(INSERT INTO data VALUES($this->AccountTypeName, $this->AccountGroup));

$dbhandler->execute();

}

oh I magaed to solve it


 <div class="row">

            <?php echo $form->labelEx($account, 'AccountTypeName'); ?>

            <?php

            echo CHtml::dropDownList('AccountForm[AccountTypeName]', '', array(1 => 'Administrator', 2 => 'Manager', 3 => 'Business', 4 => 'Finance', 5 => 'Customer Support'), array(

                'prompt' => '',

                'ajax' => array(

                    'type' => 'POST',

                    'url' => CController::createUrl('loadcities'),

                    'update' => '#AccountForm_AccountGroup',

                    'data' => array('AccountForm_AccountTypeName' => 'js:this.value'),

                    )));

            ?>

            <?php echo $form->error($account, 'AccountTypeName'); ?>

        </div>


        <div class="row">

            <?php echo $form->labelEx($account, 'AccountGroup'); ?>

    <?php

    echo CHtml::dropDownList('AccountForm[AccountGroup]', '', array(), array('prompt' => ''));

    ?>

            <?php echo $form->error($account, 'AccountGroup'); ?>

        </div>

I edit the name of my dropdown adding my current model, and its like magic, my detects my dropdown value, Thank you very much!!