【问题标题】:Can a table row expand and close? [closed]表格行可以展开和关闭吗? [关闭]
【发布时间】:2011-04-23 06:45:27
【问题描述】:

是否可以让表格行展开和折叠?任何人都可以参考我的脚本或示例吗?

如果可能的话,我更喜欢 jQuery。我有一个想要实现的绘图概念:

【问题讨论】:

  • 使用 Jquery 向下滑动和向上滑动。 api.jquery.com/slideDown
  • 你能举个例子来说明使用的标记吗?我猜他可以只使用<div>s 的列表,但这似乎不太符合语义...:\
  • @Christian:这将是 DIV 的 OL。
  • OP 卖小孩的事,没人惊恐吗?
  • @AndrewMairose 一开始在我看来是这样,但如果你仔细观察,它是关于捐赠给特定的孩子(这对我来说仍然很奇怪,但无论如何)。不过,“添加到购物车”按钮的措辞可能不那么尴尬!

标签: jquery tablelayout


【解决方案1】:

是的,表格行可以上下滑动,但它很难看,因为它会改变表格的形状,让所有东西都跳起来。取而代之的是,在每个 td 中添加一个元素,这样才有意义,例如 ph2 等。

至于实现表格滑动切换...

将点击处理程序放在整个表格上可能是最简单的,.stopPropagation()check what was clicked

如果单击带有 colspan 的行中的 td,请关闭其中的 p。如果它不是带有 colspan 的连续 td,则关闭然后切换下一行的 p

必须将所有书面内容包装在tds 内的一个元素中,因为你永远不想slideUp tdtr 否则表格形状会改变!

类似:

$(function() {
  
      // Initially hide toggleable content
    $("td[colspan=3]").find("p").hide();

      // Click handler on entire table
    $("table").click(function(event) {

          // No bubbling up
        event.stopPropagation();

        var $target = $(event.target);

          // Open and close the appropriate thing
        if ( $target.closest("td").attr("colspan") > 1 ) {
            $target.slideUp();
        } else {
            $target.closest("tr").next().find("p").slideToggle();
        }                    
    });
});​

Try it out with this jsFiddle example.

... and try out this jsFiddle showing implementation of a + - - toggle.

HTML 只需要有几行交替的 tds 行,然后是一行 td 的 colspan 大于 1。显然,您可以很容易地调整细节。

HTML 看起来像:

<table>
    <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
    <tr><td colspan="3"><p>Blah blah blah blah blah blah blah.</p>
    </td></tr>

    <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
    <tr><td colspan="3"><p>Blah blah blah blah blah blah blah.</p>
    </td></tr>
    
    <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
    <tr><td colspan="3"><p>Blah blah blah blah blah blah blah.</p>
    </td></tr>    
</table>​

【讨论】:

  • @Peter-Ajtai 你能帮我解决这个问题吗? stackoverflow.com/questions/23916317/…
  • @peter 我也有类似的问题,但我的 HTML 表格是完全动态的,如果你有时间请帮助stackoverflow.com/questions/55919420/…
  • 您好,我尝试了这个解决方案,目前它运行良好。但是,如果我单击展开行的最底部(端口展开),它将使展开的行完全消失,如果不刷新页面就无法重新展开。
  • 对于您的小提琴,如果您单击展开行的顶部,它的行为似乎相同。
  • 下面是我的问题的链接。提供的答案修复了此解决方案错误,您无法再看到可展开的行。 stackoverflow.com/questions/57776725/…
【解决方案2】:

你可以这样做:

HTML

<table>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
        <td>Cell 4</td>
        <td><a href="#" id="show_1">Show Extra</a></td>
    </tr>
    <tr>
        <td colspan="5">
            <div id="extra_1" style="display: none;">
                <br>hidden row
                <br>hidden row
                <br>hidden row
            </div>
        </td>
    </tr>
</table>

jQuery

$("a[id^=show_]").click(function(event) {
    $("#extra_" + $(this).attr('id').substr(5)).slideToggle("slow");
    event.preventDefault();
});

查看demo on JSFiddle

【讨论】:

    【解决方案3】:

    这取决于你的标记,但它肯定可以工作,我使用了以下:

    jQuery

    $(document).ready(
      function() {
      $('td p').slideUp();
        $('td h2').click(
          function(){
           $(this).siblings('p').slideToggle();
          }
          );
      }
      );
    

    html

      <table>
      <thead>
        <tr>
          <th>Actor</th>
          <th>Which Doctor</th>
          <th>Significant companion</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><h2>William Hartnell</h2></td>
          <td><h2>First</h2><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></td>
          <td><h2>Susan Foreman</h2><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p></td>
        </tr>
        <tr>
          <td><h2>Patrick Troughton</h2></td>
          <td><h2>Second</h2><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></td>
          <td><h2>Jamie MacCrimmon</h2><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p></td>
        </tr>
        <tr>
          <td><h2>Jon Pertwee</h2></td>
          <td><h2>Third</h2><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></td>
          <td><h2>Jo Grant</h2><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p></td>
        </tr>
      </tbody>
    </table>
    

    我处理它的方法是折叠行的单元格中的特定元素,这样,在我的情况下,行将 slideUp() 因为段落被隐藏,并且仍然留下一个元素 h2 来点击以重新显示内容。如果该行完全倒塌,就没有显而易见的方法可以将其恢复。

    Demo at JS Bin


    正如@Peter Ajtai 所指出的,在 cmets 中,上述方法仅关注一个单元格(尽管是故意的)。要扩展所有子 p 元素,这将起作用:

    $(document).ready(
      function() {
      $('td p').slideUp();
        $('td h2').click(
          function(){
           $(this).closest('tr').find('p').slideToggle();
          }
          );
      }
      );
    

    Demo at JS Bin

    【讨论】:

    • 很好的解决方案,尽管@Erik 的布局要求隐藏单元格完全位于不同的行中,因为它跨越了上一行中的多个其他单元格。
    • 没错,不幸的是,尝试使用同级表行来实现此解决方案会导致各种不吸引人的跳跃。不过,可能通过仔细嵌套的列表就可以完成。
    • 您可以使用siblingnext.find("p"),这样您就不必折叠tdtr,然后您可以轻松实现colspan 解决方案。 - 查看我的解决方案。
    • 如果单击打开的行,您的 jsBin 示例不会关闭该行。 ----- 同样令人困惑的是,当您单击某些内容时,会打开整行,但只显示与您单击的一个单元格相关的信息(同一行中的其他信息保持隐藏)。跨度>
    • @Peter,可能;但这更像是对意图的快速演示,而不是经过完善的完成实施。
    【解决方案4】:

    jQuery

    $(function() {
        $("td[colspan=3]").find("div").hide();
        $("tr").click(function(event) {
            var $target = $(event.target);
            $target.closest("tr").next().find("div").slideToggle();                
        });
    });
    

    HTML

    <table>
        <thead>
            <tr>
                <th>one</th><th>two</th><th>three</th>
            </tr>
        </thead>
        <tbody>
    
            <tr>
                <td><p>data<p></td><td>data</td><td>data</td>
            </tr>
            <tr>
                <td colspan="3">
                    <div>
                        <table>
                                <tr>
                                    <td>data</td><td>data</td>
                                </tr>
                        </table>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    

    这很像前面的例子。我在尝试实现该示例时发现,如果在未展开的情况下单击要展开的表格行,它将消失,并且不再可展开

    为了解决这个问题,我只是删除了单击可展开元素以进行向上滑动的功能,并使其只能使用上面的表格行进行切换。

    我还对 HTML 和相应的 jQuery 做了一些小的改动。

    注意:我本来只是发表评论,但还不允许发表长篇文章。只是想发布这个,因为我花了一点时间才弄清楚消失的表格行发生了什么。

    感谢彼得·阿杰泰

    【讨论】:

      【解决方案5】:

      回答你的问题,不。不过,这将是可能的 div 。唯一的问题是,如果功能是用 div 而不是表来完成的,会引起混乱。

      【讨论】:

        【解决方案6】:

        好吧,我会说使用 DIV 而不是表格,因为它会更容易(但使用表格并没有错)。

        我的方法是使用 jQuery.ajax 并从服务器请求更多数据,这样,选定的 DIV(或 TD,如果您使用表)将根据请求的内容自动扩展。

        这样,它可以节省带宽并使其运行速度更快,因为您不会一次加载所有内容。它仅在被选中时加载。

        【讨论】:

        • 这涵盖了扩展。崩溃呢?
        • 只需添加一个函数即可在单击时删除选定的 DIV 元素。那将“崩溃”。您需要添加 delegate() 函数让 jQuery 将函数绑定到新元素。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 2014-01-31
        • 1970-01-01
        相关资源
        最近更新 更多