
本文介绍了如何在下拉列表中显示默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在索引中有两个 dropDownlist,当我选择提交按钮时,它会在控制器中执行一些操作,并执行一些操作并再次使用 gridview 渲染索引,但是 Selected dropDownList 变为空白,我如何设置默认值dropDownllist??
任何建议都应该是可观的...这是我的看法
<?= $form->field($model, 'fk_int_payroll_month')->dropDownList(ArrayHelper::map(TblPayrollMonth::find()->all(), 'pk_int_payroll_month_id','vchr_month'),['提示'=>'选择...'])?>
<div class="col-sm-12" align="center"><?= $form->field($model, 'fk_int_payroll_year')->dropDownList(ArrayHelper::map(TblPayrollYear::find()->all(), 'pk_int_payroll_year_id','year'),['选项' =>[isset($_POST['fk_int_payroll_year'])?'fk_int_payroll_year':'' =>['已选择'=>true]]],['提示'=>'看...'])?>
这是我的控制器
公共函数actionDisplay(){$model = new TblPayroll();if(Yii::$app->request->post()!=null){$data = Yii::$app->request->post();//var_dump($data);//死;$month = $data['TblPayroll']['fk_int_payroll_month'];$year = $data['TblPayroll']['fk_int_payroll_year'];/* 小心这个!*/$dataProviderSearch = 新的 ActiveDataProvider(['查询' =>TblPayroll::find()->where(['fk_int_payroll_month'=>$month, 'fk_int_payroll_year'=> $year]),'分页' =>['pageSize' =>5],]);如果($dataProviderSearch){返回 $this->render('index', ['dataProviderSearch' =>$dataProviderSearch,'模型' =>$模型,]);}}别的{return $this->render('index', ['model' => $model]);
}
当我选择下拉列表时,它会得到 actionDisplay,并找到 dataprovider,然后它再次渲染到索引,这样我如何在 dropDownlist 中显示默认选定值?
解决方案
如果您使用的是 activeRecord 那么在 controllerAction 中,您应该将默认值分配给 $model->your_fiedl
else{$model->fk_int_payroll_month = 3return $this->render('index', ['model' => $model]);}
如果您不使用活动记录,则可以使用
->dropDownList($yourlist ,$selection)
例如
->dropDownList(ArrayHelper::map(TblPayrollMonth::find()->all(),3)
I have two dropDownlist in index, when I select the submit button it will get action in controller, and it performs some operations and again render the index with gridview, but the Selected dropDownList become blank, how can I set the default in dropDownllist??
Any suggestion should be appreciatable...this is my view
<div class="col-sm-12" align="center">
<?= $form->field($model, 'fk_int_payroll_month')->dropDownList(
ArrayHelper::map(TblPayrollMonth::find()->all(), 'pk_int_payroll_month_id','vchr_month'),
['prompt'=> 'Select...'])
?>
</div>
<div class="col-sm-12" align="center">
<?= $form->field($model, 'fk_int_payroll_year')->dropDownList(
ArrayHelper::map(TblPayrollYear::find()->all(), 'pk_int_payroll_year_id','year'),
['options' => [isset($_POST['fk_int_payroll_year'])?'fk_int_payroll_year':'' => ['Selected'=>true]]],
['prompt'=> 'Seect...'])
?>
</div>
this is my controller
public function actionDisplay()
{
$model = new TblPayroll();
if(Yii::$app->request->post()!=null)
{
$data = Yii::$app->request->post();
// var_dump($data);
// die;
$month = $data['TblPayroll']['fk_int_payroll_month'];
$year = $data['TblPayroll']['fk_int_payroll_year'];
/* Be careful with this! */
$dataProviderSearch = new ActiveDataProvider
([
'query' => TblPayroll::find()->where(['fk_int_payroll_month'=>$month, 'fk_int_payroll_year'=> $year]),
'pagination' => ['pageSize' => 5],
]);
if($dataProviderSearch)
{
return $this->render('index', [
'dataProviderSearch' => $dataProviderSearch,
'model' => $model,
]);
}
}
else{
return $this->render('index', ['model' => $model]);
}
when i select dropdown it will gott actionDisplay, and find the dataprovider, and then it again render to index, so that time how can i show default selected value in dropDownlist?
解决方案
If you are using an activeRecord then in controllerAction you should assign to the $model->your_fiedl the value you need for default
else{
$model->fk_int_payroll_month = 3
return $this->render('index', ['model' => $model]);
}
if you don't are using an active record you could use
->dropDownList($yourlist ,$selection)
eg
->dropDownList(ArrayHelper::map(TblPayrollMonth::find()->all() ,3)
这篇关于如何在下拉列表中显示默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!