2016-02-15 96 views
0

有沒有辦法讓兩個字段在GridView美國DatePicke中挑選日期。我需要dateFromdateTo字段爲單個屬性。Yii2 GridView - 使用範圍DatePicker

這是我當前的代碼:

[ 
    'attribute' => 'created_at', 
    'label'  => 'Date Added', 
    'value'  => 'created_at', 
    'filter' => \yii\jui\DatePicker::widget([ 
     'model'=>$searchModel, 
     'dateFormat' => 'MM-dd-yyyy', 
     'attribute'=>'dateFrom',         
     'options' => [ 
       'class' => 'form-control' 
      ], 

     ]), 
    'format' => ['date', 'php:m-d-Y, H:s'] 
] 
+0

我認爲你必須在你的模型創建兩個新的屬性,並結合他們保存。 –

+0

請參閱:http://demos.krajee.com/widget-details/datepicker#markup-range –

+0

本主題也應該有所幫助http://www.yiiframework.com/forum/index.php/topic/55966-yii2-日期範圍/ – SiZE

回答

0

這必將幫助你。你必須要在GridView的以下變化:

[ 
    'attribute' => 'created', 
    'value' => 'created', 
    'filter' => DateRangePicker::widget([ 
     'model' => $searchModel, 
     'attribute'=>'created', 
     'convertFormat'=>true, 
     'pluginOptions'=>[ 
      'locale'=>[ 
       'format'=>'Y-m-d', 
       'separator' => 'to', 
      ] 
     ] 
    ]) 
], 

你必須要在模式搜索方法如下定製:

$strCondition = ''; 
if (!empty($params['ModelSearch']['created'])) { 
    $arrExplodeRange = explode('to', $params['ModelSearch']['created']); 
    $strCondition = "created BETWEEN '". $arrExplodeRange[0] . "' AND '" . $arrExplodeRange[1] . "'"; 
} 

$query = Category::find()->where($strCondition); 
$dataProvider = new ActiveDataProvider([ 
    'query' => $query, 
]); 
+0

您使用哪個庫作爲DateRangePicker? – Sasha