• Yii 日期时间过滤列 filter


    在yii使用过程中,我们经常要使用到 按时间区间来检索数据

    用gridview自身的filter就无法满足我们得需求。

    下面可以用插件的方式来搞定:

    sydatecolumn

    下载地址:http://www.yiiframework.com/extension/sydatecolumn/

    1.复制SYDateColumn.php到components目录中。

    这个类会自动生成以属性名加_range['from']和_range['to']为name的输入框,如果只用single的话,只有一个to.

    2.修改model

    因为加了与数据库字段不相关的model属性,所以这里我们定义一个属性

    public $addtime_range = array();

    function rules() {
      return array(
             ...
          array('....., addtime_range', 'safe', 'on'=>'search'),

    3.修改search()方法

      //addtime_range
             $from = $to = '';
             if (count($this->addtime_range)>=1) {
                 if (isset($this->addtime_range['from'])) {
                     $from = $this->addtime_range['from'];
                 }
                 if (isset($this->addtime_range['to'])) {
                     $to= $this->addtime_range['to'];
                 }
             }

             if ($from!='' || $to !='') {
                 if ($from!='' && $to!='') {//当两框都选择时,则搜索这个区间的数据.
                     $from = date("Y-m-d", strtotime($from));
                     $to = date("Y-m-d", strtotime($to));
                     $criteria->compare('t.addtime',">=".strtotime($from));
                     $criteria->compare('t.addtime',"<=".strtotime($to));
                 }
                 else {//当只选择了一个框时,搜出这天的记录.
                     if ($from!='') $addtime = $from;
                     if ($to != '') $addtime = $to;
                     $addtime = date("Y-m-d", strtotime($addtime));

                    $criteria->compare('t.addtime',">=".strtotime($addtime));
                    $criteria->compare('t.addtime',"<=".(strtotime($addtime)+86400));
                 }
             }

            
             //orderdate
             $to = '';
             if (isset($this->orderdate_range['to'])) {
                 $to= $this->orderdate_range['to'];
             }
             if ($to!='') {
                 $orderdate = date("Y-m-d", strtotime($to));
                 $criteria->compare('orderdate',$orderdate,true);
             }
     4.修改view

      array(             
                'header'=>'时间',//具体设置每列的header  
                'name'=>'addtime',  
                'filter'=>'range',//可以设置的值为:range,single.
                'class'=>'SYDateColumn',            
                'htmlOptions'=>array('style'=>' 150px;text-align: center;'),
            ),

  • 相关阅读:
    颜色混合openglglBlendFunc函数
    Types of Maps
    ogre 检测显卡gpu支持参数
    (转载)lua和c/c++互相调用实例分析
    光照模型
    阴影(转载)
    eval()解析JSON
    android中The connection to adb is down,问题和解决 AndroidEclipseAntXML
    比较android中的像素单位dp、px、pt、sp
    区别:DOM Core 与 HTMLDOM
  • 原文地址:https://www.cnblogs.com/jshen/p/4133055.html
Copyright © 2020-2023  润新知