简体   繁体   English

从Hyperlink中检索行元素数据,请在Jquery Datatables中单击

[英]Retrieve row element data from Hyperlink click in Jquery Datatables

I am new to Jquery Datatables. 我是Jquery Datatables的新手。 I have managed to create a table and add some elements into it. 我设法创建了一个表并向其中添加了一些元素。 This is What I have got . 这就是我所拥有的

I want to trigger the mouse click events on the hyperlinks in my table and retrieve the data in the data table of certain row. 我想在表中的超链接上触发鼠标单击事件,并检索某些行的数据表中的数据。 Such as "blockid" of clicked hyperlink's row. 如单击的超链接行的“ blockid”。 How can I do it? 我该怎么做?

Please advice. 请指教。 Thanks! 谢谢!

$(function () {
 $('#tbl_datablocks').dataTable( {
      "aoColumns": [
          { "sTitle": "blockid","sName": "blockid"},
          { "sTitle": "Name" },
          { "sTitle": "Created Time" },
          { "sTitle": "Updated Time", "sClass": "center" },
          { "sTitle": "Updated Time", "sClass": "center" }
          ]    } );


$('#tbl_datablocks').dataTable().fnAddData(["id1","data1","data2","data3","<a href='#' class='retrievedata' onclick='retrievedata(this)'>Read</a>"]);
$('#tbl_datablocks').dataTable().fnAddData(["id2","data1","data2","data3","<a href='#' class='retrievedata' onclick='retrievedata(this)'>Read</a>"]);
});   

function retrievedata(which)
{
    alert($(which).parents("tr"));
}
​

Have you considered using the data attributes? 您是否考虑过使用数据属性? When creating your anchor <a> , add an attribute data-blockid with the correct value. 创建锚点<a> ,添加具有正确值的属性data-blockid When jQuery registers the click event, you can use $(this).data("blockid") to get the value. 当jQuery注册click事件时,您可以使用$(this).data("blockid")获得该值。

Here's an updated example 这是一个更新的示例

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

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