I have working on create dynamic dropdowns (many to many relation) code. My problem is that On Update 2nd and 3rd dynamic dropdown does not show saved values/ I cannot update.
Base structure/classes had been created with gii.
Relations are following:
Table ticket has fields for sector, company, person. Sector has many to many relation with Company, and company has many to many relation with person.
for design of relations look @ attached file.
_form.php
<div class="row">
<?php echo $form->dropDownList($model, 'ticket_sector', CHtml::listData(Sector::model()->findAll(), 'idsector', 'sector_name'),array(
'empty'=>'Select sector',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('ticket/dynamiccompanies'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'ticket_requestor_company'), //selector to update
'data'=>array('ticket_sector'=>'js:this.value'),
))); ?>
<?php echo $form->error($model,'ticket_sector'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ticket_requestor_company'); ?>
<?php
echo $form->dropDownList($model, 'ticket_requestor_company',
//CHtml::tag('option', array('value'=>$value),CHtml::encode($person_surname),true);
array(), array(
'empty'=>'Select sector',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('ticket/dynamicemployees'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'ticket_requestor'), //selector to update
'data'=>array('ticket_requestor_company'=>'js:this.value'),
//'success'=> 'function(data) { $("ticket_requestor").empty(); $("ticket_requestor").append(data); } ',
)));
?>
<?php echo $form->error($model,'ticket_requestor_company'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ticket_requestor'); ?>
<?php echo $form->dropDownList($model,'ticket_requestor',array(), array('prompt'=>'Select company')); ?>
<?php echo $form->error($model,'ticket_requestor'); ?>
</div>
My controller => TicketController.php
public function actionDynamiccompanies()
{
$data= Sector::model()->findByPk((int) $_POST['ticket_sector']);
$data= $data->companies;
$dataArray = CHtml::listData($data, 'idcompany', 'company_name');
echo CHtml::tag('option', array('value'=>'0'),CHtml::encode("Select company"),true);
foreach ($dataArray as $value=>$company_name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($company_name),true);
}
}
public function actionDynamicemployees()
{
$pers= Company::model()->findByPk((int) $_POST['ticket_requestor_company']);
$data = $pers->people;
$dataArray = CHtml::listData($data, 'idperson', 'person_surname');
echo CHtml::tag('option', array('value'=>'0'),CHtml::encode("Select employee"),true);
foreach ($dataArray as $value=>$person_surname)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($person_surname),true);
}
}
Any suggestions are much appreciated.
Thanks in advance for your help.