fawnshao的个人博客分享 http://blog.sciencenet.cn/u/fawnshao

博文

Yii Framework 自定义排序的问题

已有 3866 次阅读 2013-11-28 14:23 |个人分类:关于技术|系统分类:科研笔记| sort, yii

没那个功夫仔细阅读yii的文档,赶鸭子上架般匆匆用它做了个网站,默认的功能里有些要改,费了老大子劲解决了一些,赶紧写下来。

 

有一列数据A列既包含数字也包含字符串,老板说排序的时候只对数字排,字符串的都不要显示在前头,也就是说排序的时候忽略所有字符串。找了半天终于试出了一个可行的办法。在数据库中增加一列B,标出A列对应的表格是number还是character,然后在protected/models/对应modelphp页面,改写public function search()函数,

public function search()

       {

               // Warning: Please modify thefollowing code to remove attributes that

               // should not be searched.

 

               $criteria=new CDbCriteria;

 

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

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

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

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

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

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

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

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

               

               $sort = new CSort();

               $sort->defaultOrder = 'pencompDESC, CAST(penetrance AS UNSIGNED) ASC';

               $sort->attributes = array(

                       'expname' =>'expname',

                       'strain' => 'strain',

                       'mkgene' => 'mkgene',

                       'kogene' => 'kogene',

                       //'penetrance' =>'penetrance',

                       'cellcount' =>'cellcount',

                       'endtimepoint' =>'endtimepoint',

                       'coord' => 'coord',

                       'penetrance'=>array(

                               'asc'=>'pencomp desc, CAST(penetrance AS UNSIGNED) ASC',

                               'desc'=>'pencomp desc, CAST(penetrance AS UNSIGNED) DESC',

                       ),

               );

               

 

               return newCActiveDataProvider($this, array(

                       'criteria'=>$criteria,

                       'sort'=>$sort,

                       'pagination' =>array('pageSize' => 20,),

               ));

       }

主要就是设置了B列(pencomp)永远按照降序排列,而A列(penetrance)数字部分的排序则加了CAST(penetrance AS UNSIGNED)




https://wap.sciencenet.cn/blog-824692-745416.html

上一篇:isPcr 和 blat 的error
收藏 IP: 158.182.150.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 13:05

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部