简体   繁体   English

如何响应用户在jQuery.dataTables中选择一行?

[英]How to respond to the user selecting a row in jQuery.dataTables?

I'm writing a web application using jQuery.dataTables. 我正在使用jQuery.dataTables编写Web应用程序。 I'm sure I did this some time before, but I'm having trouble figuring out the proper way to make my page respond to the user clicking on a row. 我敢肯定我之前有做过,但是我很难弄清楚使页面响应用户单击行的正确方法。

The basic idea is that I want to be able to respond to a click on a row by showing the user more information about that row. 基本思想是,我希望能够通过向用户显示有关该行的更多信息来响应该行的单击。 I would like to invoke some function when the row is clicked. 单击该行时,我想调用一些功能。 I may also need to place one or more buttons in each row that can be clicked on to do row-specific functionality, such as navigate to a form to edit or delete a row. 我可能还需要在每行中放置一个或多个按钮,可以单击它们以执行特定于行的功能,例如导航到表单以编辑或删除行。

$('#tblData').dataTable({
    data: data,
    columns: columns, // only column names specified
    bFilter: false,
    bPaginate: false,
    bInfo: false
});

I would have thought that the "Events" reference might answer this ( http://www.datatables.net/reference/event/ ), but I haven't had any such luck. 我本以为“事件”参考可能会回答这个问题( http://www.datatables.net/reference/event/ ),但是我还没有这样的运气。

To place an event on the table's row I would use the .on method: 要将事件放置在表的行上,我将使用.on方法:

$("#tblData").on('click','tbody > tr',function(e){ });

Then, if you place buttons inside the rows, you can do the same thing using the button selectors. 然后,如果将按钮放在行内,则可以使用按钮选择器执行相同的操作。 Just make sure you use e.stopPropagation() in the button click to keep the row click from firing. 只需确保在按钮单击中使用e.stopPropagation()即可防止触发行单击。

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

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