从 cgridview 中的按钮访问 $data 变量
有什么方法可以从 CButtonColumn 访问位于 $data
变量中的模型吗? 下面的代码不起作用。
array(
'class' => 'CButtonColumn',
'template' => '{test}',
'buttons' => array(
'test' => array(
'label' => 'Select',
'click' => 'js:function() { <b>alert($data->_id);</b> return false;}',
),
),
),
Is the any way, to access model located in $data
variable from CButtonColumn?
Below code is not working.
array(
'class' => 'CButtonColumn',
'template' => '{test}',
'buttons' => array(
'test' => array(
'label' => 'Select',
'click' => 'js:function() { <b>alert($data->_id);</b> return false;}',
),
),
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以从 jquery 访问可见属性:
It is possible to access visible attributes from jquery:
CButtonColumn 类中唯一允许使用
$data
的字段是url
、imageUrl< /code>
和
可见
。要将 id 传递给 javascript 点击事件,您可以将此类 id 放入 url 中并从 DOM 中获取它。这是非常粗鲁的黑客,但很容易实现。如果您正在寻找更清晰的编码,您可以在 CDataColumn 类中工作
The only field where
$data
is allowed in CButtonColumn class isurl
,imageUrl
andvisible
. To pass id to the javascript click event you may place such id in the url and grab it from the DOM. This is very rude hack but easy implementation.If you are looking for a more clear coding you could work in CDataColumn class
看起来 _id 是一个私有变量(根据 Yii 的编码“标准”)。您无法访问对象外部的私有变量(和方法)。在您的模型中创建一个像这样的 getter 方法:
然后将代码更改为:
It looks like _id is a private variable (according to Yii's coding "standards"). You can not access private variables (and methods) outside of an object. Create a getter-method like this in your model:
and then change your code to:
您需要自定义 CButtonColumn 类。看看这篇文章:
http://www.yiiframework.com/wiki/714/yii-1-1-cgridview-use-special-variable-data-in- the-options-of-a-button-ie-evaluate-options-attribute/
You need to customize the CButtonColumn class. Have a look this post:
http://www.yiiframework.com/wiki/714/yii-1-1-cgridview-use-special-variable-data-in-the-options-of-a-button-i-e-evaluate-options-attribute/
您可以通过自定义函数来做到这一点,因为我们可以在其中派生一个 $data 变量,以便我们可以更好地利用 php 以及 yii 本身。
尝试这样:
You can do that by custom function, as we can derived a $data variable inside it so that we can utilize better php as well as yii itself.
Try like this :