我可以在 yii2 中创建一个依赖下拉列表吗?
我有两张桌子:
'id','name_country"
'id','name_city','country_id'
并在我的模型中有两种方法:
public function getCountryList()
{
$models = NetCountry::find()->asArray()->all();
return ArrayHelper::map($models, 'id', 'country_name');
}
和
public function getCityList($parent_id) {
$models = \common\models\City::find()->where(['parent_id' => $country_id])->asArray()->all();
return ArrayHelper::map($models, 'id', 'name_city','country_id');
}
我有第一个字段:
<?= $form->field($model, 'country')->dropDownList($model->countryList),['id'=>'parent_id'];
第二个
<?= $form->field($model, 'city')->dropDownList($model->cityList);
我需要“传输”parent_id
到控制器并city_list
通过 AJAX(使用 JSON)返回。
我怎样才能做到这一点?我在 Yii1 中看到了一个例子,但是 Yii2 呢?