Datepicker In Cgridview Filter Format

Hi,

I have problem with DatePickers in CGridView filter. There are 3 columns with DatePicker filter. I want it bo be in Polish language and it is. I also need it to have format: yy-mm-dd. Unofortunately it has the format only for the initial request to the page. After ajax update of the CGridView (doesn’t matter if I changed one of datepickers or any other filter) - when I try to change/set date in datepicker it changes the format to dd.mm.yyy. Why? I have javascript function that is being called after ajax update.

Here is the code (part of the CGridView plus the javascript function):


$this->widget('bootstrap.widgets.TbExtendedGridView', array(

	'id'=>'projects-grid',

	'type' => 'striped condensed',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'afterAjaxUpdate' => 'reinstallDatePicker',

	'ajaxUrl' => $this->createUrl('projects/admin'),

	'columns'=>array(

		array(

			'name' => 'idProject',

			'htmlOptions' => array('width' => '60px', 

                        'style' => 'text-align: right;', 

                        'class' => 'gridIdColumn'),

		),

		'name',

		array(

			'name' => 'projStart',

			'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', 

				array(

					'model' => $model,

					'attribute' => 'projStart',

					'language' => 'pl',

					'htmlOptions' => array(

						'id' => 'Projects_projStart',

						'dateFormat' => 'yy-mm-dd',

					),

					'options' => array(  // (#3)

	          'showOn' => 'focus', 

	          'dateFormat' => 'yy-mm-dd',

	          'showOtherMonths' => true,

	          'selectOtherMonths' => false,

	          'changeMonth' => false,

	          'changeYear' => false,

          )

				),

				true),

		),

		array(

			'name' => 'confirmStart',

			'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', 

				array( ....


Yii::app()->clientScript->registerScript('re-install-date-picker', "

function reinstallDatePicker() {

  $('#Projects_projStart').datepicker($.datepicker.regional[ 'pl' ]);

  $('#Projects_projStart').datepicker({dateFormat: 'yy-mm-dd'});

  $('#Projects_confirmStart').datepicker($.datepicker.regional[ 'pl' ]);

  $('#Projects_presentationDate').datepicker($.datepicker.regional[ 'pl' ]);

}

");



I also use YiiBooster but I don’t think it’s relevant.

Check when your controller receive your date value is then reformatted as standard format "yyyy-mm-dd",

so after ajax call, date field receive date input in format "yyyy-mm-dd"

I actually am not sure how to check it. VarDump the projStart value (column with datepicker name)? After first time I change the DatePicker value it is ok - data are filtered fine and the value inside the text field is ok. After very next request it changes.

Post your code

The gridview:


$this->widget('bootstrap.widgets.TbExtendedGridView', array(

	'id'=>'projects-grid',

	'type' => 'striped condensed',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'afterAjaxUpdate' => 'reinstallDatePicker',

	'ajaxUrl' => $this->createUrl('projects/admin'),

	'columns'=>array(

		array(

			'name' => 'idProject',

			'htmlOptions' => array('width' => '60px', 'style' => 'text-align: right;', 'class' => 'gridIdColumn'),

		),

		'name',

		array(

			'name' => 'projStart',

			'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', 

				array(

					'model' => $model,

					'attribute' => 'projStart',

					'language' => 'pl',

					'htmlOptions' => array(

						'id' => 'Projects_projStart',

						'dateFormat' => 'yy-mm-dd',

					),

					'options' => array(  // (#3)

	          'showOn' => 'focus', 

	          'dateFormat' => 'yy-mm-dd',

	          'showOtherMonths' => true,

	          'selectOtherMonths' => false,

	          'changeMonth' => false,

	          'changeYear' => false,

          )

				),

				true),

		),

		array(

			'name' => 'confirmStart',

			'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', 

				array(

					'model' => $model,

					'attribute' => 'confirmStart',

					'language' => 'pl',

					'i18nScriptFile' => 'jquery.ui.datepicker-ja.js',

					'htmlOptions' => array(

						'id' => 'Projects_confirmStart',

						'dateFormat' => 'yy-mm-dd',

					),

					'options' => array(  // (#3)

	          'showOn' => 'focus', 

	          'dateFormat' => 'yy-mm-dd',

	          'showOtherMonths' => true,

	          'selectOtherMonths' => true,

	          'changeMonth' => true,

	          'changeYear' => true,

	          'showButtonPanel' => true,

          )

				),

				true),

		),

		array(

			'name' => 'presentationDate',

			'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', 

				array(

					'model' => $model,

					'attribute' => 'presentationDate',

					'language' => 'pl',

					'i18nScriptFile' => 'jquery.ui.datepicker-ja.js',

					'htmlOptions' => array(

						'id' => 'Projects_presentationDate',

						'dateFormat' => 'yy-mm-dd',

					),

					'options' => array(  // (#3)

	          'showOn' => 'focus', 

	          'dateFormat' => 'yy-mm-dd',

	          'showOtherMonths' => true,

	          'selectOtherMonths' => true,

	          'changeMonth' => true,

	          'changeYear' => true,

	          'showButtonPanel' => true,

          )

				),

				true),

		),

		array(

			'name' => 'activated',

			'value' => '$data->getBoolText($data->activated)',

			'filter' => $model->getBoolOptions(),

		),

		array(

			'class'=>'bootstrap.widgets.TbButtonColumn',

		),

		array(

			'header' => '',

			'htmlOptions' => array('style' => 'width:43px;'),

			'value' => function($data)

			{

				$this->renderPartial('partials/_actionMenu', array('idProject' => $data->idProject, 'activated' => $data->activated, 'afterAction' => 'adminCorrectAfter'));

			}

		),

		array(

			'header' => '',

			'value' => function($data)

			{

				$this->renderPartial('partials/_statMenu', array('idProject' => $data->idProject, 'showIcon' => true));

			}

		),

	),

));


Yii::app()->clientScript->registerScript('re-install-date-picker', "

function reinstallDatePicker() {

  $('#Projects_projStart').datepicker($.datepicker.regional[ 'pl' ]);

  $('#Projects_projStart').datepicker({dateFormat: 'yy-mm-dd'});

  $('#Projects_confirmStart').datepicker($.datepicker.regional[ 'pl' ]);

  $('#Projects_presentationDate').datepicker($.datepicker.regional[ 'pl' ]);

}

");

In controller:


public function actionAdmin()

	{

		$model=new Projects('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Projects']))

			$model->attributes=$_GET['Projects'];

		

		$this->render('admin',array(

			'model'=>$model,

		));

	}

Model’s search method:


public function search($addCriteria = null)

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		

		$criteria->order = 'projStart DESC';

		

		if(is_array($addCriteria))

			$criteria->mergeWith($addCriteria);

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'pagination' => array(

				'pageSize' => 50,

			),

		));

	}

As you can see - there is no $addCriteria passed to the search() method.

Thanks for helping I cannot figure it out.

We have managed to work out the answer thanks to @darkheir at StackOverflow

Topic can be closed.