简体   繁体   English

jQuery DataTable - 在点击时获取最近的td元素类

[英]jQuery DataTable - Get class of closest td element on click

Given a jQuery Datatable, how can I determine if a click occurs on a certain column? 给定jQuery Datatable,如何确定某个列上是否发生了单击?

I have a jQuery DataTable instance populated with assorted data that is a summary of a database record. 我有一个jQuery DataTable实例,其中填充了各种数据,这些数据是数据库记录的摘要。 Currently, a click on a row opens a dialog box with all the fields of the corresponding database record. 目前,单击一行会打开一个对话框,其中包含相应数据库记录的所有字段。 What I want to do is open a second dialog box if the column clicked has a specified class 'comments' 如果单击的列具有指定的类“注释”,我想要做的是打开第二个对话框

Currently, I initialize the datatable via 目前,我通过初始化数据表

timelineTable = $("#timeline_table").dataTable({
    "aLengthMenu" : [
        ["-1", "10", "25", "50", "100"],
        ["All", "10", "25", "50", "100"]
    ],
    "iDisplayLength" : -1,
    "aoColumnDefs" : [
        {"bVisible" : false, "aTargets" : [6, 8] },
        {"sClass" : "comments", "aTargets" : [7]} //adds class 'comments' to this column index
    ]
});

and have a click event bound to the dynamically populated rows: 并将一个click事件绑定到动态填充的行:

$("#timeline_table").on("click", "tbody tr", timelineTableClickHandler);

This works fine for the default action of opening up the detail dialog, but I want to modify the function handler to 这适用于打开详细信息对话框的默认操作,但我想修改函数处理程序

$("#timeline_table").on("click", "tbody tr", function(e){
  if ($(this).closest("td").hasClass("comments")) 
    //call secondary dialog open event
  else
    //call default dialog open event
});

However, $(this).closest("td").hasClass("comments") is returning undefined, and when I put a breakpoint on that in Firebug, the following commands and output result 但是, $(this).closest("td").hasClass("comments")返回undefined,当我在Firebug中放置一个断点时,以下命令和输出结果

$(this).attr("id") prints out row id
$(this).closest("td") prints out jQuery( )
$(this).closest("td").text() prints out "", should be 0

The problem is here this is pointing to the tr , as you well know td is a child of tr so you cannot use .closest() on this to find the clicked td 问题就在这里this是指向tr ,你很清楚td是一个孩子tr ,所以你不能使用.closest()this找到点击TD

Instead you can use the target property of the event to find the actual source of the event and find the closest td of that element 相反,您可以使用eventtarget属性来查找event的实际来源,并找到该元素的closest td

$("#timeline_table").on("click", "tbody tr", function(e){
  if ($(e.target).closest("td").hasClass("comments")) 
    //call secondary dialog open event
  else
    //call default dialog open event
});

There's a mistake I think. 我认为这是一个错误。

tr has no closest td because td is a children of tr. tr没有最接近的td,因为td是tr的子节点。 The closest element to tr is regularry tbody or table. 与tr最接近的元素是常规的tbody或table。

To access the td's you have to use: 要访问td,您必须使用:

$("#timeline_table").on("click", "tbody tr", function(e){
 if ($(e.target).children("td:eq(indexNo)").hasClass("comments")) 
  //call secondary dialog open event
 else
  //call default dialog open event

}); });

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

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