Amigos o que fiz de errado que o meu dropDownList não carrega. Seguindo a sugestão do Newerton fiz assim:
_model
class setorsub extends CActiveRecord {
public static function listSubSetores($idsetor) {
$setores = array();
$models = self::model()->findAll('setor=:setor ORDER BY nome ASC', array(':setor' => $idsetor));
foreach ($models as $model) {
$setores[$model->idsetorsub] = $withName ? $model->nome : $model->nome;
}
return $setores;
}
}
_Controller
public function actionSubsetor() {
$subs = setorsub::model()->findAll('setor = :setor', array(':parent' => $_POST['idsetores']));
$subs = CHtml::listData($subs, 'idsetorsub', 'nome');
echo CHtml::tag('option', array('value' => ''), CHtml::encode('Selecione'), true);
foreach ($subs as $value => $name) {
echo CHtml::tag('option', array('value' => $value), $name, true);
}
}
_form
<div class="control-group">
<div class="controls">
<?php echo $form->labelEx($model, 'destino'); ?>
<?php
echo $form->dropDownList($model, 'destino', setores::listSetores(), array('class' => 'span12', 'empty' => ' ', 'title' => 'Selecione o Setor',
'ajax' => array(
'type' => 'POST', //request type
//'dataType' => 'json',
'url' => Yii::app()->createUrl('setorsub/subsetor'),
'success' => 'function(data){$("select#ocorrencias_setorsub").html(data);}',
'data' => array('destino' => 'js:$(this).val()')
)));
?>
<?php echo $form->error($model, 'destino'); ?>
</div>
</div>
<div class="control-group">
<div class="controls">
<?php echo $form->labelEx($model, 'setorsub'); ?>
<?php echo $form->dropDownList($model, 'setorsub', CHtml::listData(setorsub::listSubSetores($model->destino), 'idsetorsub', 'nome'), array('class' => 'span12', 'empty' => ' ', 'title' => 'Selecione o Setor')); ?>
<?php echo $form->error($model, 'setorsub'); ?>
</div>
</div>
Como isso todos os setores são mostrados, porém os SubSetores nada aparecem. Como posso resolver isto??
