如何使用
dropdown
和模型在yii2
中制作activeform
?由于yii2
中所有方法都发生了变化,那么在新方法中是如何完成的?
就像
<?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 和 @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) ;
?>
不是活动表单,而是来自数据库表的数据
<?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'=>'Attendance Status'] );
也可以进行以下操作。如果您想附加前置图标。这会有帮助的。
<?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();?>