Hello everyone, I am new in Yii and I need your help. I have a model called ‘Share’ and another one is ‘Usergroup’ which links only two different model ‘User’ and ‘Group’ by userId and groupId. However. I want to load a dropdownlist in the ‘Share’ view from the ‘Usergroup’ table/model using a condition which I have done. But now I have to load another dropdown/listdata (userid) based on input of first dropdown(groupId)from the table ‘usergroup’ but its not working. Please help me:
table:
Share: id, userId(fk), fileId(fk), …
Usergroup: id, userId(fk), groupId(fk) , …
Code:
View: Share : _form.php
…
<div class="row">
<?php echo $form->labelEx($model,'Groups'); ?>
<?php
$id_tmp = Yii::app()->session['id'];
echo $form->dropDownList($model,'groupId',CHtml::listData(Usergroup::model()->findAll(array('condition'=>"userId='$id_tmp'")), 'id', 'groupId'), array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('ShareCon'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
//'update'=>'#' . CHtml::activeId($model, 'userId'), //selector to update
'update'=>'#userId'
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
))); ?>
<?php echo $form->error($model,'Groups'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Users'); ?>
<?php echo $form->dropdownList($model,'userId',array()); ?>
<?php echo $form->error($model,'Users'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'fileId'); ?>
<?php //echo $form->textField($model,'fileId'); ?>
<?php
echo Yii::app()->session['fileid'];
?>
<?php echo $form->error($model,'fileId'); ?>
</div>
…
model: share.php
public $groupId, $grouid_1;
Controller: ShareController.php
public function actionShareCon()
{
$data=Usergroup::model()->findAll('id=:id',
array(':id'=>(int) $_POST['Share']['groupId']));
$data=CHtml::listData($data,'id','userId');
foreach($data as $value=>$userid)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($userId),true);
}
}