php 没有 $model 的 Yii2 下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26735329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 dropdownlist WITHOUT $model
提问by Ares Draguna
I have searched the web far and wide for a solution to this problem. I already know the Yii2 dropdown way is this:
我已经在网上广泛搜索了这个问题的解决方案。我已经知道 Yii2 的下拉方式是这样的:
<?php
use yii\helpers\ArrayHelper;
use backend\models\Standard;
?>
<?= Html::activeDropDownList($model, 's_id',
ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
But I want to make the dropdown without the $model
... Is there ANY way to do this?
但我想在没有$model
...的情况下制作下拉菜单,有没有办法做到这一点?
Thank you in advance!
先感谢您!
回答by Barry
You can also use
你也可以使用
Html::dropDownList()
Html::dropDownList()
<?= Html::dropDownList('s_id', null,
ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
See Yii Manual
参见Yii 手册
回答by Insane Skull
You can also use this:
你也可以使用这个:
public function getAll()
{
$get = Standard::find()->all();
$result = ArrayHelper::map($get, 'id', 'name');
return $result;
}
Then dropdown:
然后下拉:
<?= Html::dropDownList(Standard::getAll(), ['prompt' => '--- select ---']) ?>
This will solve your error.
这将解决您的错误。