Ich folgte einem Youtube Tutorial, um eine abhängige DropDownList (State und City) zu erstellen, und bald nach dem Laden zeigt die Dropdownliste City alle Städte an.Yii2 abhängige DropDownList Zustand und Stadt
Ich wünschte, keine wurden angezeigt, nur bei der Auswahl eines Staates. Aber ich bin mir nicht sicher, wie es geht, und hätte gerne Hilfe.
Schau, wie ich getan habe:
VIEW: _search.php
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model,'state_id')->dropDownList(
ArrayHelper::map(State::find()->all(), 'id', 'nome'),
[
'prompt'=>'Selecione',
'onchange'=>'
$.get("'.Url::toRoute('/city/lists').'", { id: $(this).val() })
.done(function(data) {
$("#'.Html::getInputId($model, 'city_id').'").html(data);
}
);'
]);
?>
<?= $form->field($model,'city_id')->dropDownList(
ArrayHelper::map(City::find()->all(), 'id', 'name'),
[
'prompt'=>'Selecione o estado',
]);
?>
REGLER: CityController.php
public function actionLists($id)
{
$countCity = City::find()
->where(['state_id' => $id])
->count();
$cities = City::find()
->where(['state_id' => $id])
->all();
if($countCity > 0)
{
foreach($cities as $city){
echo "<option value='".$city->id."'>".$city->name."</option>";
}
}
else{
echo "<option> - </option>";
}
}
SQL:
CREATE TABLE `city` (
`id` int(11) NOT NULL,
`name` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`state_id` int(11) DEFAULT NULL,
`population_2010` int(11) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
CREATE TABLE `state` (
`id` int(11) NOT NULL,
`name` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`sigla` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Hallo, zu sehen auf Suchseite funktioniert gut, aber auf Profilseite wird nicht gespeichert. – gugoan
Entschuldigung, ich habe Sie nicht bekommen, zeigen Sie bitte Ihren Code – Dipendra