【问题标题】:Expand/Collapse Table row展开/折叠表格行
【发布时间】:2015-10-31 11:26:33
【问题描述】:

我想要一个可以展开或折叠的表格。我的要求是当我单击表格行然后显示隐藏行时,或者当我单击其他表格行然后之前打开的行隐藏或显示相对隐藏行时。我发现一个非常适合我的 jquery jxpand 它显示隐藏行但是当我单击其他行时,以前的隐藏数据保持打开而不是隐藏我想要以前的隐藏或显示新的相关行隐藏数据。我的代码 sn-p 如下

$(document).ready(function(){
            $("#report tr:odd").addClass("odd");
            $("#report tr:not(.odd)").hide();
            $("#report tr:first-child").show();
            
            $("#report tr.odd").click(function(){
                $(this).next("tr").toggle();
                $(this).find(".arrow").toggleClass("up");
            });
            //$("#report").jExpand();
        });
#report { border-collapse:collapse;}
        #report h4 { margin:0px; padding:0px;}
        #report img { float:right;}
        #report ul { margin:10px 0 10px 40px; padding:0px;}
        #report th { background:#7CB8E2 url(header_bkg.png) repeat-x scroll center left; color:#fff; padding:7px 15px; text-align:left;}
        #report td { background:#C7DDEE none repeat-x scroll center left; color:#000; padding:7px 15px; }
        #report tr.odd td { background:#52edc7; cursor:pointer; }
        #report div.arrow { background:transparent url(arrows.png) no-repeat scroll 0px -16px; width:16px; height:16px; display:block;}
        #report div.up { background-position:0px 0px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

    <table id="report">
        <tr>
            <th>Country</th>
            <th>Population</th>
            <th>Area</th>
            <th>Official languages</th>
            <th></th>
        </tr>
        <tr>
            <td>United States of America</td>
            <td>306,939,000</td>
            <td>9,826,630 km2</td>
            <td>English</td>
            <td>show/hide</td>
        </tr>
        <tr>
            <td colspan="5">
                <img src="125px-Flag_of_the_United_States.svg.png" alt="Flag of USA" />
                <h4>Additional information</h4>
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/Usa">USA on Wikipedia</a></li>
                    <li><a href="http://nationalatlas.gov/">National Atlas of the United States</a></li>
                    <li><a href="http://www.nationalcenter.org/HistoricalDocuments.html">Historical Documents</a></li>
                 </ul>   
            </td>
        </tr>
        <tr>
            <td>United Kingdom </td>
            <td>61,612,300</td>
            <td>244,820 km2</td>
            <td>English</td>
            <td>show/hide</td>
        </tr>
        <tr>
            <td colspan="5">
                <img src="125px-Flag_of_the_United_Kingdom.svg.png" alt="Flag of UK" />
                <h4>Additional information</h4>
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/United_kingdom">UK on Wikipedia</a></li>
                    <li><a href="http://www.visitbritain.com/">Official tourist guide to Britain</a></li>
                    <li><a href="http://www.statistics.gov.uk/StatBase/Product.asp?vlnk=5703">Official 
                        Yearbook of the United Kingdom</a></li>
                </ul>
                
            </td>
        </tr>
        <tr>
            <td>India</td>
            <td>1,147,995,904</td>
            <td>3,287,240‡ km2</td>
            <td>Hindi, English</td>
            <td>show/hide</td>
        </tr>
        <tr>
            <td colspan="5">
                <img src="125px-Flag_of_India.svg.png" alt="Flag of India" />
                <h4>Additional information</h4>
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/India">India on Wikipedia</a></li>
                    <li><a href="http://india.gov.in/">Government of India</a></li>
                    <li><a href="http://wikitravel.org/en/India">India travel guide</a></li>
                 </ul>   
            
            </td>
        </tr>
        <tr>
            <td>Canada</td>
            <td>33,718,000</td>
            <td>9,984,670 km2</td>
            <td>English, French</td>
            <td>show/hide</td>
        </tr>
        <tr>
            <td colspan="5">
                <img src="125px-Flag_of_Canada.svg.png" alt="Flag of Canada" />
                <h4>Additional information</h4>
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/Canada">Canada on Wikipedia</a></li>
                    <li><a href="http://atlas.gc.ca/site/index.html" >Official 
                        Government of Canada online Atlas of Canada</a></li>
                    <li><a href="http://wikitravel.org/en/Canada">Canada travel guide</a></li>
                 </ul>   
            </td>
        </tr>
        <tr>
            <td>Germany</td>
            <td>82,060,000</td>
            <td>357,021 km2</td>
            <td>German</td>
            <td>show/hide</td>
        </tr>
        <tr>
            <td colspan="5">
                <img src="125px-Flag_of_Germany.svg.png" alt="Flag of Germany" />
                <h4>Additional information</h4>
                <ul>
                    <li><a href="http://en.wikipedia.org/wiki/Germany">Germany on Wikipedia</a></li>
                    <li><a href="http://www.deutschland.de/home.php?lang=2">Deutschland.de Official Germany portal</a></li>
                    <li><a href="http://www.cometogermany.com/">Germany Travel Info</a></li>
                 </ul>   
            </td>
        </tr>
    </table>
    <em>* Information taken from Wikipedia</em>

如果查看英国隐藏数据然后打开加拿大隐藏数据英国打开的数据将再次隐藏,加拿大数据将打开

【问题讨论】:

  • 请提供jsfiddle。

标签: javascript jquery html css


【解决方案1】:

只需在点击函数中添加$("#report tr:not(.odd)").hide();即可。

$("#report tr:odd").addClass("odd");
$("#report tr:not(.odd)").hide();
$("#report tr:first-child").show();

$("#report tr.odd").click(function () {
    $("#report tr:not(.odd)").hide();
    $(this).next("tr").toggle();
    $(this).find(".arrow").toggleClass("up");
});

JsFiddle

编辑 基于 cmets - 我们可以隐藏当前选定的元素,如下所示:

$("#report tr:odd").addClass("odd");
$("#report tr:not(.odd)").hide();
$("#report tr:first-child").show();

$("#report tr.odd").click(function () {
    var trToToggle = $(this).next("tr");
    $("#report tr:not(.odd)").not(trToToggle).hide();
    $(trToToggle).toggle();
    $(this).find(".arrow").toggleClass("up");
});

编辑 jsFiddle here

【讨论】:

  • 干得好,但它总是显示一个打开的行,我想显示或隐藏两者,就像我打开最后一行或者我想再次隐藏它但它保持打开而不是隐藏
  • 你的意思是说如果同一行被点击两次然后隐藏当前行。对吗?
  • 查看编辑的答案或查看 jsFiddle here
  • 干得好,感谢您的快速帮助。谢谢兄弟以你为荣。
  • 抱歉兄弟再次打扰,当我点击任何一行时 行也隐藏了你也可以在你的小提琴中看到
猜你喜欢
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2020-02-22
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多