Php CGridView中的cguidatepicker和UNIX时间戳don';不行?
在数据库字段中,值“created”存储在UNIX时间戳中。在CJuiDatePicker中选择日期后,不会发生任何事情,即使是这样的日期。代码如下:Php CGridView中的cguidatepicker和UNIX时间戳don';不行?,php,yii,cgridview,Php,Yii,Cgridview,在数据库字段中,值“created”存储在UNIX时间戳中。在CJuiDatePicker中选择日期后,不会发生任何事情,即使是这样的日期。代码如下: this->widget('zii.widgets.grid.CGridView', array( 'dataProvider' => $model->search(), 'enableSorting'=>false, 'filter'=>$model, 'afterAjaxUpdat
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;'),
),
问题:
我需要排序以在所选日期发生。怎么做?您有两种可能: 一:专业/复杂的方式 您可以为unix格式的日期提供一个附加字段。datepicker有这个选项来指定一个用于显示的字段(人类可读格式)和一个用于数据操作的字段。由于您需要将其与请求一起发送,因此必须告诉cdgridview包含其他(隐藏)字段。您可以通过将包含时间戳的附加字段指定为css类并将同一类设置为cgridview()的filterSelector属性来实现这一点。这样,它将与ajax请求一起发送 第二条:简单的方法 在模型的搜索方法中,首先检查是否使用
if(strpos($this->created,'.')发送了一个人类可读的日期,
,并在将其与db值进行比较之前对其进行解析……这绝对是更简单的方法!搜索方法中的完整代码可能如下所示:
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,
));
}
要查看所有解析格式,只需查看GitHub上CDateTimeParser的代码……您可以在顶部的类注释中找到所有内容:
希望有帮助 XHR显示,在选择一个日期后,例如页面[创建]:2014年5月13日,如果我理解正确,它必须采用UNIX时间戳格式,以便显示排序后的值。谢谢。但是在被发送回后,它变成了UNIX格式,这对于用户来说是不合适的。