简体   繁体   English

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

[英]Get child elements from a specific cell in an HTMLTableRowElement and hide/remove them?

I am working with some custom ajax functionality using Telerik's MVC Grid and I am trying to hide/remove child elements of a cell based on specific criteria, I found a similar question here: Telerik MVC Grid making a Column Red Color based on Other Column Value but couldn't get it working right. 我正在使用Telerik的MVC网格使用一些自定义ajax功能,并且尝试根据特定条件隐藏/删除单元格的子元素,我在这里发现了一个类似的问题: Telerik MVC网格根据其他列的值使列为红色但无法正常工作。

Basically when the row is databound in the grid this event fires: 基本上,当行在网格中数据绑定时,此事件将触发:

 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
                //}
            }

        }
    }

So far it's working in that I can get and iterate through each column in the row, but I am not sure what to do at this point, I found this How can I get the corresponding table header (th) from a table cell (td)? 到目前为止,它的工作原理是我可以获取并遍历行中的每一列,但我不知道此时该怎么做,我发现了如何从表格单元格中获取相应的表格标题(t) )? to check to make sure the column name name is Actions, but I couldn't get it working. 检查以确保列名称名称为Actions,但我无法使其正常工作。 I am not sure how to convert the javascript HTMLTableCellElement object into a jQuery object so I can use syntax I am more familiar with. 我不知道如何将javascript HTMLTableCellElement对象转换为jQuery对象,因此我可以使用我更熟悉的语法。

Here is what I need to do after that: 以下是我之后需要做的事情:

  1. Get the child elements in the 'Actions' (has to go by column header name instead of cell index because the number of columns can change) column whose class contains t-grid-Edit, t-grid-edit, t-grid-Delete, or t-grid-delete 在“操作”(类中包含t-grid-Edit,t-grid-edit,t-grid-Delete)的“ Actions”(必须使用列标题名称而不是单元格索引,因为列数可以更改)中的子元素或t-grid-delete
  2. Take those elements and (each of these actions would be used on different pages using similar setups): 采取这些元素和(这些操作中的每一个将使用类似的设置在不同的页面上使用):

    • a. 一种。 Set the element's display style to none 将元素的显示样式设置为无

    • b. Add a class to the element of name "Hidden" 将一个类添加到名称为“隐藏”的元素中

    • c. C。 Remove the element from the code entirely 从代码中完全删除元素

How can I put the above functionality into my onRowDataBound function? 如何将上述功能放入我的onRowDataBound函数中?

Thank you SO =). 谢谢SO =)。

I was able to figure this out with a lot of playing: 我能够通过大量的游戏来解决这个问题:

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();

                    });
                }

            }); 
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM