简体   繁体   English

jQuery隐藏表格行

[英]jQuery hide table row

I have the following jQuery script which hides/expands row in table: 我有以下jQuery脚本,它隐藏/扩展表中的行:

it was inspired by this example: 它受到以下示例的启发:

http://www.jankoatwarpspeed.com/examples/expandable-rows/ http://www.jankoatwarpspeed.com/examples/expandable-rows/

<script type="text/javascript">  

$(document).ready(function(){
    $("#orders tr:odd").addClass("odd");
    $("#orders tr:not(.odd)").hide();
    $("#orders tr:first-child").show();

    $("#orders tr.odd").click(function(){
        $(this).next("tr").toggle();
        $(this).find(".toggler").toggleClass("on");
        $(this).find(".toggler").text("hide");

    });
});

</script>        

My HTML is the following: 我的HTML如下:

<table class="blueWrapperTbl" id="orders">
  <tbody>
    <tr>
      <td>  <h:outputText value="#{order.orderId}" /> </td>
      <td>  <h:outputText value="#{order.orderId}" /> </td>
      <td>  <h:outputText value="#{order.orderId}" /> </td>
      <td>  <h:outputText value="#{order.orderId}" /> </td>
      <td><a href="#" class="ubber"><b>Change Order</b></a></td>
      <td><a href="#" class="toggler">Show</a></td>
    </tr>
    <tr>
    ...
    </tr> 
</table>

The relevant CSS parts is the following: 相关的CSS部分如下:

.blueWrapperTbl .toggler { background:url(../images/plus.gif) right center no-repeat; padding-right:20px; display:inline-block; text:'fff'}
.blueWrapperTbl .toggler.on { background:url(../images/minus.gif) right center no-repeat; }

I need to do the following: 我需要执行以下操作:

  1. When my row is expended I would like to change 'toggler' text from 'Show' to 'hide'. 当我的行用完时,我想将“切换”文本从“显示”更改为“隐藏”。
  2. When my row is hidden I dont need to show 'Change order' URL. 当我的行被隐藏时,我不需要显示“更改订单” URL。 It must be hidden. 它必须是隐藏的。

Please help me. 请帮我。

The toggle() and toggleClass() functions are convenience functions that make it easier to do simple things... but they are limited. toggle()toggleClass()函数是便捷函数,可以使做简单的事情变得更加容易...但是它们受到限制。

Rather than use toggleClass() , you might consider using an if to check to see if the element has the 'on' class. 您可以考虑使用if来检查元素是否具有“ on”类,而不是使用toggleClass() If it does, remove that class and change the text to 'Show'. 如果是这样,请删除该类,然后将文本更改为“ Show”。 If it doesn't, add the 'on' class and change the text to 'Hide'. 如果不是,请添加“ on”类,然后将文本更改为“ Hide”。

Rather than using toggle() , you could check for the visibility of the row. 您可以检查行的可见性,而不是使用toggle() If it's visible, hide it and hide the 'Change order' URL. 如果可见,将其隐藏并隐藏“更改订单” URL。 If it's not visible, show it and show the 'Change order' URL. 如果看不到它,请显示它并显示“更改订单” URL。

Here's a notional example: 这是一个概念示例:

$('#orders td a')
    .each(function(){
        if( $(this).hasClass('on') ) {
            $(this).removeClass('on');
            // do other things
        } else {
            $(this).addClass('on');
            // do other things
        }
    }
;

I hope this helps! 我希望这有帮助!

1) Instead of 1)代替

$(this).find(".toggler").text("hide");

use 采用

$(this).find(".toggler").html("hide");

2) Add this to your click handler function 2)将此添加到您的点击处理函数

$(this).find(".ubber").toggle();

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

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