问题
In my controller:
$model = new Sheets();
$criteria = new CDbCriteria;
$criteria->compare('id', $model->id);
$criteria->compare('track_name', $model->track_name, true);
$criteria->addCondition('user_id = '.Yii::app()->user->getId().' and '.$listSetups[$i]['condition']);
$setups = new CActiveDataProvider($model, array('criteria' => $criteria));
In my view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'sheets-grid',
'dataProvider' => $model,
//'filter' => $model,
'template'=>'{items}<div class="nav-controller">{pager}</div>',
'enableSorting' => false,
'columns' => array(
array(
'header' => 'Track',
'name' => 'track_name',
'value' => $model->track_name,
'htmlOptions' => array(
'width' => '135px',
),
),
But I get this error:
Error:Property "CActiveDataProvider.track_name" is not defined.
Anybody can show me why is this? And how can i fix it.And I don't want to use $data->track_name
because I want to use $model->track_name in this code to customize button 'Add' :
array(
'header' => '',
'class' => 'CButtonColumn',
'template' => '<div class="wrapper-tools">{share}{facebook}{twitter}{download}{add}{update}{delete2}</div>',
'buttons'=>array (
'add' => array
(
'label'=>'Add favorite',
'imageUrl'=> (Users::model()->checkFavorite(Yii::app()->user->getId(), $model->track_name)) ? Yii::app()->themeManager->baseUrl."/default/images/favorite.png" : Yii::app()->themeManager->baseUrl."/default/images/favorite-disabled.png",
'url'=>'Yii::app()->createUrl("/sheets/", array("id"=>$data->id))',
'options' => array('id' => 'add-favorite', 'class' => 'admin-tools' ),
//'visible' => 'Users::model()->checkFavorite(Yii::app()->user->getId(), $data->id) == false',
),
),
'htmlOptions' => array(
'class' => 'admin-tools-2',
),
)
Thanks
回答1:
In your CGridView you have to use $data->track_name
, the following line is wrong:
'value' => $model->track_name,
You need to set the value like so:
'value' => '$data->track_name',
UPDATE
As you mention you want to use the data in a button for each line you still need to use the data param, as the CGridView widget renders one line at a time with the given line's data in the $data
param, $model->attribute
still has no context here as it will be different for every line in the CGridView.
Because imageUrl
param isn't evaluated, you can't use the $data
param in there by default, but you can if you create a custom class to extend CButtonColumn and override this behaviour.
You could create a class, say MyCButtonColumn
, and save it in your components
folder in a file named MyCButtonColumn.php
(this way it will be auto loaded when needed).
Now you'll need to create a renderButton
method within this new class to evaluate imageUrl
if it needs evaluating and then return the result of the standard CButtonColumn::renderButton()
method with the newly evaluated imageUrl
like so:
class MyCButtonColumn extends CButtonColumn
{
protected function renderButton($id,$button,$row,$data)
{
if(isset($button['imageUrl']) && is_string($button['imageUrl']) && strpos($button['imageUrl'],'$data')!==false)
$button['imageUrl'] = $this->evaluateExpression($button['imageUrl'],array('row'=>$row,'data'=>$data));
return parent::renderButton($id,$button,$row,$data);
}
}
Lastly you'll need to change the class of your button column from CButtonColumn
to MyCButtonColumn
. Once you've done that then you will be able to parse $data
variables through the imageUrl
variable like so:
array(
...
'class' => 'MyCButtonColumn',
...
'buttons'=>array (
'add' => array(
...
'imageUrl'=> '(Users::model()->checkFavorite(Yii::app()->user->getId(),$data->track_name)) ? Yii::app()->themeManager->baseUrl."/default/images/favorite.png" : Yii::app()->themeManager->baseUrl."/default/images/favorite-disabled.png"',
...
),
...
),
...
),
来源:https://stackoverflow.com/questions/14193234/yii-cgridview-property-cactivedataprovider-abc-is-not-defined