X
X
XenK2017-05-11 11:09:00
Yii
XenK, 2017-05-11 11:09:00

Yii2-date-range usage example in GridView?

I use kartik-v/yii2-date-range in my project .
The question arose, how to actually use it in the GridView?
Tried like this:
Search:

...
    public function behaviors()
    {
        return [
            [
                'class' => DateRangeBehavior::className(),
                'attribute' => 'createTimeRange',
                'dateStartAttribute' => 'createTimeStart',
                'dateEndAttribute' => 'createTimeEnd',
            ]
        ];
    }

    public function rules()
    {
        return [
            [['id', 'card_id', 'card_type_id', 'balance', 'created_at', /*'expires_at',*/
                'corporative', 'barter', 'active', 'countNew', 'status'], 'integer'],
            [['user_id'], 'safe'],
            [['createTimeRange'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
        ];
    }

    public function search($params)
    {
      ...
        $query->andFilterWhere(['>=', 'expires_at', Date('Y-m-d 00:00:00', strtotime($this->createTimeStart))])
            ->andFilterWhere(['<', 'expires_at', Date('Y-m-d 00:00:00', strtotime($this->createTimeEnd))]);
     ...
    }
...

View :
...
[
                'attribute'           => 'createTimeRange',
                'value'               => function ($model, $index, $widget) {
                    return date('Y-m-d', strtotime($model->expires_at));
                },
                //'format'              => 'date',
                'width'               => '20%',
                'filterType'          => \kartik\grid\GridView::FILTER_DATE_RANGE,
                'filterWidgetOptions' => [
                    'convertFormat'  => false,
                    'presetDropdown' => true,
                    'pluginOptions'  => [
                        'format'    => 'Y-m-d',
                        'autoclose' => true,
                    ]
                ]
            ],
...

As a result, nothing is filtered. I would like to see an example of how it should look

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
Boris Korobkov, 2017-05-11
@BorisKorobkov

What does DateRangeBehavior have to do with it?
Demos.krajee.com/date-range#usage is full of examples of using DateRangePicker . In particular, for filtering it is more convenient to use the example "Advanced configuration using separate start and end attributes to store information". Further filtering is no different from any other field.

D
Dmitry, 2017-05-11
@slo_nik

Good morning.
In the GridView itself, you can use it like this:

[
             'attribute' => 'created_at',
             'format' => 'datetime',
             'filter' => DatePicker::widget([
                'model' => $searchModel,
                'attribute' => 'date_from',
                'attribute2' => 'date_to',
                'type' => DatePicker::TYPE_RANGE,
                'separator' => '-',
                'pluginOptions' => ['format' => 'yyyy-mm-dd']
             ])
            ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question