Javascript JQuery DataTable Cell 从一行单击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3185391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:35:55  来源:igfitidea点击:

JQuery DataTable Cell from a row click

javascriptjquerydatatables

提问by nokheat

I am trying to implement a function on jquery datatable, that returns the 1st and the 4th column of a clicked row

我正在尝试在 jquery 数据表上实现一个函数,该函数返回单击行的第 1 列和第 4 列

i am following this example, which allows me to manipulate a clicked row http://datatables.net/examples/api/select_single_row.html

我正在关注这个例子,它允许我操纵点击的行 http://datatables.net/examples/api/select_single_row.html

thinking that i can change this handler to do the read cell value procedures and use the value on my own logic

认为我可以更改此处理程序来执行读取单元格值程序并在我自己的逻辑中使用该值

/* Add a click handler to the rows - this could be used as a callback */
$("#example tbody").click(function(event) {
    $(oTable.fnSettings().aoData).each(function (){
        $(this.nTr).removeClass('row_selected');
    });
    $(event.target.parentNode).addClass('row_selected');
});

i have also come over with this little code segment from dataTable forum http://datatables.net/forums/comments.php?DiscussionID=1384&page=1#Item_0

我也从 dataTable 论坛http://datatables.net/forums/comments.php?DiscussionID=1384&page=1#Item_0过来了这个小代码段

$('#example tbody tr').click( function () {
    // Alert the contents of an element in a SPAN in the first TD
    alert( $('td:eq(0) span', this).html() );
} );

may i have any pointer so i can get the 1st and 4th column of the clicked field?

我可以有任何指针,以便我可以获得单击字段的第 1 列和第 4 列吗?

next partI have the above solved, thanks nick

下一部分我已经解决了上述问题,谢谢尼克

however i have the next part of the problem. when i init the table i use

但是我有问题的下一部分。当我初始化我使用的表时

/* Init the table */
    oTable = $('#filetable').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/crvWeb/jsonFileList.do",
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax( {
                "dataType": 'json', 
                "type": "POST", 
                "url": sSource, 
                "data": aoData, 
                "success": fnCallback
            } );
        }
    } );

my servlet takes a dir request parameter and returns a listing to the datatable as json response.

我的 servlet 接受一个 dir 请求参数并将列表作为 json 响应返回到数据表。

/crvWeb/jsonFileList.do

how can i add and get serlvet response with post request so i can have my table updated?

我如何通过发布请求添加和获取 serlvet 响应,以便我可以更新我的表?

回答by Nick Craver

You can use .delegate()easiest here, like this:

您可以.delegate()在此处使用最简单的方法,如下所示:

$("#example tbody").delegate("tr", "click", function() {
  var firstCellText = $("td:first", this).text();
  var fourthCellText = $("td:eq(3)", this).text();
});

You can try out a demo here

您可以在此处试用演示

With .delegate()thisrefers to the <tr>since that's the click we're handling, making things quite a bit cleaner..and it's still only one event handler at the <tbody>level, not one per <tr>.

With指的是因为那是我们正在处理的点击,使事情变得更加清晰......而且它仍然在级别上只有一个事件处理程序,而不是每个..delegate()this<tr><tbody><tr>

回答by You

This should do the trick, if I'm reading your code correctly:

如果我正确阅读您的代码,这应该可以解决问题:

$("tr.row_selected td:nth-child(1), tr.row_selected td:nth-child(4)");

It should return the first and fourth child of all tr elements with the class row_selected.

它应该返回具有 class 的所有 tr 元素的第一个和第四个子元素row_selected