CGridView Date Range Without Cookies or Session (External Filtering)

You are viewing revision #9 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#8)next (#10) »

  1. Date Column Class
  2. View
  3. Model

Here's a tutorial on how to create a CGridView with external date filters the trick is to create hidden columns within the CGridView Filters something like in the example below: grid

first we have to create a Class File for the column that we will put our hidden fields you can save this file under protected/components folder

this tutorial does not only apply to date filtering but also to other forms of filtering that is outside the CGridView

Date Column Class

class DateColumn extends CDataColumn{
    //put your code here
    public $from_date;
    public $to_date;
    public function renderFilterCellContent() {
        echo CHtml::activeDateField($this->grid->filter, $this->name, array('id'=>false));
        if(isset($this->from_date))
            echo CHtml::activeHiddenField($this->grid->filter,$this->from_date);
        if(isset($this->to_date))
            echo CHtml::activeHiddenField($this->grid->filter,$this->to_date);
    }
}

View

Now we have to edit our CGridView file and make our date column to use this class file i am using the yii's default admin.php and _search.php

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'opportunity-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        array(
            'name' => 'name',
        ),

       'amount',
        array(
            //here is where we use our DateColumn class
            'class'=>'DateColumn',
            'name'=>'close_date',
            'from_date'=>'start_date',
            'to_date'=>'end_date'
          
            ),

        array(
            //another example of customized column
            'class'=>'OpportunityStageColumn',
            'name' => 'stage',
            'filter' => $stage_list,
            'closed'=>'closed',
            'open'=>'open',
            'value' => '$this->filter[$data->stage]',
        ),


    ),
));

The extended filter form (i placed this inside my _search.php)

$form = $this->beginWidget('CActiveForm', array(
    'method' => 'get',
    'id'=>'extended-filters'
        ));
//you can replace the DateField inputs with CJuiDatePicker
echo $form->dateField($model, 'start_date');
echo $form->dateField($model, 'end_date');
echo $form->submitButton();

if you don't have, you can add the following line of code to your admin.php this makes our form above submit via ajax and then reload the grid with the response data

Yii::app()->clientScript->registerScript('search', "
$('#extended-filters').submit(function(){
	$('#opportunity-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");

Model

we just now have to add the two attributes

class Opportunity extends CActiveRecord{

    public $start_date;
    public $end_date;

then don't forget to add the validation rules

public function rules(){
     return array(....
                  .
                  .
      array('start_date, end_date','safe','on'=>'search')
      );
}

public function search() {
.
.
if ($this->start_date != null) {
            $criteria->addCondition("close_date>=:from");
            $criteria->params += array('from' => date('Y-m-d', strtotime($this->start_date)));
}
if ($this->end_date != null) {
            $criteria->addCondition("close_date<=:to");
            $criteria->params += array('to' => date('Y-m-d', strtotime($this->end_date)));
}

done!