1、首先找到模型,然后找到public function search()方法
2、在$criteria=new CDbCriteria;后面增加如下方法:
//商品列表按ID字段、降序排列
$criteria->order='goods_id desc';
3、这时,该数据表将会控照goods_id字段,降序排列,升序为asc.
4、补充:
//商品列表按ID字段、降序排列
//注意:在gridview的表头中,有点击升降序排列的功能
//如果点击了表头上的排序,就不按goods_id排序。
if(!isset($_GET['TblGoods_sort'])){
$criteria->order='goods_id desc';
}
完整代码如下:
模型:TblGoods.php
<?php
/**
* This is the model class for table "tbl_goods".
*
* The followings are the available columns in table 'tbl_goods':
* @property string $goods_id
* @property string $cat_id
* @property string $goods_sn
* @property string $goods_name
* @property string $click_count
* @property string $brand_id
* @property string $goods_number
* @property string $market_price
* @property string $shop_price
* @property string $promote_price
* @property string $promote_start_date
* @property string $promote_end_date
* @property string $goods_desc
* @property string $goods_small_pic
* @property string $goods_big_pic
* @property integer $is_sale
* @property integer $is_delete
* @property integer $is_best
* @property integer $is_new
* @property integer $is_hot
* @property integer $is_promote
* @property string $add_time
* @property string $shops_id
* @property string $goods_color
* @property string $goods_size
*/
class TblGoods extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_goods';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('is_sale, is_delete, is_best, is_new, is_hot, is_promote', 'numerical', 'integerOnly'=>true),
array('cat_id, click_count, brand_id, goods_number, market_price, shop_price, promote_price', 'length', 'max'=>8),
array('goods_sn', 'length', 'max'=>7),
array('goods_name', 'length', 'max'=>32),
//array('goods_desc, goods_small_pic, goods_big_pic', 'length', 'max'=>255),
array('goods_desc','length','max'=>255),
//要实现图片的上传与修改,要将保存图片URL地址的字段改为file,如下所示。
//array('goods_small_pic','file','types'=>'jpg,gif,png','on'=>'insert'),
//array('goods_big_pic','file','types'=>'jpg,gif,png','on'=>'insert'),
array('promote_start_date, promote_end_date, add_time', 'safe'),
array('shops_id', 'length', 'max'=>8),
//array('goods_color', 'length', 'max'=>32),
//array('goods_size', 'length', 'max'=>32),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('goods_id, cat_id, goods_sn, goods_name, click_count, brand_id, goods_number, market_price, shop_price, promote_price, promote_start_date, promote_end_date, goods_desc, goods_small_pic, goods_big_pic, is_sale, is_delete, is_best, is_new, is_hot, is_promote, add_time', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
//使用下拉菜单,实现添加产品或修改产品时,可以从下拉列表选择产品类别:以下为第2步
//将category这个表关联到goods
//'category'=>array(self::BELONGS_TO, 'category','cat_id')
'tblCategory'=>array(self::BELONGS_TO,'tblCategory','cat_id'),
'tblShops'=>array(self::BELONGS_TO,'tblShops','shops_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'goods_id' => '产品ID',
'cat_id' => '类别ID',
'goods_sn' => '产品编号',
'goods_name' => '产品名称',
'click_count' => '点击次数',
'brand_id' => '品牌',
'goods_number' => '库存',
'market_price' => '市场价',
'shop_price' => '本店价',
'promote_price' => '促销价',
'promote_start_date' => '开始时间',
'promote_end_date' => '结束时间',
'goods_desc' => '产品描述',
'goods_small_pic' => '产品小图',
'goods_big_pic' => '产品大图',
'is_sale' => '是否销售',
'is_delete' => '是否删除',
'is_best' => '是否精品',
'is_new' => '是否新品',
'is_hot' => '是否热卖',
'is_promote' => '是否促销',
'add_time' => '创建时间',
'shops_id' => '门店ID',//表示这商品属于哪个门店
'goods_color' => '颜色',
'goods_size' => '尺寸',
使用下拉菜单,实现添加产品或修改产品时,可以从下拉列表选择产品类别:以下为第3步
//将关联到的字段,设置标题名称
//注意:这里的goods表和category表都有cat_id这个字段,系统将会使用后面设置的
'cat_id'=>'类别ID',
'cat_name'=>'类别名称',
'parent_id'=>'类别父类 ID'
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
//$criteria->with='tblCategory';
/**
*按照特定的条件显示数据表的内容
*/
//创建访问控制对象,该类在components组件文件夹下accessCtrl,该类是自定义的by ping
$accessCtrl=new accessCtrl();
//把上面得到的criteria对象传递到shopmanager方法,得到返回结果
if($temp=$accessCtrl->accessForGoods($criteria)){
$criteria=$temp;
}
//END:按照特定的条件显示数据表的内容
//商品列表按ID字段、降序排列
//注意:在gridview的表头中,有点击升降序排列的功能
//如果点击了表头上的排序,就不按goods_id排序。
if(!isset($_GET['TblGoods_sort'])){
$criteria->order='goods_id desc';
}
$criteria->compare('goods_id',$this->goods_id,true);
$criteria->compare('cat_id',$this->cat_id);
$criteria->compare('goods_sn',$this->goods_sn,true);
$criteria->compare('goods_name',$this->goods_name,true);
$criteria->compare('click_count',$this->click_count,true);
$criteria->compare('brand_id',$this->brand_id,true);
$criteria->compare('goods_number',$this->goods_number,true);
$criteria->compare('market_price',$this->market_price,true);
$criteria->compare('shop_price',$this->shop_price,true);
$criteria->compare('promote_price',$this->promote_price,true);
$criteria->compare('promote_start_date',$this->promote_start_date,true);
$criteria->compare('promote_end_date',$this->promote_end_date,true);
$criteria->compare('goods_desc',$this->goods_desc,true);
$criteria->compare('goods_small_pic',$this->goods_small_pic,true);
$criteria->compare('goods_big_pic',$this->goods_big_pic,true);
$criteria->compare('is_sale',$this->is_sale);
$criteria->compare('is_delete',$this->is_delete);
$criteria->compare('is_best',$this->is_best);
$criteria->compare('is_new',$this->is_new);
$criteria->compare('is_hot',$this->is_hot);
$criteria->compare('is_promote',$this->is_promote);
$criteria->compare('add_time',$this->add_time,true);
$criteria->compare('shops_id',$this->shops_id);
$criteria->compare('goods_color',$this->goods_color);
$criteria->compare('goods_size',$this->goods_size);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return TblGoods the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
admin.php
<?php
/* @var $this TblGoodsController */
/* @var $model TblGoods */
$this->breadcrumbs=array(
'Tbl Goods'=>array('index'),
'Manage',
);
/*
$this->menu=array(
array('label'=>'商品列表', 'url'=>array('index')),
array('label'=>'增加商品', 'url'=>array('create')),
);
*
*/
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#tbl-goods-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<div class="search-form" style="display">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tbl-goods-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
//'goods_id',//这一句跟下面这句没区别,YII默认有排序功能。
array(
'name'=>'goods_id',
'value'=>'$data->goods_id',
'sortable'=>true
),
//'cat_id',
array(
'class'=>'CDataColumn',
'name'=>'tblCategory.cat_name',
'value'=>'$data->tblCategory->cat_name',
),
'goods_sn',
array(
'class'=> 'CDataColumn',
'type'=>'image',
'name'=>'goods_small_pic',
'value'=>'$data->goods_small_pic',
'htmlOptions'=>array(
'class'=>'r_img',
)
),
'goods_name',
//'click_count',
// 'shops_id',
'shop_price',
'goods_number',
array(
'class'=>'CDataColumn',
'name'=>'tblShops.shops_name',
'value'=>'$data->tblShops->shops_name',
'htmlOptions'=>array(
'width'=>'40px'
)
),
//'brand_id',
/*
'goods_number',
'market_price',
'shop_price',
'promote_price',
'promote_start_date',
'promote_end_date',
'goods_desc',
'goods_small_pic',
'goods_big_pic',
'is_sale',
'is_delete',
'is_best',
'is_new',
'is_hot',
'is_promote',
'add_time',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
<style type="text/css">
.r_img{
text-align: center;
}
.r_img img{
width: 80px;
}
</style>