简体   繁体   English

如何展开和折叠(即切换)表行(tr)?

[英]How do I expand and collapse (i.e. toggling) a table row (tr)?

With my preexisting knowledge (for example this one ) I have seen so far that a div container can easily be toggled (ie hide and show). 凭借我现有的知识(例如, 这一知识),到目前为止,我已经了解到可以轻松切换div容器(即隐藏和显示)。 However I am pretty confused when I have some data inside tr and I want to display and hide few items once that tr is clicked. 但是,当我在tr内有一些数据并且想要单击tr要显示和隐藏一些项目时,我感到非常困惑。 Let's consider a food menu (I name it "Rice"), and there are few sub menus under that category (I name them "Fried rice", "Boiled rice"...). 让我们考虑一个食物菜单(我将其命名为“饭”),并且该类别下的子菜单很少(我将其命名为“炒饭”,“煮饭” ...)。 Once the "Rice" is clicked (or possibly if I have a arrow or plus icon to click on), the sub menus should get displayed. 单击“大米”后(或者如果我有一个箭头或加号图标可以单击),应该显示子菜单。 Similarly, they should hide themselves once the arrow is clicked again. 同样,一旦再次单击箭头,他们应该隐藏自己。 Please see in this website how they toggle the restaurant menu with arrow key. 请在此网站上查看他们如何使用箭头键切换餐厅菜单。 I want to do the exactly same thing. 我想做完全一样的事情。

The code I am working on: 我正在处理的代码:

    <div class="media-right">
        <table class="table">
            <tr>
                <td>
                    <a href="#"><h3 class="menu-title">Rice</h3></a>
</td> <!--make this tr expandable and collapsable-->
                <td>
                    <div class="menu-rate">&#36;100.00</div>
                </td>
            </tr>

             <tr>
                <td>
                    <a href="#"><h3 class="menu-title">Fried Rice</h3></a></td>
                <td>
                    <div class="menu-rate">&#36;50.00</div>
                </td>
            </tr>
            <tr>
                <td>
                    <a href="#"><h3 class="menu-title">Boiled Rice</h3></a></td>
                <td>
                    <div class="menu-rate">&#36;25.00</div>
                </td>
            </tr>
         </table>   
    </div>

You can try a class "Accordion" which has the similar functionality. 您可以尝试具有类似功能的“ Accordion”类。

You can find it here in detail. 您可以在这里找到详细信息。

<script>
    $(function(){
      $('.menu-rate').click(function(){
        $(this).closest('.container').toggleClass('collapsed');
      });

    });
</script>

Something like this? 像这样吗

 function Rice(){ if(document.getElementById("Rice").style.display == "none"){ document.getElementById("Rice").style.display = "block"; }else{ document.getElementById("Rice").style.display = "none"; } } function boiledRice(){ if(document.getElementById("boiledRice").style.display == "none"){ document.getElementById("boiledRice").style.display = "block"; }else{ document.getElementById("boiledRice").style.display = "none"; } } function friedRice(){ if(document.getElementById("friedRice").style.display == "none"){ document.getElementById("friedRice").style.display = "block"; }else{ document.getElementById("friedRice").style.display = "none"; } } 
  <div class="media-right"> <table class="table"> <tr> <td> <a onclick="Rice()"><h3 class="menu-title">Rice</h3></a><div id="Rice" style="display:none;"><ul><li>1</li><li>2</li><li>3</li></ul></div></td> <!--make this tr expandable and collapsable--> <td> <div class="menu-rate">&#36;100.00</div> </td> </tr> <tr> <td> <a onclick="friedRice()"><h3 class="menu-title">Fried Rice</h3></a><div id="friedRice" style="display:none;"><ul><li>1</li><li>2</li><li>3</li></ul></div></td> <td> <div class="menu-rate">&#36;50.00</div> </td> </tr> <tr> <td> <a onclick="boiledRice()"><h3 class="menu-title">Boiled Rice</h3></a><div id="boiledRice" style="display:none;"><ul><li>1</li><li>2</li><li>3</li></ul></div></td> <td> <div class="menu-rate">&#36;25.00</div> </td> </tr> </table> </div> 

Is there a constraint that is forcing you to utilize a table? 是否存在强制您使用表的约束? Since you're already using jQuery, you could easily achieve this type of functionality with jQuery UI's accordion widget. 由于您已经在使用jQuery,因此可以通过jQuery UI的手风琴小部件轻松实现这种功能。

$( "#accordion" ).accordion({
  collapsible: true
});

Here's a basic example . 这是一个基本示例

If you're open to use a library for this, you can try using bootstrap-table by wenzhichin. 如果您愿意为此使用库,则可以尝试使用wenzhichin的bootstrap-table。 I find it very flexible. 我觉得它很灵活。

Subtable - http://issues.wenzhixin.net.cn/bootstrap-table/#options/sub-table.html 子表- http://issues.wenzhixin.net.cn/bootstrap-table/#options/sub-table.html

Collapsing row - http://issues.wenzhixin.net.cn/bootstrap-table/#methods/expandRow-collapseRow.html 折叠行-http: //issues.wenzhixin.net.cn/bootstrap-table/#methods/expandRow-collapseRow.html

checkout collapsible elements on material design made by google, really helpful an easy to use: google制作的材料设计中的checkout可折叠元素,非常易于使用:

http://materializecss.com/collapsible.html http://materializecss.com/collapsible.html

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

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