2017-06-19 114 views
1

我在索引中有兩個dropDownlist,當我選擇提交按鈕時,它將在控制器中得到動作,並且它執行一些操作並再次使用gridview渲染索引,但是所選dropDownList變爲空白,我怎麼能在dropDownllist中設置默認?如何在dropDownlist中顯示默認值

任何建議應appreciatable ...... 這是我的看法

<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> 

這是我的控制器

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]); 

}

當我選擇下拉會戈特actionDisplay,並找到數據提供者,然後它再次渲染到索引,以便該如何顯示默認的選擇值dropDownlist?

+0

那些下拉列表關聯到'$ model',因此他們有VALU e他們各自的模型領域有 – gmc

+0

我只獲得單一的價值所有,,如何定義我的選項 – Midlaj

+0

assingn的默認值是關係到模型..請更新您的問題,並添加您的相關控制器/行動代碼 – scaisEdge

回答

1

如果您正在使用一個ActiveRecord然後controllerAction你應該分配給$模型 - > your_fiedl你需要爲默認

 else{ 
      $model->fk_int_payroll_month = 3 
      return $this->render('index', ['model' => $model]); 

     } 

,如果你不使用的活動記錄的值,你可以使用

->dropDownList($yourlist ,$selection) 

->dropDownList(ArrayHelper::map(TblPayrollMonth::find()->all() ,3) 
+0

你的意思是我想要在控制器硬編碼,如果我harcode hows dropDownlList看起來像? – Midlaj

+0

答案更新...如果第一種情況下,你應該在控制器之前分配給前.. – scaisEdge

+0

maaan非常感謝你....經過很長一段時間,其工作正常.... – Midlaj