## 多级联动
* https://github.com/kartik-v/dependent-dropdown
* 在view文件form中
~~~
<?php echo $form->field($model, 'brand_id')
->dropDownList(\yii\helpers\ArrayHelper::map(
\common\models\WxBrand::find()
->where(['client_id' => $gh->client_id])->orderBy('sort_order desc')->all(),
'id',
'name'
), ['prompt'=>'选择品牌...', 'id'=>'brand_id'])->label('品牌'); ?>
<?php echo $form->field($model, 'model_id')->label('型号')
->widget(\kartik\depdrop\DepDrop::classname(), [
'options' => ['id' => 'model_id', 'class'=>'', 'style'=>''],
'pluginOptions'=>[
'depends'=>['brand_id'],
'placeholder' => '机型...',
'initialize' => $model->isNewRecord ? false : true,
'url' => Url::to(['/sj-policy/model-subcat']),
]
]); ?>
~~~
* 在controll中, 当下拉框change时对应的动作, 注意返回的json(字段必须是'id', 'name')
~~~
[
0 => ['id' => 1, 'name' => 'SONY-1'],
1 => ['id' => 2, 'name' => 'SONY-2'],
...
]
public function actionModelSubcat()
{
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$parent_id = $parents[0];
//$out = \common\models\SjPhoneModel::find()->select(['id', 'name'])
->where(['brand_id' => $parent_id])->orderBy(['sort_order' => SORT_DESC])->asArray()->all();
$out = \common\models\SjPhoneModel::find()->select(['id', 'title as name'])
->where(['brand_id' => $parent_id])->orderBy(['sort_order' => SORT_DESC])->asArray()->all();
return \yii\helpers\Json::encode(['output'=>$out, 'selected'=>empty($out) ? '' : $out[0]['id']]);
}
}
return \yii\helpers\Json::encode(['output'=>'', 'selected'=>'']);
}
~~~