Answer the question
In order to leave comments, you need to log in
Did I pull the data from the database correctly, as indicated in the DepDrop widget?
Data does not come from the database, I work with the DepDrop widget.
It turns out I have a frontend/signup.php file
$form->field($model, 'country')->dropDownList([
'3159'=>'Россия'
], ['prompt'=>'Выберите страну'])
<?= $form->field($model, 'region')->widget(DepDrop::classname(), [
'options' => ['region_id'=>'region_id'],
'pluginOptions'=>[
'depends'=>['country_id'],
'placeholder' => 'Выберите страну',
'url'=>Url::to(['/site/region'])
]
]);
?>
<?= $form->field($model, 'city')->widget(DepDrop::classname(), [
'pluginOptions'=>[
'depends'=>['country_id', 'region_id'],
'placeholder'=>'Выберите регион',
'url'=>Url::to(['city'])
]
]);
?>
public function actionRegion() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$country_id = $parents[0];
$out = self::getRegionList($country_id);
echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
public function actionCity()
{
$out = [];
if (isset($_POST['depdrop_parents']))
{
$ids = $_POST['depdrop_parents'];
$country_id = empty($ids[0]) ? null : $ids[0];
$region_id = empty($ids[1]) ? null : $ids[1];
if ($country_id != null) {
$data = self::getCityList($country_id, $region_id);
echo Json::encode(['output'=>$data['out'], 'selected'=>$data['selected']]);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
public function getRegionList($country_id){
$model = frontend\models\Region::find()->where(['country_id'=> $country_id])->asArray()->all();
return ArrayHelper::map($model, 'region_id', 'name');
}
public function getCityList($country_id, $region_id){
$model = frontend\models\City::find()->where(['country_id'=> $country_id, 'region_id'=> $region_id])->asArray()->all();
return ArrayHelper::map($model, 'city_id', 'name');
}
namespace frontend\models;
use yii\db\ActiveRecord;
class Region extends ActiveRecord
{
}
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'', 'name'=>''],
// ['id '=>'', 'name'=>'']
// ]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question