First thing is , i hope it will help other thats all,
Suppose i have two table named: work_order and clients
work_order contains a client id which is related table of clients
Now i want a form(in work_order model) with a dropdown list containing work orders from work_order model
when i select one from the work_order dropdownlist, the associative client name (for that work_order) will be displayed in the desired location.
So, here i done this:
In my views/work_order/_form.php:
<?php echo $form->labelEx($model, 'work_order_no'); ?>
<?php
echo $form->dropDownList($model, 'work_order_no', CHtml::listData(WorkOrder::model()->findAll(), 'id', 'id'), array(
'prompt' => 'Select One',
'ajax' => array(
'type' => 'POST',
'dataType'=>'json',
'url' => CController::createUrl('WorkOrder/clientFromThis'),
'success' => 'function(data) {
$("#Work_order_client_view").html(data.client);
}',
'data' => array('work_order_no' => 'js:this.value'),
),
));
?>
<div class="total_qty_lebel_custom">Client of this work order</div>
<div id="Work_order_client_view">
</div>
and in your WorkOrder controller make a function :
public function actionClientFromThis() {
$data = WorkOrder::model()->findAll('id=:work_order_no', array(':work_order_no' => $_POST['work_order_no']));
foreach ($data as $d):
$client_name= Clients::model()->findByPk($d->client_id);
$client=$client_name->client_name;
echo $array = CJSON::encode(array(
'client' => $client,
));
endforeach;
if (!$_POST['work_order_no']) {
echo $array = CJSON::encode(array(
'client' => '',
));
}
}
Thats it, if anything i missed please leave reply, many many thanks in advance.