Using CJuiDatePicker for CGridView filter

25 followers

We can use a CJuiDatePicker for a CGridView inline filter.

I hope the following sample code fragment will be enough for you.

Sample Code

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $model->search(),
    'filter' => $model,
    'afterAjaxUpdate' => 'reinstallDatePicker', // (#1)
    'columns' => array(
        ...
        array(
            'name' => 'due_date',
            'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'model'=>$model, 
                'attribute'=>'due_date', 
                'language' => 'ja',
                'i18nScriptFile' => 'jquery.ui.datepicker-ja.js', // (#2)
                'htmlOptions' => array(
                    'id' => 'datepicker_for_due_date',
                    'size' => '10',
                ),
                'defaultOptions' => array(  // (#3)
                    'showOn' => 'focus', 
                    'dateFormat' => 'yy/mm/dd',
                    'showOtherMonths' => true,
                    'selectOtherMonths' => true,
                    'changeMonth' => true,
                    'changeYear' => true,
                    'showButtonPanel' => true,
                )
            ), 
            true), // (#4)
        ),
        ...
    ),
));
 
// (#5)
Yii::app()->clientScript->registerScript('re-install-date-picker', "
function reinstallDatePicker(id, data) {
    $('#datepicker_for_due_date').datepicker();
}
");

Note

  • Call CController::widget() with the third parameter set to 'true'. (#4)
  • Define 'afterAjaxUpdate' to call a javascript function that will reinstall the datepicker, because it will return to a plain textField each time the grid is updated by an ajax call. (#1) and (#5)
  • I had to define 'i18nScriptFile' option. Setting 'language' option was not enough. (#2)
  • Use 'defaultOptions' instead of 'options', otherwise you have to set those options again in the javascript function to reinstall the datepicker. (#3)

That's all. Have fun.

More to read

Total 3 comments

#10283 report it
softark at 2012/10/17 08:38am
@coy_coy

Do you try to use 'value' property of CJuiDatePicker in the filter? Don't use 'name' and 'value', use $model and 'attribute' instead.

#10273 report it
coy_coy at 2012/10/17 02:07am
question

date picker filter will not return the date which I selected. I set the date column value to this

'value'=>'date("m/d/Y",strtotime($data->date_requested))'

I think this is due to hour and second values is not set. any help?

#10070 report it
Kollipara Rama Krishna at 2012/10/03 08:39am
thanks

thanks pal its very useful for me

Leave a comment

Please to leave your comment.

Write new article