Hi Everybody,
I have 2 areas in a form and every area has 6 service points. Areas and services points are static but dependent. I want to show the service points dropdown list depending on the area is selected.
i.e. Area 1 has 6 service points and
Area 2 has 6 service points
Now, I want to show Area 1’s service points in the service point’s dropdown once area 1 is selected otherwise area 2’s service points will be shown if area 2 is selected.
Please help me, I have tried a lot but couldn’t. Let me know if my explanation is vague.
Kind regards,
Shimul
kalpit
(Panditkalpit)
August 26, 2013, 5:44am
3
<div class="row">
<?php echo $form->labelEx($model,'areaId'); ?>
<?php echo CHtml::dropDownList('areaId','',
array('prompt'=>'Select Type','1'=>'area1','2'=>'area2','0'=>'Other'),
array('ajax' => array('type'=>'POST',
'url'=>CController::createUrl('myActionName'),
'update'=>'#service'))); ?>
<?php echo $form->error($model,'areaId'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'service'); ?>
<?php echo CHtml::dropDownList('service','',array()); ?>
<?php echo $form->error($model,'service'); ?>
</div>
and in controller make method myActionName() like this…
public function actionMyActionName()
{
$data= Service::model()->findAll('areaserviceId=:areaId',
array(':areaId'=>(int) $_POST['areaId']));
$data=CHtml::listData($data,'serviceId','serviceName');
foreach($data as $value=>$serviceName)
{
echo CHtml::tag('option',
array('value'=>$value),
CHtml::encode($serviceName),true);
}
}
hope it may help you…
Thanks Ankit and Kalpit.
I did same thing what this tutorial says and also Kalpit’s code. But it doesn’t work for me. I wrote as :
<div class="row">
<?php echo $form->labelEx($model,'service_site'); ?>
<?php
echo CHtml::dropDownList('siteId','',
array(1=>'Site 1',2=>'Site 2'),
array(
'prompt'=>'Select Site',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('servicePoint'),
'update'=>'#service_point',
'data'=>array('siteId'=>'js:this.value'),
)));
?>
<?php echo $form->error($model,'service_site'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'service_point'); ?>
<?php echo CHtml::dropDownList('service_point','',array()); ?>
<?php echo $form->error($model,'service_point'); ?>
</div>
and in controller:
public function actionServicePoint(){
$models = Sp::model()->findAll('site_id=:id',
array(':id' => (int) $_POST['siteId']));
$list = CHtml::listData($models,
'id', 'name');
return $list;
}
Please could you check please and let me know where I am doing wrong. Please help me.
Thank in advance,
kalpit
(Panditkalpit)
August 28, 2013, 8:37am
5
why you are using return $list…
you are doing ajax request from form and from function you are returning array…
i think for ajax request either you have to use echo or alert…
so use echo $list instead of return $list…
Sorry Kalpit,
I pasted a wrong code. Actual code what I tried that is,
In the form:
<div class="row">
<?php echo $form->labelEx($model,'service_site'); ?>
<?php
echo CHtml::dropDownList('siteId','',
array(1=>'Site 1',2=>'Site 2'),
array(
'prompt'=>'Select Site',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('servicePoint'),
'update'=>'#service_point',
'data'=>array('siteId'=>'js:this.value'),
)));
?>
<?php echo $form->error($model,'service_site'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'service_point'); ?>
<?php echo CHtml::dropDownList('service_point','',array()); ?>
<?php echo $form->error($model,'service_point'); ?>
</div>
In the controller:
public function actionServicePoint(){
$data = Sp::model()->findAll('site_id=:id',
array(':id' => (int) $_POST['siteId']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),
CHtml::encode($name),true);
}
}
Sorry, for this mistake. But this way does not work.
kalpit
(Panditkalpit)
August 28, 2013, 9:16am
7
i think you are making some mistake in the following code…
$data = Sp::model()->findAll('site_id=:id',
array(':id' => (int) $_POST['siteId']));
verify once this code…
because which code i have given above that i used in my project and it’s working fine for me…
just check id value which you are checking in controller method…
kalpit:
i think you are making some mistake in the following code…
$data = Sp::model()->findAll('site_id=:id',
array(':id' => (int) $_POST['siteId']));
verify once this code…
because which code i have given above that i used in my project and it’s working fine for me…
just check id value which you are checking in controller method…
Actually my ajax request does not work. I echoed something in the action method but did not find any response. Is there any way to fix it?
kalpit
(Panditkalpit)
August 28, 2013, 9:54am
9
why you kept prompt inside ajax array…
and have you given permission inside accessRules()?
kalpit
(Panditkalpit)
August 29, 2013, 5:29am
11
Glad to know
What was the issue?
Only this ajax field data is not getting saved or all fields data?
Thanks to reply. Both field’s data, calling field as well as ajax field data.
Please help me.
kalpit
(Panditkalpit)
August 29, 2013, 6:47am
13
for that write in controller create() method…
if(isset($_POST['your_controller_name']))
{
$model->siteId=$_POST['siteId'];
$model->service_point=$_POST['service_point'];
}
joblo
(Joe)
August 29, 2013, 6:59am
14
You can try cascadedropdown?
a lot of thanks Kalpit , You did a big afford for me.
I understood the logic of posting the site and service point and place them into the create action properly.
Thanks again my friend.
Yes done but thinking about for update. Could you advise me what should I do for update.
kalpit
(Panditkalpit)
August 29, 2013, 8:27am
18
same thing you can apply for update also…
$model->siteId=$_POST['siteId'];
$model->service_point=$_POST['service_point'];
write above code inside actionUpdate() method. it will work…
tulacik
(Tulacik)
August 29, 2013, 8:33am
19
Hi there,
not sure if my solution is the best (had similar problem). But you could check it:
http://www.yiiframework.com/forum/index.php/topic/46584-dynamic-dropdown-on-many-to-many-tables/
Hope code is self explaining.
Good luck
Hi Kalpit actually I am worried to show data as selected in site and service point once I created.
For example, I created a form where I selected "Site 1" in Site field and "Service Point 1" in Service point field. Now my issues is when I want to update this data, I want them as selected "Site 1" in Site field and "Service Point 1" in Service point field. At this moment, I got the fields as "create form"
Please let me know how to do this thing.
Thank you
Thanks Dulo to reply here. I am testing your reference.