如何基于下拉列表创建动态复选框列表

时间:2018-04-26 10:19:21

标签: php mysql json drop-down-menu yii2

我想基于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' => ''];
}

0 个答案:

没有答案