我想根据yii2中的下拉列表生成一个动态复选框列表。字段如下所示。
<?= $form->field($model, 'country')->dropDownList(Countries::getCountries(),
['id' => 'country_id', 'prompt' => 'Select countries'])
?>
<?= $form->field($model, 'state')->widget(DepDrop::classname(), [
//'data' => Regions::getRegions($model->country),
'options' => ['id' => 'region_id', 'prompt' => 'Choose a country first'],
'pluginOptions' => [
'depends' => ['country_id'],
'placeholder' => 'Select a state',
'url' => Url::to(['/regions/regions'])
]
]) ?>
<?= $form->field($model, 'city')->widget(DepDrop::classname(), [
//'data' => Cities::getCities($model->country),
'options' => ['id' => 'city_id', 'prompt' => 'Select a state'],
'pluginOptions' => [
'depends' => ['country_id','region_id'],
'placeholder' => 'Select',
'url' => Url::to(['/cities/cities'])
]
]) ?>
<?= $form->field($model, 'publisher_name')->widget(DepDrop::classname(), [
//'data' => Cities::getCities($model->country),
'options' => ['id' => 'user_id', 'prompt' => 'Choose a publisher'],
'pluginOptions' => [
'depends' => ['country_id','region_id','city_id'],
'placeholder' => 'Select',
'url' => Url::to(['/publisher/publishers'])
]
]) ?>
当我删除所有的下拉列表时,它正确地返回了一个json,如下所示,但是现在我要创建复选框而不是下拉列表,我如何才能确保返回的json如下所示
{"output":[{"user_id":"109","name":"user1"},{"user_id":"114","name":"user2"}],"selected":""}
控制器
public function actionPublishers() {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if (isset(Yii::$app->request->post()['depdrop_parents'])) {
$parents = Yii::$app->request->post('depdrop_parents');
if ($parents != null) {
$countryID = $parents[0];
$stateID = $parents[1];
$cityId = $parents[2];
return [
'output' => publisher::getPublishers($countryID,$stateID,$cityId, true),
'selected' => '',
];
}
}
return ['output' => '', 'selected' => ''];
}
暂无答案!
目前还没有任何答案,快来回答吧!