有没有办法在 Telerik MVC 网格中有条件地显示命令按钮?
我在网格中保留了一列用于命令,如下所示:
.Columns(c =>
{
c.Command(cmd =>
{
cmd.Edit().ButtonType(GridButtonType.Image);
cmd.Delete().ButtonType(GridButtonType.Image);
});
...
}
是否有一种方法可以根据当前行中字段的值有条件地显示“删除”按钮?我想做这样的事情:
cmd.Delete().ButtonType(GridButtonType.Image)
.HtmlAttributes((item.HasChildren == true ? new { style = "display: none"} : null));
问题是该项目不在此处的范围内,因为它在模板列中。
谢谢。
I have a column in my grid reserved for commands as follows:
.Columns(c =>
{
c.Command(cmd =>
{
cmd.Edit().ButtonType(GridButtonType.Image);
cmd.Delete().ButtonType(GridButtonType.Image);
});
...
}
Is there a way to conditionally display the Delete button based on a value of a field in the current row? I'd like to do something like this:
cmd.Delete().ButtonType(GridButtonType.Image)
.HtmlAttributes((item.HasChildren == true ? new { style = "display: none"} : null));
The problem is that item is not in scope here as it is in a Template column.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好使用网格的 OnRowDataBound 客户端事件来处理此问题。以下 JavaScript 应该可以工作:
e 变量可用于访问您的任何字段(因此可以检查 HasChildren 是否为 true 或 false),然后您所要做的就是查看特定行($(e.row )) 并找到代表删除按钮 (t-grid-delete) 的元素。
It might be best to handle this with the OnRowDataBound client-side event of the Grid. The following JavaScript should work:
The e variable can be used to access any of your fields (hence HasChildren can be checked if it is true or false) and then all you have to do is look at the specific row ($(e.row)) and find the element representing the Delete button (t-grid-delete).
上面的答案仅适用于 Ajax 绑定。如果在实例化期间将集合传递到 Grid 中,那么您还必须在 OnLoad 事件中执行类似的操作(因为行实际上不是数据绑定的)。
The above answer only works with Ajax binding. If you pass a collection into the Grid during instantiation, then you also have to do something similar in the OnLoad event as well (because the rows aren't actually databound).