Yii2 GridView - 使用范围DatePicker

时间:2016-02-15 17:02:19

标签: gridview yii yii2

有没有办法在 GridView usign DatePicke 中设置两个字段来挑选日期。我需要单个属性的 dateFrom dateTo 字段。

这是我目前的代码:

[
    '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']
]

1 个答案:

答案 0 :(得分: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,
]);