<?php
//form
class SomeForm extends CFormModel
{
public $id;
public $user_id;
public function search()
{
$sql = 'SELECT id, name FROM some_table';
$sql_count = 'SELECT COUNT(id) FROM some_table';
return new CSqlDataProvider($sql, array(
'totalItemCount' => Yii::app()->db->createCommand($sql_count)->queryScalar(),
'sort' => array(
'attributes' => array(
'id', 'name',
),
),
'pagination' => array(
'pageSize' => 50,
),
));
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'NAME',
);
}
}
//grid
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(), //$model = new SomeForm()
'columns' => array(
'id',
'name'
),
));
/*
Result:
id | name
---------
1 | John
EXPECTED Result:
ID | NAME
---------
1 | John
*/如何设置查询列的自定义名称?
发布于 2012-05-23 17:39:16
最简单的方法:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(), //$model = new SomeForm()
'columns' => array(
'id::ID',
'name::NAME'
),
));另一种方式:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(), //$model = new SomeForm()
'columns' => array(
array(
'header' => 'ID',
'name' => 'id'
),
array(
'header' => 'NAME',
'name' => 'name',
),
),
));Link to api doc
发布于 2013-05-10 04:50:48
如果您不想使用自定义名称;如果您想使用在模型中声明的标签,那么您可以这样做:
$data (CSqlDataProvider)以及空模型。$labelModel =新建my_model;$this->widget('zii.widgets.CListView',CSqlDataProvider ( 'dataProvider' => $my_ model ->search(),//返回一个viewData 'itemView'=> '_view','viewData‘=>数组(’labelModel‘=> $labelModel),));
$data['field_name']回显数据。此link包含有关如何将附加模型传递给CListView或CGridView的更多信息。
https://stackoverflow.com/questions/10717219
复制相似问题