Using dialog form for date range filter in CGridView

Hello,

I’m trying to develop a dialog form to filter dates by a “From” and “To” in my CGridView. The eventual goal would be to eliminate the search form and just use the table filters. I’m in the process of testing my dialog but can’t seem to pass my date value to the search function. The CGrid gets updated but my date value doesn’t get applied. Can you tell what I’m doing wrong?

FORM


<div class="dialogtest" style=display:none><form style="font-size:1em;">

From <input type='text' maxlength='16' id='fromdate' name='fromdate'></input></form></div>

JQuery


                                Yii::app()->clientScript->registerScript("test", 

					   "

					   $('input[name*=date]').dblclick(function(){

					   

                                                var posLeft=$(this).position().left;

					        var posTop=$(this).position().top;


					        $('.dialogtest').dialog({autoOpen: false,

					               position:[posLeft,posTop],

					               buttons:{ 

					               'OK':function(){

					                      //alert($('#fromdate').serialize());

					                      $.fn.yiiGridView.update('timeentries-grid',{data: $('#fromdate').serialize()});

					                      $(this).dialog('close');

					                }

					                }

					         });

					   $('.dialogtest').dialog('open');

					   });

					    $('#fromdate').click().datepicker({	

			                        disable:false,				    

			                        numberOfMonths: 1,

			                        showButtonPanel: true,

			                        dateFormat: 'yy-mm-dd'

		                           });

					   "

					   , CClientScript::POS_READY);

Model




        public $fromdate;


	public function rules()

	{

          array('time, date,fromdate, project, internalid, notes, projectitem, timerunning, createdby, createddate, updatedby, updateddate', 'safe', 'on'=>'search'),

        }


	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('time',$this->time,true);

		$criteria->compare('date',$this->fromdate,true,'>=');

		$criteria->compare('project',$this->project,true);

		$criteria->compare('internalid',$this->internalid,true);

		$criteria->compare('notes',$this->notes,true);

		$criteria->compare('projectitem',$this->projectitem,true);

		$criteria->compare('timerunning',$this->timerunning);

		$criteria->compare('createdby',$this->createdby,true);

		$criteria->compare('createddate',$this->createddate,true);

		$criteria->compare('updatedby',$this->updatedby,true);

		$criteria->compare('updateddate',$this->updateddate,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'pagination'=>array('pagesize'=>40),

		));

	}




I think ‘name’ should be in the format of ModelName[attributeName], e.g. MyModel[fromdate], if you are modifying the “admin” page that has been created by gii.

And in the model, I would do like this:




        public $fromdate;

        public $todate;


	public function rules()

	{

		...

          array('time, date, fromdate, todate, project, internalid, notes, projectitem, timerunning, createdby, createddate, updatedby, updateddate', 'safe', 'on'=>'search'),

        }


	public function search()

	{

		...

		$criteria->compare('date', $this->date);

		$criteria->compare('date', '>='.$this->fromdate);

		$criteria->compare('date', '<='.$this->todate);

		...

	}



CDbCriteria::compare() will recognize the comparison operator prefixed to the 2nd parameter.

And it won’t create any condition clause if the given value($this->date, $this->fromdate and/or $this->todate) is empty.

So, if the user has entered fromdate and todate, without date, then 2 conditions are created and they are concatenated by the 4th parameter of compare which is ‘AND’ by default.

Thank you softark. This is working.

The remaining piece I need to add is to rebind the dialog afterAjaxupdate of the Cgridview. My CGridView afterAjaxUpdate parameter is getting pretty full (as I’ve already got lots of scripts on the CGridView). I will have to try and squeeze this one in or maybe use the .live Jquery function.

Thanks again.