zone de liste déroulante dépendante avec Yii
-
28-09-2019 - |
Question
J'ai un pays, états, tableaux de villes, et j'ai besoin 3 menus déroulants dans lequel, si je sélectionne un pays comme par exemple aux États-Unis, les états Menu déroulant affichera automatiquement que les États qui sont en États-Unis, et à côté, si je sélectionne un Etat comme par exemple la Californie, le menu déroulant Villes vers le bas ne montrera au large de la villes sous Californie
am avoir actuellement problème dans la mise en œuvre de cette yu. J'ai ce fichier _form où ces 3 menu déroulant devrait être inclus. J'ai un code partiel, mais je ne peux pas comprendre comment faire ce travail sur
est du contrôleur
public function actionDynamicstates()
{
$sql = "SELECT StateName FROM gg_t_worldareasstates ".
"WHERE CountryID = :countryid";
$command = Yii::app()->createCommand($sql);
$command->bindValue(':countryid', $_POST['CountryID'], PDO::PARAM_INT);
$data = $command->execute();
$data = CHtml::listData($data,'StateID','StateName');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
est à partir du fichier de vue _form
<div class="row">
<?php
$country = new CDbCriteria;
$country->order = 'CountryName ASC';
echo $form->dropDownList($model, 'CountryID',
CHtml::listData
(
Worldareascountries::model()->findAll($country),
'CountryID',
array
(
'ajax' => array
(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
'update' => '#StateID',
)
)
)
);
echo $form->dropDownList('StateID','', array());
?>
</div>
J'ai changé que ces codes ci-dessus dans celui-ci
<div class="row">
<?php echo $form->labelEx($model,'Country'); ?>
<?php
$country = new CDbCriteria;
$country->order = 'CountryName ASC';
?>
<?php
echo $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID','CountryName'),
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
'update' => "#StateID"
)
)
);
?>
<?php echo $form->error($model,'CountryID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'State'); ?>
<?php
$state = new CDbCriteria;
$state->order = 'StateName ASC';
?>
<?php
echo $form->dropDownList($model,'StateID',CHtml::listData(Worldareasstates::model()->findAll($state),'StateID','StateName'),
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamiccities'),
'update' => '#CityID'
)
)
);
?>
<?php echo $form->error($model,'StateID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'CityID'); ?>
<?php echo $form->dropDownList($model,'CityID','',array());?>
<?php echo $form->error($model,'CityID'); ?>
La solution 2
ce problème a été résolu, il est sur le wiki yu docs
Autres conseils
modifier cette ligne
$ Data = $ command-> execute ();
à
$ data = $ command-> query ();