0

in view page i have 2 dropdown

<?php
$list = CHtml::listData($category, 
                'cid', 'cname');    
echo $form->dropDownList($model,'cid',$list,
              array('empty' => '(Select a category)')); 
?> 
<label>Sub Category *</label>
<?php  
$subcategory = array();
echo $form->dropDownList($model,'sid',$subcategory,
              array('empty' => '(Select a subcategory)')); 
?> 

Using value from category i have to change subcategory values

Here is my ajax function

<script type="text/javascript">
    $(function (){ 
        $("#Product_cid").change(function (){             
            var cid = $('#Product_cid').val();
            var path = "<?php echo $this->createUrl('admin/mysubcategory') ?> ";
            $.ajax({  
            type: "POST",      
            url: path, //url to be called
            data: { cid: cid } //data to be send 
            }).done(function( msg ) {
                 alert(msg)
                 $("#Product_sid").val("msg");
                 $("#Product_sid").selectmenu('refresh'); 
.
             });

        });
    });
</script>

And here is my controller

public function actionMysubcategory()
{
        $cid = Yii::app()->request->getPost('cid'); 
        $subcategory= Subcategory::model()->findAll(
                array('order'=>'sid',
               'condition'=>'cid=:cid', 
               'params'=>array(':cid'=>$cid)));
            $list = CHtml::listData($subcategory, 
            'sid', 'sname');    

        print_r($list);
} 

enter image description here

I gets list from controller. How can make into a dropdown???

1
  • what and How does the response look like? alert(msg)... Commented Feb 16, 2015 at 7:05

2 Answers 2

1

In Your controller you can

public function actionMysubcategory(){
    $model = new YourModel();
    $cid = Yii::app()->request->getPost('cid'); 
    $subcategory= Subcategory::model()->findAll(
            array('order'=>'sid',
           'condition'=>'cid=:cid', 
           'params'=>array(':cid'=>$cid)));
        $list = CHtml::listData($subcategory, 
        'sid', 'sname');
       echo CHtml::activeDropDownList($model,'sid',$list);
    } 

In your view you can do something like this

<?php
       $list = CHtml::listData($category, 
            'cid', 'cname');    
       echo $form->dropDownList($model,'cid',$list,
             array('empty' => '(Select a category)')); 
?> 
<label>Sub Category *</label>

 <div id="sub-category-wrapper">
 <?php  
      $subcategory = array();
      echo $form->dropDownList($model,'sid',$subcategory,
          array('empty' => '(Select a subcategory)')); 
  ?>
</div>

Then in your ajax

<script type="text/javascript">
$(function (){ 
    $("#Product_cid").change(function (){             
        var cid = $('#Product_cid').val();
        var path = "<?php echo $this->createUrl('admin/mysubcategory') ?> ";
        $.ajax({  
        type: "POST",      
        url: path, //url to be called
        data: { cid: cid }, //data to be send
        success: function( response ) {
             $('#sub-category-wrapper').html(response);
         }
        })

     });
 });

Sign up to request clarification or add additional context in comments.

Comments

0

May be it is problem with jQuery slectmenu ui. Check where ajax return value is correct.

And try

$('#Product_sid').val(msg).selectmenu('refresh',true);

Static version to select 'ttttt' option

//16 is representing 'ttttttt' in your option stack.
$('#Product_sid').val(16).selectmenu('refresh',true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.