yii cgridview 属性“CActiveDataProvider.abc"没有定义 [英] yii cgridview Property "CActiveDataProvider.abc" is not defined
问题描述
在我的控制器中:
$model = new Sheets();$criteria = 新的 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));
在我看来:
widget('zii.widgets.grid.CGridView', array('id' =>'床单网格','数据提供者' =>$模型,//'过滤器' =>$模型,'template'=>'{items}{pager}</div>','启用排序' =>错误的,'列' =>大批(大批('标题' =>'追踪','名称' =>'track_name','价值' =>$model->track_name,'htmlOptions' =>大批('宽度' =>'135px',),),但我收到此错误:
<块引用>错误:未定义属性CActiveDataProvider.track_name".
谁能告诉我这是为什么?我该如何修复它.我不想使用 $data->track_name
因为我想在此代码中使用 $model->track_name 来自定义按钮添加":>
数组('标题' =>'','类' =>'CButtonColumn','模板' =>'<div class="wrapper-tools">{share}{facebook}{twitter}{download}{add}{update}{delete2}</div>','按钮'=>数组('添加' =>大批('label'='添加收藏','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))','选项' =>array('id' => 'add-favorite', 'class' => 'admin-tools' ),//'可见' =>'Users::model()->checkFavorite(Yii::app()->user->getId(), $data->id) == false',),),'htmlOptions' =>大批('类' =>'管理工具-2',),)
谢谢
解决方案在你的CGridView中你必须使用$data->track_name
,下面一行是错误的:
'value' =>$model->track_name,
您需要像这样设置值:
'value' =>'$data->track_name',
更新
正如您提到的,您希望在每一行的按钮中使用数据,您仍然需要使用数据参数,因为 CGridView 小部件一次渲染一行,并在 $data<中使用给定行的数据/code> param, $model->attribute
这里仍然没有上下文,因为 CGridView 中的每一行都不同.
因为 imageUrl
参数没有被评估,所以默认情况下你不能使用 $data
参数,但是如果你创建一个自定义类来扩展你可以CButtonColumn 并覆盖此行为.
您可以创建一个类,例如 MyCButtonColumn
,并将其保存在您的 components
文件夹中名为 MyCButtonColumn.php
的文件中(这样它会在需要时自动加载).
现在您需要在这个新类中创建一个 renderButton
方法来评估 imageUrl
是否需要评估,然后返回标准 CButtonColumn 的结果::renderButton()
方法与新评估的 imageUrl
像这样:
class MyCButtonColumn 扩展 CButtonColumn{受保护的函数 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);}}
最后,您需要将按钮列的类从 CButtonColumn
更改为 MyCButtonColumn
.完成后,您将能够通过 imageUrl
变量解析 $data
变量,如下所示:
数组(...'类' =>'MyCButtonColumn',...'按钮'=>数组('添加' =>大批(...'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"',...),...),...),
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
解决方案 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"',
...
),
...
),
...
),
这篇关于yii cgridview 属性“CActiveDataProvider.abc"没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- Yii cgridview ajaxUpdate 属性;
- Yii - CGridView - 添加自己的属性;
- YII CGridView 错误;
- 如何迭代Yii CActiveDataProvider对象?;
- 如何遍历 Yii CActiveDataProvider 对象?;
- 在Yii中返回CActiveDataProvider;
- 如何覆盖CActiveDataProvider法在默认情况下,在Yii中降序排列显示CGridview;
- 如何自定义Yii CGridView Pager?;
- 如何自定义 Yii CGridView Pager?;
- 带有关系表的 YII CActiveDataProvider;
- CArrayDataProvider 与 CGridView 分页 Yii;
- 在 yii 中显示 Cgridview;
- yii : 如何 ajax 更新 cgridview;
- Yii:反转 CGridView 显示顺序;
- yii:如何ajax更新cgridview;
- Yii CGridView javascript 分页事件;
- Yii CGridview 使用 CSqlDataProvider 排序;
- Yii的cgridview ajaxUpdate财产;
- yii cgridview 排序不起作用;
- 在 YII CGridView 中隐藏列;
- Yii:CGridView中dataProvider的格式;
- 在YII CGridView中隐藏列;
- yii CGridView 过滤器与关系;
- Yii CGridView 仅隐藏过滤器输入;
- 如何使用CGridView的Yii中ajaxUrl参数?;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;