javascript - 从 HTMLTableRowElement 中的特定单元格获取子元素并隐藏/删除它们?

标签 javascript jquery ajax grid telerik-mvc

我正在使用 Telerik 的 MVC 网格处理一些自定义 ajax 功能,并且我正在尝试根据特定条件隐藏/删除单元格的子元素,我在这里发现了一个类似的问题:Telerik MVC Grid making a Column Red Color based on Other Column Value但无法使其正常工作。

基本上,当行在网格中进行数据绑定(bind)时,会触发此事件:

 function onRowDataBound(e) {
        if (e.dataItem.Status == "Submitted") {
            $.each(e.row.cells, function (index, column) {
                alert(column.innerHTML);
                //var $header = $(column).closest('table').find('th').eq($(column).index());
                //if(header text == "Actions)
                //{
                     //Get the <a> child elements in the 'Actions' column whose class contains t-grid-Edit, 
                     //t-grid-edit, t-grid-Delete, or t-grid-delete and set their display to none, add a css class, and remove the element entirely
                //}
            }

        }
    }

到目前为止,我可以获取并遍历行中的每一列,但我不确定此时该做什么,我发现了这个 How can I get the corresponding table header (th) from a table cell (td)?检查以确保列名称名称为 Actions,但我无法使其正常工作。我不确定如何将 javascript HTMLTableCellElement 对象转换为 jQuery 对象,以便我可以使用我更熟悉的语法。

之后我需要做的是:

  1. 获取“Actions”中的子元素(必须按列标题名称而不是单元格索引,因为列数可以更改)列的类 包含 t-grid-Edit、t-grid-edit、t-grid-Delete 或 t-grid-delete
  2. 采用这些元素并(这些操作中的每一个都将在使用类似设置的不同页面上使用):

    • 一个。将元素的显示样式设置为无

    • b。给名称为“Hidden”的元素添加一个类

    • c。从代码中完全删除该元素

如何将上述功能放入我的 onRowDataBound 函数中?

谢谢你=)。

最佳答案

我玩了很多次才弄明白:

function onRowDataBound(e) {
        if(e.dataItem.Status == "Submitted"){
            var $row = $(e.row);
            $.each($row.children("td"), function (index, column) {
                var $column = $(column);
                var $headerText = $column.closest('table').find('th').eq($column.index()).children(".t-link").text();
                if ($headerText == "Actions") {
                    $.each($column.children("a.t-grid-delete, a.t-grid-Edit"), function (subIndex, link) {
                        $(link).remove();

                    });
                }

            }); 
        }
    }

关于javascript - 从 HTMLTableRowElement 中的特定单元格获取子元素并隐藏/删除它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9283938/

相关文章:

javascript - Chrome 中的平滑滚动功能卡住了

javascript - Rails 上的 js 连接遇到困难

javascript - 使用 PHP 脚本从数据库获取动态结果(AJAX 调用)的 JS 框架/库

php - Ajax 完成后返回 javascript 代码

javascript - JS 中的乘法不起作用

Javascript 如果为 null 则忽略并继续剩余脚本

javascript - iOS 8 故障影响窗口大小调整

javascript - 如何使用 jQuery 在 delay() 之后重定向?

javascript - Ajax:如何从其 ID wordpress 获取帖子

javascript - jquery.dataTables.min.js :48 Uncaught TypeError: Cannot read property 'length' of undefined