i have a custom model
<?php
class ApplicationVersion extends CModel {
public $id;
public $version;
public $description;
public function __construct($id = null, $version = null, $desc = null) {
$this->id = $id;
$this->version = $version;
$this->description = $desc;
}
public function attributeNames() {
return array('id', 'version', 'description');
}
public function attributeLabels() {
return array('id' => 'Id', 'version' => 'Version', 'description' => 'Description');
}
public function rules() {
return array(
array('id, version, description', 'safe'),
);
}
public function search() {
$rawData = array(
new ApplicationVersion(1, '1.1', 'verzija 1.1'),
new ApplicationVersion(2, '1.2', 'verzija 1.2'),
);
$config = array(
'pagination' => array(
'pageSize' => 10,
),
'sort' => array(
'attributes' => array(
'version', 'description'
),
),
);
$provider = new CArrayDataProvider($rawData, $config);
return $provider;
}
}
?>
and a grid view
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'version',
'description',
),
'selectableRows' => 0,
'htmlOptions' => array('style' => 'width:700px'),
));
?>
the problem is that grid view shows names "version" and "description" in a header row instead of labels "Version" and "Description"
how can i fix that?
funner
(Ujovlado)
October 5, 2010, 1:55pm
2
paste here controller, too.
<?php
class ApplicationVersionHistoryController extends Controller {
public function actionIndex() {
$this->pageTitle = 'Application Version History';
$model = new ApplicationVersion;
$model->unsetAttributes();
if (isset($_GET['ApplicationVersion']))
$model->attributes = $_GET['ApplicationVersion'];
$this->render('index', array('model' => $model));
}
}
here’s the controller
mahesch
(Martin)
October 6, 2010, 10:05am
4
Use following code for multiple languages or remove Yii::t() and use direct strings for header entries.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array('name'=>'version',
'header'=>Yii::t('app', 'Version'))
array('name'=>'description',
'header'=>Yii::t('app', 'Description'))
),
'selectableRows' => 0,
'htmlOptions' => array('style' => 'width:700px'),
));
?>
jason2k
(Jasonlaw83)
July 7, 2013, 5:08pm
6
According to the source codes in "yii/framework/zii/widgets/grid/CDataColumn.php"
protected function renderHeaderCellContent()
{
if($this->grid->enableSorting && $this->sortable && $this->name!==null)
echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);
else if($this->name!==null && $this->header===null)
{
if($this->grid->dataProvider instanceof CActiveDataProvider)
echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
else
echo CHtml::encode($this->name);
}
else
parent::renderHeaderCellContent();
}
If sorting for that particular column is not disabled (column sorting is enabled by default), the label will be automatically resolved if your model derives from CActiveRecord (for more info, refer to "yii/framework/web/CSort.php")
If sorting for that particular column is disabled, the label will be automatically resolved (getAttributeLabel) if your data provider is an instance of CActiveDataProvider.
Your model doesn’t derive from CActiveRecord but CModel and your data provider is not an instance of CActiveDataProvider but CArrayDataProvider, therefore the labels are not automatically resolved.
In order to resolve them explicitly you can do
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name'=>'version',
'header'=>$model->getAttributeLabel('version'),
),
array(
'name'=>'description',
'header'=>$model->getAttributeLabel('description'),
),
),
'selectableRows' => 0,
'htmlOptions' => array('style' => 'width:700px'),
));
?>