Presnt an extension for Yii 1.1 developers. It helps to view and search date columns in CGridView.
Example of using (see column ‘class’ => ‘DateGridColumn’):
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-event-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'typeid',
'value' => 'CHtml::encode($data->type->description)',
'type' => 'html',
'filter' => CHTML::listData(UserEventType::model()->findAll(), 'id', 'description'),
),
array(
'name' => 'model',
'value' => 'CHtml::encode($data->model)',
'type' => 'html',
),
'param',
array(
'name' => 'edate',
'value' => '$data->edate',
'type' => 'raw',
'class' => 'DateGridColumn',
'datePickerOptions' => array(
'htmlOptions'=>DatePickerWidgetHelper::getHtmlOptions(array('id'=>'edate_filter','placeholder'=>'')),
),
),
array(
'class'=>'CButtonColumn',
)));
Source of the component
/protected/components/DateGridColumn.php:
<?php
/**
* @author Maxim Morskov
* DateGridColumn class for date type columns of CGridView
*/
class DateGridColumn extends CDataColumn
{
public $datePickerOptions = array();
public $dateFormat = 'Y-m-d H:i:s';
/**
* Component initialization
*/
public function init()
{
parent::init();
$this->attachAjaxUpdateHandler();
}
/**
* Renders the data cell content.
* @param integer the row number (zero-based)
* @param mixed the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if($this->value!==null) $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
elseif($this->name!==null) $value=CHtml::value($data,$this->name);
echo $value===null ? $this->grid->nullDisplay : $this->getFormattedDate($value);
}
/**
* Renders the filter cell content.
*/
protected function renderFilterCellContent()
{
$options = array_merge($this->datePickerOptions, array(
'model' => $this->grid->filter,
'attribute' => $this->name,
'theme'=>'redmond',
'language'=>Yii::app()->language,
'options'=>DatePickerWidgetHelper::getWidgetOptions(array('showOn'=>'focus')),
));
$widget = Yii::app()->getWidgetFactory()->createWidget($this, 'zii.widgets.jui.CJuiDatePicker', $options);
$widget->init();
$widget->run();
}
/**
* Attaches js handler function for ajax update event for CGridView component
*/
protected function attachAjaxUpdateHandler()
{
$prevHandlers = (!$this->grid->afterAjaxUpdate || strpos($this->grid->afterAjaxUpdate, 'function(id, data){')===false) ? '}' : substr($this->grid->afterAjaxUpdate, 20);
$this->grid->afterAjaxUpdate = 'function(id, data){'."\n";
if (strpos($this->grid->afterAjaxUpdate, '$.datepicker.setDefaults($.datepicker.regional[')===false) $this->grid->afterAjaxUpdate .= '$.datepicker.setDefaults($.datepicker.regional["'.Yii::app()->language.'"]);'."\n";
$this->grid->afterAjaxUpdate .= '$("#'.$this->datePickerOptions['htmlOptions']['id'].'").datepicker({"dateFormat": "'.'<=>'.DTHelper::JQ_DATE_FORMAT.'","showAnim": "drop","changeYear": "true","changeMonth": "true","yearRange": "c-80:c"});'."\n";
$this->grid->afterAjaxUpdate .= $prevHandlers;
}
/**
* DateFormatter function
* @param string $datetime
*/
protected function getFormattedDate($datetime)
{
return date($this->dateFormat, strtotime($datetime));
}
}
?>
Source of the Helper
/protected/helpers/DatePickerWidgetHelper.php:
<?php
/**
* @author Maxim Morskov
* DatePickerWidgetHelper
*/
class DatePickerWidgetHelper
{
static public function getWidgetOptions(array $options=array())
{
return array_merge(
array(
'showOn'=>'both',
'buttonImage'=>'/images/datePickerCalendar.gif',
'buttonImageOnly'=>'true',
'dateFormat'=>'<=>'.DTHelper::JQ_DATE_FORMAT,
'constrainInput' => 'false',
'showAnim'=>'drop',
'changeYear'=>'true',
'changeMonth'=>'true',
'yearRange'=>'c-80:c',
),
$options);
}
static public function getHtmlOptions(array $options=array())
{
return array_merge(array(
'class'=>'',
'title'=>_('Дата в формате ГГГГ-ММ-ДД'),
'placeholder'=>_('Укажите дату'),
),
$options);
}
}
?>