如何隐藏 Ajax 绑定 Telerik Grid for MVC 上的“编辑”按钮?

发布于 2024-10-18 22:18:11 字数 231 浏览 6 评论 0原文

我使用 Telerik Extensions for MVC。在一页上,我使用网格来显示状态消息。我想显示所有具有正 status_id 的状态消息的编辑按钮。

我之前已经使用服务器绑定网格和 CellAction 完成了此操作。不过,我试图将其更改为 Ajax 绑定网格,但我无法弄清楚如何隐藏特定行的按钮。

也许有一种方法可以从 JavaScript 引用特定单元格,并以这种方式隐藏它?

谢谢!

I use Telerik Extensions for MVC. On one page I use a grid to display status messages. I want to show edit buttons for all status messages that have a positive status_id.

I have previously done this using a server bound grid and CellAction. However I am trying to change it into an Ajax bound grid, but I cannot figure out how to hide the buttons for specific rows then.

Is there, perhaps, a way to reference a specific cell from JavaScript, and hide it that way?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我偏爱纯白色 2024-10-25 22:18:11

我意识到这个问题是不久前发布的,但我希望我的答案对其他人有用。

在您的数据模型中传递一个字段。在本例中,它是“RemoveDelete”,并且由于它明确用于根据预定条件删除“删除”按钮,因此它被设置为隐藏在网格中。如果网格中显示的数据已包含您要检查的条件,则无需执行此操作。

在网格中...

.ClientEvents(events => events.OnRowDataBound("onRowDataBound"))
.Columns(columns => {
  columns.Bound(c => c.ColumnName).Attributes().Etc();
  columns.Bound(c => c.ColumnName).Attributes().Etc();
  columns.Command(commands => {
    commands.Edit().ButtonType(ButtonType);
    commands.Delete().ButtonType(ButtonType);
  });
  columns.Bound(c => c.RemoveDelete).Hidden(true);
})

脚本...

<script type="text/javascript">
  function onRowDataBound(e) {
    if (e.dataItem.RemoveDelete > 0) {
      $(e.row).find('a.t-grid-delete').remove(); //remove Delete button
    }
  }
</script>

删除编辑按钮...

$(e.row).find('a.t-grid-edit').remove();

隐藏最后一列使用

$(e.row).find('td.t-last a.t-grid-action').hide();

综上所述,这允许您预先确定要逐行显示的按钮。

I realize this question was posted some time ago but I am hoping my answer will prove useful to others.

In your data model pass a field. In this case it is "RemoveDelete" and since it is used expressly to remove the Delete button based on a predetermined condition it is set to be hidden in the grid. If data displayed in your grid already contains the condition you want to examine then you don't have to do this.

In the Grid...

.ClientEvents(events => events.OnRowDataBound("onRowDataBound"))
.Columns(columns => {
  columns.Bound(c => c.ColumnName).Attributes().Etc();
  columns.Bound(c => c.ColumnName).Attributes().Etc();
  columns.Command(commands => {
    commands.Edit().ButtonType(ButtonType);
    commands.Delete().ButtonType(ButtonType);
  });
  columns.Bound(c => c.RemoveDelete).Hidden(true);
})

the script...

<script type="text/javascript">
  function onRowDataBound(e) {
    if (e.dataItem.RemoveDelete > 0) {
      $(e.row).find('a.t-grid-delete').remove(); //remove Delete button
    }
  }
</script>

to remove the Edit button...

$(e.row).find('a.t-grid-edit').remove();

to hide the last column use

$(e.row).find('td.t-last a.t-grid-action').hide();

With all that being said, this allows you to predetermine what buttons you want displayed on a row by row basis.

桜花祭 2024-10-25 22:18:11

我设法以一种有点古怪的方式解决了这个问题:

我为“OnRowBound”事件添加了一个函数,它将包含“编辑”按钮的单元格的内部文本设置为空。

将函数添加到事件:

.ClientEvents(events => events.OnRowDataBound("hideEdit"))

函数:

function hideEdit(e) {

    if (e.dataItem["status_id"] < 0) {
        e.row.cells[5].innerText = "";
    }
}

I managed solve it in a somewhat hacky way:

I added a function for the "OnRowBound"-event, that set the innerText of the cell containing the Edit button to nothing.

Adding the function to the event:

.ClientEvents(events => events.OnRowDataBound("hideEdit"))

The function:

function hideEdit(e) {

    if (e.dataItem["status_id"] < 0) {
        e.row.cells[5].innerText = "";
    }
}
清风不识月 2024-10-25 22:18:11

@AZee 目前接受的答案很好,但自从 Telerik 从“MVC 扩展”过渡到 KendoUI 包装器后将不再有效。请参阅 Telerik 网格迁移页面 。靠近底部的是使用 DataBound 网格事件代替 onRowDataBound 事件的说明。 另请注意中的 view()以前的链接不可用,因此您需要使用_data。最后,类前缀从 t- 更改为 k-

这些更改的影响: 设置事件处理程序:

@(Html.Kendo().Grid<type>()
    .Name("grid")
    .Events(e => e.DataBound("onDataBound"))
    ...

以及事件处理程序本身:

function onDataBound() {
    var data = this._data;

    for (var i = 0; i < data.length; i++) {
        // the relevant row-data
        var dataItem = data[i]; 
        // selector for the table-row in the DOM
        var $tr = $("#grid").find("[data-uid='" + dataItem.uid + "']");

        if (<dataItem not deleteable>) $tr.find("a.k-grid-delete").remove();
        if (<dataItem not editable>) $tr.find("a.k-grid-edit").remove();
    }
}

@AZee's currently accepted answer is good, but will no longer work since Telerik transitioned from "MVC extensions" to KendoUI wrappers. See Telerik Grid Migration page. Near the bottom are instructions for using the DataBound grid event in place of the onRowDataBound event. Note also that the view() in the former link isn't available, so you'll need to use _data. Finally, the class-prefixes were changed from t- to k-.

The impact of these changes: to set up the event handler:

@(Html.Kendo().Grid<type>()
    .Name("grid")
    .Events(e => e.DataBound("onDataBound"))
    ...

And the event handler itself:

function onDataBound() {
    var data = this._data;

    for (var i = 0; i < data.length; i++) {
        // the relevant row-data
        var dataItem = data[i]; 
        // selector for the table-row in the DOM
        var $tr = $("#grid").find("[data-uid='" + dataItem.uid + "']");

        if (<dataItem not deleteable>) $tr.find("a.k-grid-delete").remove();
        if (<dataItem not editable>) $tr.find("a.k-grid-edit").remove();
    }
}
豆芽 2024-10-25 22:18:11

就我而言,我使用了不同的 jQuery 函数来隐藏“编辑”按钮,看起来像这样。

创建样式:

.hide
 {
   display:none !important;
 }

在 onRowDataBound javascript 函数中:

if (e.dataItem.SomeValueA!= 4 || e.dataItem.SomeValueB != 16) {
   $(e.row).find('a.t-grid-edit').removeClass("t-button"); 
   $(e.row).find('a.t-grid-edit').addClass("hide");
}

希望,这有帮助。

In my case I used a different jQuery function to hide the Edit button and looks like that.

Create a style:

.hide
 {
   display:none !important;
 }

In the onRowDataBound javascript function:

if (e.dataItem.SomeValueA!= 4 || e.dataItem.SomeValueB != 16) {
   $(e.row).find('a.t-grid-edit').removeClass("t-button"); 
   $(e.row).find('a.t-grid-edit').addClass("hide");
}

Hope, this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文