实例中使用到的表结构如下:
Model:Users.php
<?php
/**
* The followings are the available columns in table 'tbl_user':
* @property integer $id
* @property string $name
* @property integer $sex
* @property string $home
*/
class Users extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return static the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{users}}';
}
/**
* @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('name, sex, home', 'required'),
array('name', 'length', 'max'=>32),
array('home', 'length', 'max'=>64),
array('id','safe'),
);
}
/**
* @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(
'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'name' => 'name',
'sex' => 'sex',
'home' => 'home',
);
}
}
Controller: TestController.php --> actionForm()
public function actionForm()
{
//ActiveDataProvider + GridView
$dataProvider = new CActiveDataProvider("Users", [ //Users 位model名
'pagination'=>[ //每页5条数据
'pageSize'=>5,
]
]);
$this->render("form",[
"dataProvider"=>$dataProvider
]);
}
View:test/form.php
$this->widget('zii.widgets.grid.CGridView',
array('dataProvider'=>$dataProvider,//数据源
'pager'=>array( //通过pager设置样式 默认为CLinkPager
'prevPageLabel'=>'上一页',
'firstPageLabel'=>'首页', //first,last 在默认样式中为{display:none}及不显示,通过样式{display:inline}即可
'nextPageLabel'=>'下一页',
'lastPageLabel'=>'末页',
'header'=>'',
),
'ajaxUpdate'=>false, //是否使用ajax分页 null为ajax分页
'columns'=>array(
array( //具体设置每列的header
'name'=>'ID',
'value'=>'$data->id',
),
array(
'name'=>'姓名', //需要连接可使用CLinkColumn
'value'=>'$data->name',
),
array(
'name'=>'性别', //需要连接可使用CLinkColumn
'value'=>'$data->sex==1?"male":"female"',
),
array(
'name'=>'老家',
'value'=>'$data->home',
),
array( //自定义按钮操作列
'header'=>'操作',
'buttons'=>array(
'preview'=>array(
'label'=>'审核',
'url'=>'',// 通过PHP表达式生成URL 例如createUrl
'imageUrl'=>'',// 按钮图片地址
'options'=>array(),// HTML 标签属性设置
'click'=>'',// js 代码或函数
'visible'=>'',// PHP表达式 用于权限控制
),
'recommend'=>array(
'label'=>'推荐',
),
),
'class'=>'CButtonColumn',
'template'=>'{preview} {recommend}',
),
array( //仅使用默认CButtonColumn不具体设置按钮,在显示为查看 修改 删除图片按钮
'class'=>'CButtonColumn',
),
),//end columns
)//end dataprovicer
);//end widget
效果图:
mark it...
仅供参考,自己留档学习