简体   繁体   English

JQuery DataTables:显示/隐藏具有多个表的行详细信息

[英]JQuery DataTables: Show/hide row details with multiple tables

I'm aware there's a similar question on here JQuery DataTables: How to show/hide row details with multiple tables? 我知道这里有一个类似的问题JQuery DataTables:如何显示/隐藏具有多个表的行详细信息? but that doesn't apply to my current problem completely. 但这并不完全适用于我当前的问题。

I have the code: 我有代码:

var oTable = $('.dataTable').dataTable( {
    "aoColumnDefs": [
     { "bSortable": false, "aTargets": [ 0,3,4 ] },
     { "bVisible": false, "aTargets": [ 4 ] }
    ],  
    "aoColumns": [
      null,
      null,
      null,
      null,
      { "sType": "html" }
    ],    
    "aaSorting": [[1, 'asc']],
    "bFilter": false,
    "bPaginate": false,
    "fnInitComplete": function(oSettings) {
      /* Add event listener for opening and closing details
      * Note that the indicator for showing which row is open is not controlled by DataTables,
      * rather it is done here
      */

    $('.dataTable tbody td img').live('click', function () {
         var nTr = this.parentNode.parentNode;

         if (oTable.fnIsOpen(nTr)) {
           // This row is already open - close it
           this.src = "css/images/details_open.png";
           oTable.fnClose(nTr);
         } else {
           // Open this row
           this.src = "css/images/details_close.png";
           oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'details');
         }  
        } );        
      }
});

That works if there's only one if I add the dataTable class to a second table, then they work as datatables but the show/hide buttons fail in both tables. 如果将dataTable类添加到第二个表中只有一个,那将起作用,然后它们作为数据表工作,但两个表中的show / hide按钮均失败。 Both tables have the same count of fields and content, just for the sake of making it work, but still no success. 这两个表具有相同的字段和内容计数,只是为了使其起作用,但仍然没有成功。

On the similar post, the person suggests adding: 该人在类似的帖子中建议添加:

tbl = $(this).parent().parent().dataTable();

to the click function but I have tried that and it didn't work. 单击功能,但我已经尝试过了,但是没有用。

What am I missing?? 我想念什么?

In short: get rid of the fnInitComplete, and move the "live" call to below the dataTable call. 简而言之:摆脱fnInitComplete,并将“实时”调用移到dataTable调用下方。

As an example, if you have three tables, after each table is completed, your current code will execute the fnInitComplete method - so fnInitComplete gets called 3 times. 例如,如果您有三个表,则在每个表完成之后,您当前的代码将执行fnInitComplete方法-因此fnInitComplete被调用3次。 Your fnInitComplete uses a selector to "live" the click event to an img, and the selector will "live" to all tables. 您的fnInitComplete使用选择器将“点击”事件“实时”存储到img,并且选择器将“实时”显示所有表。 This results in multiple bindings. 这导致多个绑定。 See this jsfiddle, http://jsfiddle.net/KeVwJ/ , which duplicates your method. 请参阅此jsfiddle, http://jsfiddle.net/KeVwJ/ ,其中复制了您的方法。 (Note that I'm not using images so only capturing click on the td cell, not an image). (请注意,我没有使用图像,因此仅捕获td单元中的单击,而不捕获图像)。

var oTable = $('.dataTable').dataTable( {
    "bFilter": false,
    "bPaginate": false,
    "fnInitComplete": function(oSettings) {
        $('.dataTable tbody td').live('click', function () {
             var nTr = this.parentNode;
            alert(nTr);
        } );        
      }
});

If you click on any row in the table, you will get 3 alert boxes, because 3 tables are created and they each "live" click for all tables at fnInitComplete. 如果单击表中的任何行,您将获得3个警报框,因为创建了3个表,并且它们对fnInitComplete上的所有表均进行“实时”单击。

To fix, remove the fnInitComplete, and put the code for "live" after the call to dataTable. 要解决此问题,请删除fnInitComplete,并在对dataTable的调用之后放置“ live”代码。 That should solve it. 那应该解决。 See this jsfiddle: http://jsfiddle.net/rgMNu/ Click on any row in the table and it will identify the correct table class. 请参阅以下jsfiddle: http : //jsfiddle.net/rgMNu/单击表中的任何行,它将标识正确的表类。 Again since I am capturing the click on td, I only have to do this.parentNode.parentNode.parentNode. 同样,由于我捕获了对td的点击,因此只需要执行this.parentNode.parentNode.parentNode。 I think you'll have to do another level. 我认为您必须再做一个级别。

$('.dataTable tbody td').live('click', function () 
        {
            var t = this.parentNode.parentNode.parentNode;
            alert(jQuery(t).attr('class'));
        } );        

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

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