Yii Dropdown List With Json

I SOLVED my task. I am using CJuiAutocomplete and dependable dropdownlist according to aucomplete search

This code is too much user friendly. can be use at any place in yii / php application. I used ajax and json to solve the issue.

My _form::

==========




<script type="text/javascript">

    function myFunction(){

        var str = document.getElementById("selectid").value;

        document.getElementById("selectid").value = str.split("-",1);

        addCodeid();

    }


    function addCodeid(){


            var id= $('#selectid').val();

            //alert(id);

            $.ajax({

                url: "<?php echo Yii::app()->createUrl('requisitiondt/loadunit') ?>"+'/'+id,

                //type: 'GET',

                dataType: "json"

            }).success(function(data){


                    //$('#data').append(JSON.stringify(data));

                    //console.log(data);

                    //return false;


                    $.each(data, function(i, value) {

                        console.log(value.cm_purunit);

                       $('#unitid').append($('<option>').text(value.cm_purunit).attr('value', value.cmpurunit));

                    });


                });

            return false;

        }




    </script>




<div class="row" >

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

        <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

            'name'=>'cm_name',

            'id' =>'selectid',

            'model'=>$model, //Model object

            'attribute'=>'cm_code', //attribute name

            'sourceUrl' => Yii::app()->createUrl('requisitiondt/Acomplete'),

            //'source' => array_keys(CHtml::listData(Productmaster::model()->findAll(array('select' => 'cm_code')), 'cm_name', 'cm_code')),


            'options'=>array(

                'minLength'=>'1',

                'showAnim'=>'fold',

                'type' => 'get',


            ),


            'htmlOptions'=>array(

                'class'=>'input-1',

                'onblur' => "myFunction()",

            ),

        )); ?>


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

</div>




<div class="row">

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


    <?php echo $form->dropDownList($model,'pp_unit', array('prompt'=> 'Select Unit'), 

        array('class'=>'unitclass', 'id'=>'unitid')


    );?>


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

</div>



My Controller::

==============




public function actionLoadUnit($id) {




            //echo $id;

            //exit();

            //$q = 'SELECT cm_purunit,cm_code AS value FROM cm_productmaster WHERE cm_code LIKE ?';


            $q = 'SELECT cm_purunit FROM cm_productmaster WHERE cm_code LIKE ?';

            $cmd = Yii::app()->db->createCommand($q);

            $result = $cmd->query(array('%'.$_GET['id'].'%'));

            $data = array();

                foreach ($result as $row) {

                $data[] = $row;

            }


            echo CJSON::encode($data);

            Yii::app()->end();


        //echo $id;

        //exit();


    }



Enjoy this code. This is really important for any application

Hi,

This may be help

json to dropdown

I have updated my code :: Now it is showing the Dropdown list but not getting any value in dropdown it show [object Object] instead of [{"cm_purunit":"22"}]

did u implement json encode /decode ?if not then u need to implement it.

How json works between clien / server , u will have it in google search.

I SOLVED my task. I am using CJuiAutocomplete and dependable dropdownlist according to aucomplete search

This code is too much user friendly. can be use at any place in yii / php application. I used ajax and json to solve the issue.

My _form::

==========




<script type="text/javascript">

    function myFunction(){

        var str = document.getElementById("selectid").value;

        document.getElementById("selectid").value = str.split("-",1);

        addCodeid();

    }


    function addCodeid(){


            var id= $('#selectid').val();

            //alert(id);

            $.ajax({

                url: "<?php echo Yii::app()->createUrl('requisitiondt/loadunit') ?>"+'/'+id,

                //type: 'GET',

                dataType: "json"

            }).success(function(data){


                    //$('#data').append(JSON.stringify(data));

                    //console.log(data);

                    //return false;


                    $.each(data, function(i, value) {

                        console.log(value.cm_purunit);

                       $('#unitid').append($('<option>').text(value.cm_purunit).attr('value', value.cmpurunit));

                    });


                });

            return false;

        }




    </script>




<div class="row" >

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

        <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

            'name'=>'cm_name',

            'id' =>'selectid',

            'model'=>$model, //Model object

            'attribute'=>'cm_code', //attribute name

            'sourceUrl' => Yii::app()->createUrl('requisitiondt/Acomplete'),

            //'source' => array_keys(CHtml::listData(Productmaster::model()->findAll(array('select' => 'cm_code')), 'cm_name', 'cm_code')),


            'options'=>array(

                'minLength'=>'1',

                'showAnim'=>'fold',

                'type' => 'get',


            ),


            'htmlOptions'=>array(

                'class'=>'input-1',

                'onblur' => "myFunction()",

            ),

        )); ?>


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

</div>




<div class="row">

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


    <?php echo $form->dropDownList($model,'pp_unit', array('prompt'=> 'Select Unit'), 

        array('class'=>'unitclass', 'id'=>'unitid')


    );?>


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

</div>



My Controller::

==============




public function actionLoadUnit($id) {




            //echo $id;

            //exit();

            //$q = 'SELECT cm_purunit,cm_code AS value FROM cm_productmaster WHERE cm_code LIKE ?';


            $q = 'SELECT cm_purunit FROM cm_productmaster WHERE cm_code LIKE ?';

            $cmd = Yii::app()->db->createCommand($q);

            $result = $cmd->query(array('%'.$_GET['id'].'%'));

            $data = array();

                foreach ($result as $row) {

                $data[] = $row;

            }


            echo CJSON::encode($data);

            Yii::app()->end();


        //echo $id;

        //exit();


    }



Enjoy this code. This is really important for any application