Issue
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);
}

I gets list from controller. How can make into a dropdown???
Solution
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);
}
})
});
});
Answered By - Rasikh Mashhadi
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.