如何使用一个dropdown
和一个模型制作一个?既然所有的方法都改变了,那么在新的方法中是如何完成的呢?yii2
activeform
yii2
13 回答
它像是
<?php
use yii\helpers\ArrayHelper;
use backend\models\Standard;
?>
<?= Html::activeDropDownList($model, 's_id',
ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
Yii2 中的 ArrayHelper 替换了 Yii 1.1 中的 CHtml 列表数据。[请从您的控制器加载数组数据]
编辑
从控制器加载数据。
控制器
$items = ArrayHelper::map(Standard::find()->all(), 's_id', 'name');
...
return $this->render('your_view',['model'=>$model, 'items'=>$items]);
在视图中
<?= Html::activeDropDownList($model, 's_id',$items) ?>
看来您已经找到了答案,但是既然您提到了活动形式,我将再提供一个,即使它只是略有不同。
<?php
$form = ActiveForm::begin();
echo $form->field($model, 'attribute')
->dropDownList(
$items, // Flat array ('id'=>'label')
['prompt'=>''] // options
);
ActiveForm::end();
?>
上面有一些很好的解决方案,我的只是两者的结合(我是来这里寻找解决方案的)。
@Sarvar Nishonboyev 的解决方案很好,因为它维护了表单输入标签的创建和错误消息的帮助块。
我去了:
<?php
use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?=
$form->field($model, 'parent_id')
->dropDownList(
ArrayHelper::map(Product::find()->asArray()->all(), 'parent_id', 'name')
)
?>
再次感谢:@Sarvar Nishonboyev's 和 @ippi
这个问题似乎有很多很好的答案。所以我会尽量给出一个详细的答案
活动形式和硬编码数据
<?php
echo $form->field($model, 'name')->dropDownList(['1' => 'Yes', '0' => 'No'],['prompt'=>'Select Option']);
?>
或者
<?php
$a= ['1' => 'Yes', '0' => 'No'];
echo $form->field($model, 'name')->dropDownList($a,['prompt'=>'Select Option']);
?>
数据库表中的活动表单和数据
我们将使用 ArrayHelper 所以首先将它添加到命名空间
<?php
use yii\helpers\ArrayHelper;
?>
ArrayHelper 有许多可用于处理数组的完整函数 map () 是我们将在此处使用的函数,此函数有助于从多维数组或对象数组创建映射(键值对)。
<?php
echo $form->field($model, 'name')->dropDownList(ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
不属于活动形式
<?php
echo Html::activeDropDownList($model, 'filed_name',['1' => 'Yes', '0' => 'No']) ;
?>
或者
<?php
$a= ['1' => 'Yes', '0' => 'No'];
echo Html::activeDropDownList($model, 'filed_name',$a) ;
?>
不是活动表单,而是来自 db 表的数据
<?php
echo Html::activeDropDownList($model, 'filed_name',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
看看这个:
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-']) ?>
也许我错了,但我认为从视图中查询 SQL 是个坏主意
这是我的方式
在控制器中
$model = new SomeModel();
$items=ArrayHelper::map(TableName::find()->all(),'id','name');
return $this->render('view',['model'=>$model, 'items'=>$items])
并且在视图中
<?= Html::activeDropDownList($model, 'item_id',$items) ?>
或使用 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']
) ?>
这将帮助你......不要忘记在标题中使用类文件。
在ActiveForm
刚刚使用:
<?=
$form->field($model, 'state_id')
->dropDownList(['prompt' => '---- Select State ----'])
->label('State')
?>
这是关于生成数据的,因此更适合从模型中完成。想象一下,如果您想更改数据在下拉框中的显示方式,比如添加姓氏或其他内容。您必须找到每个下拉框并更改arrayHelper
. 我在我的模型中使用一个函数来返回下拉数据,因此我不必在视图中重复代码。它还有一个优点是我可以在此处指定过滤器并将它们应用于从该模型创建的每个下拉列表;
/* Model Standard.php */
public function getDropdown(){
return ArrayHelper::map(self::find()->all(), 's_id', 'name'));
}
您可以像这样在视图文件中使用它;
echo $form->field($model, 'attribute')
->dropDownList(
$model->dropDown
);
如果你把它排到了列表的底部。保存一些 php 代码,然后根据需要从数据库中取回所有内容,如下所示:
$items = Standard::find()->select(['name'])->indexBy('s_id')->column();
<?=$form->field($model, 'category_id')->dropdownList(
\common\models\Category::find()
->select(['name', 'id'])
->indexBy('id')
->column(),
['prompt'=>'select category']
)?>
Html::activeDropDownList($model, 'id', ArrayHelper::map(AttendanceLabel::find()->all(), 'id', 'label_name'), ['prompt'=>'出席状态'] ) ;
也可以进行以下操作。如果要附加前置图标。这会很有帮助。
<?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();?>