简体   繁体   English

如何在JQuery / HTML中执行多层嵌套表

[英]How to do multiple levels of nested tables in JQuery/HTML

I'm trying to create tables with multiple levels of nesting which can be expanded/collapsed. 我正在尝试创建具有可扩展/折叠的多层嵌套的表。 So the main table has rows which can be expanded to show the child rows in the middle of the table, who themselves can be expanded. 因此,主表具有可扩展的行,以显示表中间的子行,这些子行本身也可以扩展。

Each table needs to have their own headers and preferably the columns would all line up but have the start of the child tables be slightly indented as I try to show below. 每个表都需要有自己的标题,最好各列都排成一行,但子表的开头应稍稍缩进,如下所示。

For example: 例如:

ColHeader1              ColHeader2   // main table headers only shown at the top
record1                 ...
  ChildHeader1          ChildHeader2 // child headers shown for each table
  childrec1             ...
     SubChildHeader1    SubChildHeader2 // child headers shown for each table
     subchildrec1       ...
  childrec2             ...
record2                 ...
  ChildHeader1          ChildHeader2 // child headers shown for each table
  childrec5             ...
record3                 ...

I've tried to create the code by expanding a sample I found, but it doesn't work for expanding the inner most table. 我试图通过扩展找到的示例来创建代码,但不适用于扩展最里面的表。 The code can be found here: http://jsfiddle.net/afsz5brg/ 可以在这里找到代码: http : //jsfiddle.net/afsz5brg/

The end product will be in a ASP.Net MVC app, but for now I'm just trying to get it working in javascript/JQuery so that hopefully I can just change the data being sent to it. 最终产品将在ASP.Net MVC应用程序中,但是现在,我只是试图使其在javascript / JQuery中运行,以便希望我可以更改发送给它的数据。 I'm happy to consider alternative ways for doing it or be told if any of the code is doing something bad/deprecated. 我很高兴考虑执行此操作的替代方法,或者会被告知是否有任何代码做得不好/不推荐使用。

There are a couple of bugs I have discovered in your code. 我在您的代码中发现了几个错误。 I would list them one by one. 我会一一列出。

1) Your assumption of registering a callback on $('#detailsTable tbody td').on('click', ...); 1)您假设在$('#detailsTable tbody td').on('click', ...);上注册回调 and that being called on every new instance of that table that gets created is wrong. 并且在创建的该表的每个新实例上调用该方法是错误的。

If jQuery Data Table was internally cloning the #detailsTable element, then your code would work. 如果jQuery数据表在内部克隆#detailsTable元素,则您的代码将起作用。 But that isn't the case as you are retrieving the HTML markup of #detailsTable and returning it to fnOpen like so: oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, detailsTableHtml), 'details'); 但是情况并非如此,因为您正在检索#detailsTable的HTML标记并将其返回给fnOpen,如下所示: oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, detailsTableHtml), 'details'); so the callback never gets called. 因此回调永远不会被调用。

You need to register a callback to the newly created details table. 您需要向新创建的详细信息表注册回调。 Check out this JsFiddle to see how. 查看此JsFiddle以了解操作方法。

2) I think I also spotted another bug while reading your code. 2)我想我在阅读您的代码时也发现了另一个错误。 I haven't fixed it as I was not sure if I was right or not. 我还没有解决问题,因为我不确定自己是否正确。 In the callback for your #detailsTable, you call fnFormatDetails() instead of fnFormatSubDetails() like so oInnerTable.fnOpen(nTr, fnFormatDetails(iSubTableCounter, subDetailsTableHtml), 'subdetails'); 在#detailsTable的回调中,您调用fnFormatDetails()而不是fnFormatSubDetails(),如oInnerTable.fnOpen(nTr, fnFormatDetails(iSubTableCounter, subDetailsTableHtml), 'subdetails'); No one is calling fnFormatSubDetails() at the moment. 目前没有人在调用fnFormatSubDetails()。 Please check if this is a bug or not. 请检查这是否是错误。

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

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