Yii2: Using Kartik Depdrop Widget?

梦想与她 提交于 2019-12-02 03:12:18

Ok I found the solution, All the code is ok, actually the depdrop widget looks for the pair id and name like:

// the getSubCatList function will query the database based on the
        // cat_id and return an array like below:
        // [
        // ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
        // ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
        // ]

Accordingly I have changed the code in the model

->select(['id','city_name'])->asArray()->all();

with

->select(['id','city_name AS name'])->asArray()->all();

That's all and it is working fine now. Hope someone will find this useful.

Instead of changing the Select statement you could also:

echo $form->field($model, 'district_city')->widget(DepDrop::classname(), [
'options'=>['id'=>'district_city'], 
'pluginOptions'=>[

////  change default 'nameParam'=>'name' to

'nameParam'=>'city_name',
'depends'=>['state_name'], // the id for cat attribute
'placeholder'=>'Select...',
'url'=>  \yii\helpers\Url::to(['patient-entry/subcat'])
]
]);
  ?>

change the 'nameParam' to 'city_name'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!