CGridView filter between dates

Discuss everything related to ApPHP MVC Framework. All versions. Any questions related to the Framework developing or using should be posted to this forum.

Moderators: alexandrleonenko, alexmst

paragate
Master
Master
Posts: 151
Joined: Nov 13th, '14, 13:04
Location: Denmark, Odense

CGridView filter between dates

Postby paragate » Aug 10th, '17, 06:54

Hi,
Want all records from a start date given in field_1 from model to end date not a field in model
How to use filters in CGridView to filter between to dates?
Say in model there is a start date so that field_1 in filters is given. The end date is not in the model. How to use a field in Filters that is not in model

is to to end another case 'datebetweentime': in CGridView or how?
Regards

ne_moj
Master
Master
Posts: 243
Joined: Jan 1st, '16, 14:11

Re: CGridView filter between dates

Postby ne_moj » Aug 11th, '17, 08:19

Hello Henrik

Maybe I misunderstood the question, but I'll try to answer it.

You can specify 2 filters for one field, for example:

Code: Select all

'filters'           => array(
            ....
            'field_1_min'       => array('visible'=>true, 'title'=>'', 'type'=>'datetime', 'table'=>CConfig::get('db.prefix').'your_table', 'sourceFilter'=>'field_1', 'operator'=>'>=', 'width'=>'', 'maxLength'=>'10', 'format'=>'yyyy-mm-dd', 'htmlOptions'=>array('placeholder'=>'Field 1 Min', 'title'=>'Field 1 Min')),
            'field_1_max'       => array('visible'=>true, 'title'=>'', 'type'=>'datetime', 'table'=>CConfig::get('db.prefix').'your_table', 'sourceFilter'=>'field_1', 'operator'=>'<=', 'width'=>'', 'maxLength'=>'10', 'format'=>'yyyy-mm-dd', 'htmlOptions'=>array('placeholder'=>'Field 1 Max', 'title'=>'Field 1 Max')),
            ....
      ),

paragate
Master
Master
Posts: 151
Joined: Nov 13th, '14, 13:04
Location: Denmark, Odense

Re: CGridView filter between dates

Postby paragate » Aug 13th, '17, 06:47

Hello Sergey,
I have to make a narrow search with dates like in shopping cart where you use narrow-by-list.
all dates from 1. jan to 15. jan..
In your example i got 2 input date fields field_1_min and field_1_max. Which is fine. Let us remove "table" i use CGridView

Code: Select all

echo CWidget::create('CGridView', array(
            'model'=>'ReportsEntityItems',

But
Only field_1_min exist in table/model ReportsEntityItems, so the Filter only take account for the field_1_min.
How to use the field_1_max as a delimiter even it is not a field in the record?
i want

Code: Select all

find all records between field_1_min and field_1_max from  model ReportsEntityItems


is it possible in helper CGridView
Many Regards

ne_moj
Master
Master
Posts: 243
Joined: Jan 1st, '16, 14:11

Re: CGridView filter between dates

Postby ne_moj » Aug 14th, '17, 08:41

Hello Henrik

The example I provided to you searches for one field field_1 (this is set by the parameter 'sourceFilter' => 'field_1'), fields field_1_min and field_1_max - does not exist.

I have to make a narrow search with dates like in shopping cart where you use narrow-by-list.

You can give a link so I understand what kind of list?

Regards

paragate
Master
Master
Posts: 151
Joined: Nov 13th, '14, 13:04
Location: Denmark, Odense

Re: CGridView filter between dates

Postby paragate » Aug 15th, '17, 08:23

Hello Sergey

For now i have the site on local host...
I thought that the CGridView filter had the opportunity to filter a list where CgridView can filter between for example two dates:

Code: Select all

date_start  and date_end

this example filter one date:

Code: Select all

echo CWidget::create('CGridView', array(
            'model'=>'ReportsEntityItems',
            'actionPath'=>'ReportsProjects/sendWorkingHoursManage',
            'pagination'=>array('enable'=>true, 'pageSize'=>20),
            'sorting'=>true,
            'defaultOrder'=>array(),
            'condition'=>'status = 1',
            'passParameters'=>true,
            'filters'           => array(
              'date'        => array('title'=> A::t('reports', 'Created At'), 'type'=>'datetime', 'operator'=>'=', 'width'=>'80px', 'maxLength'=>'', 'format'=>''),
           ),
           
            'fields'=>array(..),
));

Is there a possibility in CGridView to filter between 2 dates
then i can get a list with all workingshours from imployees from within the start date and end date?

Maybe like i created a case statement in CgridView

Code: Select all

 'type'=>'datebetweentime',

It is simple a copy of the 'type'=>'datetime' in line 321 CgridView.php . But here could be the code for the date_end field with the 'type'=>'datebetweentime' so when CGridView see that 'type'=>'datebetweentime' is used it combines the 'type'=>'datetime' and 'type'=>'datebetweentime' to a filter search that gives all records between the two filter variables.

Code: Select all

echo CWidget::create('CGridView', array(
            'model'=>'ReportsEntityItems',
            'actionPath'=>'ReportsProjects/sendWorkingHoursManage',
            'pagination'=>array('enable'=>true, 'pageSize'=>20),
            'sorting'=>true,
            'defaultOrder'=>array(),
            'condition'=>'status = 1',
            'passParameters'=>true,
            'filters'           => array(
              'date'        => array('title'=> A::t('reports', Date start search'), 'type'=>'datetime', 'operator'=>'=', 'width'=>'80px', 'maxLength'=>'', 'format'=>''), //field in the model
              'date_end'        => array('title'=> A::t('reports', 'Date end search'), 'type'=>'datebetweentime', 'operator'=>'=', 'width'=>'80px', 'maxLength'=>'', 'format'=>''), //not a field in the model its jus a variable
           ), // because of the type=>datebetweentime the list will be sorted with alle dates between date and date_end
           
            'fields'=>array(..),
));

Regards henrik

ne_moj
Master
Master
Posts: 243
Joined: Jan 1st, '16, 14:11

Re: CGridView filter between dates

Postby ne_moj » Aug 16th, '17, 05:12

Hi!

Try using this code:

Code: Select all

echo CWidget::create('CGridView', array(
            'model'=>'ReportsEntityItems',
            'actionPath'=>'ReportsProjects/sendWorkingHoursManage',
            'pagination'=>array('enable'=>true, 'pageSize'=>20),
            'sorting'=>true,
            'defaultOrder'=>array(),
            'condition'=>'status = 1',
            'passParameters'=>true,
            'filters'           => array(
              'date_from'        => array('title'=> A::t('reports', Date start search'), 'type'=>'datetime', 'operator'=>'>=', 'sourceFilter' => 'date', 'width'=>'80px', 'maxLength'=>'', 'format'=>''),
              'date_to'          => array('title'=> A::t('reports', 'Date end search'), 'type'=>'datetime', 'operator'=>'<=', 'sourceFilter' => 'date', 'width'=>'80px', 'maxLength'=>'', 'format'=>''),
           ),
           
            'fields'=>array(..),
));


Fields "date_from" and "date_to" in the table do not exist, both these filters search in the "date" field


Return to “ApPHP MVC Framework {developers/users}”