php - 如何基于下拉列表创建动态复选框列表
问题描述
我想根据 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' => ''];
}
解决方案
推荐阅读
- c# - .net core C# 在 EF Core 数据库上使用动态属性名称首先生成的模型类
- javascript - 如何在 React 中动态更改正文背景颜色?
- google-analytics - 链接点击的 Google 事件跟踪代码面临问题
- php - PHP 7.3.4 的打印机列表
- javascript - 非功能 Javascript 代码块中的返回值
- javascript - 最后一个键值在javascript中追加新对象列表
- java - 使用 NetworkCallback 而不是 NetworkInfo
- r - 如何在 bookdown / knitr 文档中使用 R 函数生成脚注 URL 链接?
- c# - 通过代码创建新用户时在 AD 中填充自定义属性
- python-3.x - 使用python在whatsapp中将多行文本作为单个消息发送