我正在使用 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 对象,以便我可以使用我更熟悉的语法。
之后我需要做的是:
- 获取“Actions”中的子元素(必须按列标题名称而不是单元格索引,因为列数可以更改)列的类 包含 t-grid-Edit、t-grid-edit、t-grid-Delete 或 t-grid-delete
采用这些元素并(这些操作中的每一个都将在使用类似设置的不同页面上使用):
一个。将元素的显示样式设置为无
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/