Question

In the database field value "created" is stored in a UNIX timestamp. After you select the date in the CJuiDatePicker nothing happens, even though such a date. Here is the code:

this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $model->search(),
    'enableSorting'=>false,
    'filter'=>$model,
    'afterAjaxUpdate'=>"function() {
        jQuery('#Page_created').datepicker(jQuery.extend(jQuery.datepicker.regional['en'],{'showAnim':'fold','dateFormat':'dd.mm.yy','changeMonth':'true','showButtonPanel':'true','changeYear':'true'}));
    }",
    'columns' => array(
        'title' => array(
            'name'=>'title',
//            'header'=>'Title',
            'type'=>'raw',
            'value'=>'CHtml::link($data->title,Yii::app()->request->baseUrl."/page/".$data->id)',
            'headerHtmlOptions' => array('style'=>'width:250px;'),
        ),
        array(
            'name'=>'created',
            'type'=>'raw',
            'value' => 'date("j.m.Y", $data->created)',
            'filter'=>false,
            'filter'=>$this->widget('zii.widgets.jui.CJuiDatePicker', array(
                    'model'=>$model,
                    'attribute'=>'created',
                    'language'=>'en',
                    'options'=>array(
                        'showAnim'=>'fold',
                        'dateFormat'=>'dd.mm.yy',
                        'changeMonth' => 'true',
                        'changeYear'=>'true',
                        'showButtonPanel' => 'true',
                    ),
                ),true),
            'htmlOptions' => array('style' => 'width:90px;'),
        ),

Question: I need to sort to occur on the selected date. How to do it?

Was it helpful?

Solution

You have two possibilities:

One: The professional / complex way

You provide an additional field for the date in unix-format. the datepicker has this option to specify a field for displaying purposes (human readable format) and one for data-manipulation-purposes. as you need to send this with your request, you have to tell cdgridview to include additional (hidden) fields. you can to this by giving your additional field containing the timestamp a css-class and set the same class to the filterSelector-property of cgridview (http://www.yiiframework.com/doc/api/1.1/CGridView#filterSelector-detail). that way it will be sent with the ajax-request.

Two: The easy way

In the search-method of your model you first check if a human-readable date was sent with if (strpos($this->created, '.')) and simply parse it before you compare it to the db value...definitely the easier way! The complete code within your search-method could look like this:

public function search() {
    //parse date if necessary
    if (strpos(($this->created, '.')) {
        $this->created = CDateTimeParser::parse($this->created, 'dd.MM.yy');
    }

    $criteria = new CDbCriteria();

    //other compares...
    $criteria->compare('created', $this->created);
    //even more compares...

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

To see all the parsing formats just check out the code of CDateTimeParser on GitHub...you find everything right in the class-comment on top: https://github.com/yiisoft/yii/blob/master/framework/utils/CDateTimeParser.php

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top