V
V
Vitaliy2015-07-14 12:06:58
MySQL
Vitaliy, 2015-07-14 12:06:58

How to filter data by two dates with ModelSearch in Yii2?

There is a data model in which there is a date_start field.
It is necessary to implement filtering by date range (between).
For the datepicker, I installed the kartik extension.
The widget code is as follows

<div class="container">
        <div class="col-md-6">
            <?='<label class="control-label">Valid Dates</label>';?>
            <?= DatePicker::widget([
                'name' => 'from_date',
                'type' => DatePicker::TYPE_RANGE,
                'name2' => 'to_date',
                'options' => ['placeholder' => 'Start date','class' => 'datepicker'],
                'options2' => ['placeholder' => 'End date', 'class' => 'datepicker'],
                'pluginOptions' => [
                    'autoclose'=>true,
                    'format' => 'yyyy/mm/dd'
                ]
            ]);

            ?>
        </div>
    </div>

5cdb8ec5de984db5964e8a424afc9935.jpg
How to organize data filtering using ModelSearch?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy, 2015-07-14
@Vitalij_D

There are small shifts, filtering has been achieved.
1. Widget with datepickers moved to GridView

[
                'attribute' => 'date_start',
                'label' => 'Date start',
                'filter' => DatePicker::widget([
                    'name' => 'CampaignSearch[from_date]',
                    'type' => DatePicker::TYPE_RANGE,
                    'name2' => 'CampaignSearch[to_date]',
                    'options' => ['placeholder' => 'Start date','class' => 'datepicker'],
                    'options2' => ['placeholder' => 'End date', 'class' => 'datepicker'],
                    'pluginOptions' => [
                        'autoclose'=>true,
                        'format' => 'yyyy/mm/dd',
                    ]
                ]),
            ],

2. Created 2 fields in the model:
public $from_date;
    public $to_date;

3. Added new model fields to safe in ModelSearch and added filtering
1. When choosing a date in the FROM or TO field, the date is entered in both fields at once (this is how the plugin intended).
2. After that, the request to the server for the filter immediately leaves. It turns out that the user does not have time to select two dates. How can this be avoided? In principle, you can remove the duplication of the date in both fields at once ... Perhaps then the problem will partially disappear.

T
TarMaks, 2016-04-06
@TarMaks

I have the same problem! I can't decide yet.
If you suddenly remember that you have a solution, please unsubscribe!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question