[英]How to make a drop down list in yii2?
How to make a dropdown
in yii2
using an activeform
and a model?如何使用
activeform
和模型在yii2
创建dropdown
? Since all the methods has changed in yii2
,how it is done in the new one?既然
yii2
所有方法都发生了yii2
,那么在新的方法中是怎么做的呢?
It is like它像是
<?php
use yii\helpers\ArrayHelper;
use backend\models\Standard;
?>
<?= Html::activeDropDownList($model, 's_id',
ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
ArrayHelper in Yii2 replaces the CHtml list data in Yii 1.1.[Please load array data from your controller] Yii2 中的 ArrayHelper 替换了 Yii 1.1 中的 CHtml 列表数据。 [请从您的控制器加载数组数据]
EDIT
编辑
Load data from your controller.从控制器加载数据。
Controller控制器
$items = ArrayHelper::map(Standard::find()->all(), 's_id', 'name');
...
return $this->render('your_view',['model'=>$model, 'items'=>$items]);
In View在视图中
<?= Html::activeDropDownList($model, 's_id',$items) ?>
It seems you've found your answer already but since you mentioned the active form I'll contribute with one more, even if it differs only ever so slightly.似乎您已经找到了答案,但既然您提到了活动形式,我将再贡献一个,即使它只是略有不同。
<?php
$form = ActiveForm::begin();
echo $form->field($model, 'attribute')
->dropDownList(
$items, // Flat array ('id'=>'label')
['prompt'=>''] // options
);
ActiveForm::end();
?>
There are some good solutions above, and mine is just a combination of two (I came here looking for a solution).上面有一些很好的解决方案,我的只是两个的组合(我来这里是为了寻找解决方案)。
@Sarvar Nishonboyev's solution is good because it maintains the creation of the form input label and help-block for error messages. @Sarvar Nishonboyev 的解决方案很好,因为它维护了表单输入标签和错误消息帮助块的创建。
I went with:我去了:
<?php
use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?=
$form->field($model, 'parent_id')
->dropDownList(
ArrayHelper::map(Product::find()->asArray()->all(), 'parent_id', 'name')
)
?>
Again, full credit to: @Sarvar Nishonboyev's and @ippi再次,完全归功于:@Sarvar Nishonboyev 和 @ippi
It Seems there are many good answers for this question .So i will try to give a detailed answer这个问题好像有很多很好的答案,所以我会尽量给出详细的答案
active form and hardcoded data活动形式和硬编码数据
<?php
echo $form->field($model, 'name')->dropDownList(['1' => 'Yes', '0' => 'No'],['prompt'=>'Select Option']);
?>
or或者
<?php
$a= ['1' => 'Yes', '0' => 'No'];
echo $form->field($model, 'name')->dropDownList($a,['prompt'=>'Select Option']);
?>
active form and data from a db table数据库表中的活动表单和数据
we are going to use ArrayHelper so first add it to the name space by我们将使用 ArrayHelper 所以首先将它添加到名称空间
<?php
use yii\helpers\ArrayHelper;
?>
ArrayHelper has many use full functions which could be used to process arrays map () is the one we are going to use here this function help to make a map ( of key-value pairs) from a multidimensional array or an array of objects. ArrayHelper 有许多可用于处理数组的完整函数 map () 是我们将在这里使用的函数,该函数帮助从多维数组或对象数组制作映射(键值对)。
<?php
echo $form->field($model, 'name')->dropDownList(ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
not part of a active form不是活动形式的一部分
<?php
echo Html::activeDropDownList($model, 'filed_name',['1' => 'Yes', '0' => 'No']) ;
?>
or或者
<?php
$a= ['1' => 'Yes', '0' => 'No'];
echo Html::activeDropDownList($model, 'filed_name',$a) ;
?>
not an active form but data from a db table不是活动形式,而是来自数据库表的数据
<?php
echo Html::activeDropDownList($model, 'filed_name',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
Have a look this:看看这个:
use yii\helpers\ArrayHelper; // load classes
use app\models\Course;
.....
$dataList=ArrayHelper::map(Course::find()->asArray()->all(), 'id', 'name');
<?=$form->field($model, 'center_id')->dropDownList($dataList,
['prompt'=>'-Choose a Course-']) ?>
Maybe I'm wrong but I think that SQL query from view is a bad idea也许我错了,但我认为从视图中进行 SQL 查询是个坏主意
This is my way这是我的方式
In controller在控制器中
$model = new SomeModel();
$items=ArrayHelper::map(TableName::find()->all(),'id','name');
return $this->render('view',['model'=>$model, 'items'=>$items])
And in View在视图中
<?= Html::activeDropDownList($model, 'item_id',$items) ?>
Or using ActiveForm或者使用 ActiveForm
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'item_id')->dropDownList($items) ?>
<?php ActiveForm::end(); ?>
<?= $form->field($model, 'attribute_name')->dropDownList(
ArrayHelper::map(Table_name::find()->all(),'id','field_name'),
['prompt' => 'Select']
) ?>
This will help you...Don't forget to use the class file in header.这将帮助您...不要忘记在标题中使用类文件。
In ActiveForm
just use:在
ActiveForm
只需使用:
<?=
$form->field($model, 'state_id')
->dropDownList(['prompt' => '---- Select State ----'])
->label('State')
?>
This is about generating data, and so is more properly done from the model.这是关于生成数据,因此更适合从模型中完成。 Imagine if you ever wanted to change the way data is displayed in the drop-down box, say add a surname or something.
想象一下,如果您想更改下拉框中数据的显示方式,例如添加姓氏或其他内容。 You'd have to find every drop-down box and change the
arrayHelper
.您必须找到每个下拉框并更改
arrayHelper
。 I use a function in my models to return the data for a dropdown, so I don't have to repeat code in views.我在我的模型中使用一个函数来返回下拉列表的数据,所以我不必在视图中重复代码。 It also has the advantage that I can specify filter here and have them apply to every dropdown created from this model;
它还具有我可以在此处指定过滤器并将它们应用于从此模型创建的每个下拉列表的优点;
/* Model Standard.php */
public function getDropdown(){
return ArrayHelper::map(self::find()->all(), 's_id', 'name'));
}
You can use this in your view file like this;您可以像这样在视图文件中使用它;
echo $form->field($model, 'attribute')
->dropDownList(
$model->dropDown
);
If you made it to the bottom of the list.如果你把它排到了列表的底部。 Save some php code and just bring everything back from the DB as you need like this:
保存一些 php 代码,然后根据需要从数据库中取回所有内容,如下所示:
$items = Standard::find()->select(['name'])->indexBy('s_id')->column();
Html::activeDropDownList($model, 'id', ArrayHelper::map(AttendanceLabel::find()->all(), 'id', 'label_name'), ['prompt'=>'Attendance Status']) ;
Following can also be done.也可以进行以下操作。 If you want to append prepend icon.
如果要附加前置图标。 This will be helpful.
这会很有帮助。
<?php $form = ActiveForm::begin();
echo $form->field($model, 'field')->begin();
echo Html::activeLabel($model, 'field', ["class"=>"control-label col-md-4"]); ?>
<div class="col-md-5">
<?php echo Html::activeDropDownList($model, 'field', $array_list, ['class'=>'form-control']); ?>
<p><i><small>Please select field</small></i>.</p>
<?php echo Html::error($model, 'field', ['class'=>'help-block']); ?>
</div>
<?php echo $form->field($model, 'field')->end();
ActiveForm::end();?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.