[英]Yii Multiple Dependent Dropdown
I'm new to Yii Framework. 我是Yii Framework的新手。 I'm making multiple dependent drop downs.
我正在进行多个从属下拉列表。 Islands Regions Provinces Cities
离岛地区省份城市
I have a dropdown of Islands, when you select an Islands, Regions dropdown list will be updated and only regions that belongs to that Island will appear. 我有一个岛屿下拉列表,当您选择一个岛屿时,“地区”下拉列表将被更新,并且仅会显示属于该岛屿的地区。 Same things goes to Provinces and Cities.
省市也一样。
Now, I already finished Islands. 现在,我已经完成了离岛。 When you select an Island, only Regions that belongs to that Island will be appear.
当您选择一个岛屿时,将仅显示属于该岛屿的地区。 My problem is the second level to down.
我的问题是第二层下降。 When I select region, only provices that belongs to that region will appear.
当我选择区域时,只会显示属于该区域的服务。 I have coded it the same way and logic as the first but I'm not getting any output or errors.
我已经用与第一种相同的方式和逻辑进行了编码,但是没有任何输出或错误。 I'm using Ajax.
我正在使用Ajax。 Can you help me?
你能帮助我吗? Thanks
谢谢
Here's my VIEW 这是我的观点
<div class="row">
<?php echo $form->labelEx($model,'island'); ?>
<?php
echo $form->dropDownList($model,'island',CHtml::listData(Islands::model()->findAll(), 'IslandID', 'IslandName'),
array(
'prompt'=>'Select Island',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadRegions'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#region', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('IslandID'=>'js:this.value'),
))
);
?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'region'); ?>
<?php
/*echo $form->dropDownList($model,'region',CHtml::listData(Regions::model()->findAll(), 'RegionID', 'RegionName'),
array('class'=>'span4 chosen','maxlength'=>20)*/
echo CHtml::dropDownList('region','', array(), array('prompt'=>'Select Region'),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadProvinces'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#province', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('region'=>'js:this.value'),
))
);
?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'province'); ?>
<?php
echo CHtml::dropDownList('province','', array(), array('prompt'=>'Select Province'));
?>
</div>
Here's my Controller 这是我的控制器
/* Function for dependent dropdown */
public function actionLoadRegions()
{
$data=Regions::model()->findAll('IslandID=:IslandID',
array(':IslandID'=>(int) $_POST['IslandID']));
$data=CHtml::listData($data,'RegionID','RegionName');
foreach($data as $value=>$region)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($region),true);
}
public function actionLoadProvinces()
{
//var_dump($_POST['region']);
$data=Provinces::model()->findAll('RegionID=:RegionID',
array(':RegionID'=>(int) $_POST['RegionID']));
$data=CHtml::listData($data,'ProvinceID','ProvinceName');
foreach($data as $value=>$province)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($province),true);
}
I just followed this but on Multiple Dropdowns, seems it doesn't work? 我只是遵循了这一点,但在“多个下拉列表”中,似乎不起作用? Thanks!
谢谢!
Try 尝试
<?php echo CHtml::activeDropDownList($model,'region',array(),array(prompt'=>'Select Region'),'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadProvinces'), //or $this->createUrl('loadcities') if '$this' extends CController
'data'=>array('region'=>'js:this.value'),
'update'=>'#'.CHtml::activeId($model,'province'),
)
)); ?>
Also 也
<?php echo CHtml::activeDropDownList($model,'province', array(),array('prompt'=>'Choose Province')); ?>
Example program, similar to your requirement. 示例程序,类似于您的要求。
Below are Islands , Regions , Provinces tables 以下是岛屿 , 地区 , 省份表
Islands table 离岛表
Islands
-------
IslandID
IslandName
Regions table 区域表
Regions
---------
RegionID
RegionName
IslandID
Provinces table 省份表
Provinces
----------
ProvinceID
ProvinceName
IslandID
RegionID
Assume that i'm programming user registration. 假设我正在编程用户注册。 So,
$model
is an instance/object of User
model 因此,
$model
是User
模型的实例/对象
My View 我的观点
Select Island
<?php
echo $form->dropDownList($model,'IslandID',CHtml::listData(Islands::model()->findAll(), 'IslandID', 'IslandName'),
array(
'prompt'=>'Select Island',
'ajax' => array(
'type' => 'POST', //My method type
'url' => CController::createUrl('myController/LoadRegions'), //This is my request/ajax URL
array('IslandID'=>'js:this.value'), //I'm passing the selected dropdonw value.
'dataType' => 'JSON',
'success'=>'js:function(data)' //The functionaliy after success
. '{'
. ' var html="";'
. ' $.each(data,function(i,obj)'
. ' {'
. ' html+="<option value=obj.RegionID>"+obj.RegionName+"</option>"'
. ' });'
. ' $("#User_RegionID").html(html);' //ID of regions dropdown list
. '}'
)));
);
?>
Select Region
<?php
echo CHtml::dropDownList($model,'RegionID', array(), array('prompt'=>'Select Region'));
?>
For this dropDownlist id will be generated as User_RegionID. 为此,dropDownlist ID将作为User_RegionID生成。 ie, A feild id will be generated with combination of Model name and field name centering with underscore "_".
即,将使用以下划线“ _”为中心的模型名称和字段名称的组合来生成领域ID。
EX: EX:
Controller Method 控制器方式
public function actionLoadRegions()
{
$IslandID=$_POST['IslandID'];
$criteria=new CDbCriteria();
$criteria->select=array('RegionID,RegionName');
$criteria->condition='IslandID='.$IslandID;
$criteria->order='RegionName';
$RegionsAry= Regions::model()->findAll($criteria);
$ary=array();
foreach($RegionsAry as $i=>$obj)
{
$ary[$i]['RegionID']=$obj->RegionID;
$ary[$i]['RegionName']=$obj->RegionName;
}
echo json_encode($ary);
}
To get the Provinces list on selecting Regions 获取选择地区的省列表
Select Region
<?php
echo CHtml::dropDownList($model,'RegionID', array(),
array(
'prompt'=>'Select Region'
'ajax' => array(
'type' => 'POST', //My method type
'url' => CController::createUrl('myController/LoadProvinces'), //This is my request/ajax URL
array('RegionID'=>'js:this.value'), //I'm passing the selected dropdonw value.
'dataType' => 'JSON',
'success'=>'js:function(data)' //The functionaliy after success
. '{'
. ' var html="";'
. ' $.each(data,function(i,obj)'
. ' {'
. ' html+="<option value=obj.ProvinceID>"+obj.ProvinceName+"</option>"'
. ' });'
. ' $("#User_ProvinceID").html(html);' //ID of Province dropdown list
. '}'
));
?>
Select Provinces
<?php
echo CHtml::dropDownList($model,'ProvinceID', array(), array('prompt'=>'Select Province'));
?>
Controller method to get all Provinces 获取所有省份的控制器方法
public function actionLoadProvinces()
{
//To your task
}
You have no problem in controller file. 您在控制器文件中没有问题。 I think you have problem in view file .
我认为您在查看文件时遇到问题。 Especially, in your Select Region Part .
特别是在“ 选择区域”中 。 Here is my updated code of your view file.
这是我更新的查看文件代码。 Please delete 1 argument array() and check it.
请删除1个参数array()并进行检查。
<div class="row">
<?php echo $form->labelEx($model,'region'); ?>
<?php
echo $form->dropDownList($model,'region',CHtml::listData(Regions::model()->findAll(), 'RegionID', 'RegionName'),
array('class'=>'span4 chosen','maxlength'=>20)*/
echo CHtml::dropDownList('region','', array('prompt'=>'Select Region'),**//Here i remove array() and it works.**
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadProvinces'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#province', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('region'=>'js:this.value'),
))
);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.