Yii2 kartik依赖下拉列表不会加载下一个下拉列表

时间:2018-05-25 16:54:04

标签: yii2 yii2-advanced-app kartik-v

我正在使用kartik DepDropdown小部件。当我选择区域时,它必须在特定的下拉列表中加载所有城市(例如ID为"include_dirs": [ "../../src", "/usr/local/include/RF24" ], "libraries": [ "/usr/local/lib/librf24.so.1.3.1" ] 的城市)。 在chrome网络选项卡中,我可以看到该操作返回所有城市的预期结果json格式

city_id

但是他们没有加载{ "output": { "40": "Велико Търново", "41": "Горна Оряховица", "42": "Елена", "43": "Златарица", "44": "Лясковец", "45": "Павликени", "46": "Полски Тръмбеш", "47": "Свищов", "48": "Стражица", "49": "Сухиндол" }, "selected": "" } 下拉列表。我的观点如下:

#city_id

我的控制器:

<div class="col-sm-6">

                                <?= $form->field($model, 'region_id')->dropDownList(Region::getAllRegions(), ['id' => 'region-dd', 'prompt'=> ' - ' . Yii::t('app', 'Region') . ' - ']); ?>

                            </div>
                            <div class="col-sm-6">

                                <?= $form->field($model, 'city_id')->widget(DepDrop::classname(), [
                                    'options'=>[
                                        'id'=>'city-id'
                                    ],
                                    'pluginOptions'=>[
                                        'allowClear' => true,
                                        'depends' => ['region-dd'],
                                        'url' => Url::to(['/system-information/load-cities'])
                                    ]
                                ]); ?>

                            </div>

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您必须提供name=>value idtext[ 'out'=>[ ['id'=>'<city-id>', 'name'=>'<city-name>'], ['id'=>'<city-id>', 'name'=>'<city-name>'] ], 'selected'=>'<city-id>' ] 分配给单个选项,如果您查看DOCS,您会看到你必须像下面那样返回数组

id

表示将为id提供索引name,下拉列表的文本将提供索引getAllCities,并查看您提供的方法{{1返回数组如下

 [
    'id'=>'name',
    'id'=>'name',
 ]

您没有添加返回City::getAllCities($region_id);数组的方法$out,但在最基本的级别上,根据文档应该如下所示,

注意:分别更改tablecolumn名称,我假设您拥有城市的idname列,如果您有任何其他城市名称的列名称如city_name,然后您必须为查询中的name字段创建别名city_name

public function getAllCities($region_id){

    $query = new \yii\db\Query;
    $query->select('id, name')
        ->from('{{%city}}')
        ->where(['=', 'region_id', $region_id])
        ->limit(20);
    $command = $query->createCommand();
    return $command->queryAll();

}

这将返回如下所示的数组。

Array
(
    [0] => Array
        (
            [id] => 40
            [name] => Велико Търново
        )

    [1] => Array
        (
            [id] => 41
            [name] => Горна Оряховица
        )

    [2] => Array
        (
            [id] => 42
            [name] => Елена
        )

    [3] => Array
        (
            [id] => 43
            [name] => Златарица
        )

)

将编码为JSON,如下所示

[
  {
    "id": "40",
    "name": "Велико Търново"
  },
  {
    "id": "41",
    "name": "Горна Оряховица"
  },
  {
    "id": "42",
    "name": "Елена"
  },
  {
    "id": "43",
    "name": "Златарица"
  }
]

并最终在按行编码时

echo Json::encode(['output'=>$out, 'selected'=>'']);

它会返回

{
  "output": [
    {
      "id": "40",
      "name": "Велико Търново"
    },
    {
      "id": "41",
      "name": "Горна Оряховица"
    },
    {
      "id": "42",
      "name": "Елена"
    },
    {
      "id": "43",
      "name": "Златарица"
    }
  ],
  "selected": ""
}

希望这有帮助。