CGridview条件删除按钮
我希望删除按钮仅在 CGgridView
CButtonColumn
中的特定条件下处于活动状态(或创建自定义删除按钮),例如如果 user=='admin' 或
status=='draft'
。有什么想法吗?谢谢!
I want the delete button to be active only in certain condition in CGgridView
CButtonColumn
(or make a custom delete button) e g if user=='admin'
or status=='draft'
. Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用“可见”参数 -
use 'visible' parameter -
如果 PHP >= 5.3,您还可以使用匿名函数
You can also use anonymous function if PHP >= 5.3
正如 zuups 在 Mukesh 帖子中所述,您必须使用单引号!如果状态是模型实例的属性,则 user1584901 的答案是正确的。所以,
是正确的。 (底部解释)
我想添加一些你也可以做的有趣的事情。
例如,考虑一个拥有资产的用户。在这种情况下,我只想向没有任何资产的用户添加删除按钮。
在这种情况下,您可以在用户模型中建立一个关系,例如
如果用户有资产,则返回 1,否则返回 0。
并将visible参数定义为
所有这一切起作用的原因(如0x7fffffff所要求的)是因为Yii使用visible中定义的字符串将其应用于呈现按钮(renderButton)的函数内的evaluateExpression函数。
来自: https://github.com /yiisoft/yii/blob/1.1.14/framework/zii/widgets/grid/CButtonColumn.php line 337
在 CComponent 类中定义:
https://github.com/yiisoft/yii/blob /1.1.14/framework/base/CComponent.php line 607
所以基本上发生的是,evaluateExpression 函数将使变量 $data 可用(这是相关行的模型实例)并且$row (所有这一切都通过使用 extract 函数)并将您的字符串表达式计算为 php 代码。因此,任何对 $data 或 $row 的提及都将使用此范围内 evauteExpression 函数已设置的变量。
这就是为什么您可以使用相应行的相应模型实例(如示例中的 $data->status 或 $data->haveAssets)。
请注意,该字符串应该是一个返回布尔值的表达式,以确定按钮的可见性。
字符串应该用单引号括起来的原因是,在使用双引号时,php 会假设任何以 $ 开头的字符串都是变量,并尝试用该变量值替换它。由于在您的范围内 $data 变量毫无意义(或可以定义),因此它会抛出错误或误导性地替换它。使用单引号可以防止出现这种行为。
As zuups states in Mukesh post, you have to use single quotes! And user1584901 is right with the answer, in the case the status is a property of the model instance. So,
is correct. (Explanation at the bottom)
I want to add some interesting things you can do as well.
For example, consider a user with assets. In this case I would want to add the delete button only to users that don't have any assets.
In this case, you can make a relation in the user model such as
Which will return 1 if the user has assets, or 0 otherwise.
And define the visible parameter as
The reason all this works (as asked by 0x7fffffff) is because Yii uses the string defined in visible to apply it to the evaluateExpression function inside the function that render the buttons (renderButton).
From: https://github.com/yiisoft/yii/blob/1.1.14/framework/zii/widgets/grid/CButtonColumn.php line 337
Which is defined in the CComponent class:
https://github.com/yiisoft/yii/blob/1.1.14/framework/base/CComponent.php line 607
So basically what happens is that the evaluateExpression function will make available the variables $data (which is the model instance for the row in question) and $row (all this by using the extract function) and evaluate your string expression as php code. So any mention to $data or $row will use the variable already set by the evaluteExpression function in this scope.
That's why you can use the respective model instance of the respective row (as $data->status, or $data->haveAssets from the examples).
Notice that the string should be a expression that returns a boolean to determine the visibility of the button.
And the reason the strings should be in single quotes is that while using double quotes, php will assume that any string that starts with $ is a variable and will try to replace it with that variable value. Since, in your scope the $data variable is meaningless (or could be defined) it will throw an error or replace it misleadingly. Using single quotes you prevent having this behaviour.