2017-07-03 74 views
0

我有关于公民的国家和城市的数据。这些数据使用js显示在下拉列表中。当我尝试更新它们时,下拉列表在保存之前不显示数据。如何解决它。 我的代码是:如何在下拉列表中设置值yii2

<?= $form->field($organization, 'country_id')->dropDownList([],[ 
           'prompt' => '---', 
           'onchange' => ' 
            $.get("../citizeninfo/city?id='.'"+$(this).val(), function(data){ 
            $("select#training-city_id").html(data); 
            });' 
            ]) ?> 

<?= $form->field($organization, 'city_id')->dropDownList([], ['prompt' => '---']) ?> 

回答

0

您可以用这种方式:

城市模型

public static function getCityByCountry($countryId) 
{ 
    $cities = self::find() 
     ->where(['country_id' => $countryId]) 
     ->select(['name']) 
     ->indexBy('id') 
     ->orderBy(['name' => SORT_ASC]) 
     ->column(); 

    return $cities; 
} 

form.php的

<?= $form->field($organization, 'city_id')->dropDownList(City::getCityByCountry($organization->country_id), ['prompt' => '---']) ?> 

country_id做同样的事情。
你也可以在你的jQuery中调用相同的函数来获取所选国家的城市。

0

集Vaue:

<?= $form->field($model, 'sex')->dropDownList(
      ['0' => 'Male', 
      '1' => 'Female'] 
    ) ?> 
相关问题