简体   繁体   English

yii2 bootstrap从模型下拉列表

[英]yii2 bootstrap dropdown from model

Been at this for some time. 在这里住了一段时间。 I am trying to set-up a drowndown box. 我正在尝试设置一个溺水箱。 I can't get to fully understand how I should fill it... Here's the link of the documentation: Dropdown . 我无法完全理解应如何填充...这是文档的链接: Dropdown

//$chapters[chapter['id','title','parent_id']]
echo Dropdown::widget([
'items'=>$chapters,
'options'=>'',
]);

When I try this I need a label, but when I add a label, Yii says it doesn't recognize label. 尝试此操作时,我需要一个标签,但是当我添加标签时,Yii说它无法识别标签。

I just can't get it to work and since yii2 is pretty new it is hard to find examples online. 我只是无法使它正常工作,而且由于yii2很新,因此很难在线找到示例。

echo Dropdown will never show you a label. echo Dropdown将永远不会显示标签。 Either use an activeField or manually show it using echo Html::label() , You can read more about the params for the label here: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#label()-detail 使用activeField或使用echo Html::label()手动显示它,您可以在此处阅读有关标签参数的更多信息: http : //www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html #label() -细节

You usually can use an activeField, or at least active models 您通常可以使用activeField或至少使用活动模型

I found the following solution: 我发现以下解决方案:

echo Html::activeDropDownList($model, 'id',
  ArrayHelper::map(Chapter::find()->all(), 'id', 'title'));

I am not using the DropDown::Widget, but this does (exactly?) the same thing. 我没有使用DropDown :: Widget,但这确实(完全相同)。 Next step it to give the ArrayHelper from the Controller, instead of getting from the database directly from the view. 下一步是从Controller提供ArrayHelper,而不是直接从视图中获取数据库。 But for now this works. 但是目前这可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM