2011-01-25 2 views
1

2つの異なるソースを使用して詳細行を切り替えようとしています。 2つの異なるトグル機能を使用して同じテーブル行を切り替えます

  1. ユーザーが名前またはAddressAlertのいずれかをクリックすると

    は、その具体的な詳細行を示すか、ユーザーがクリックした場合、すべての詳細行が表示または非表示を受ける「すべてを切り替える」
  2. 隠されます。

問題は、2つの別個のトグル機能が、他のトグル機能が何をしているのかわからないことです。たとえば、「すべてをトグルする」をクリックしただけで、すべての詳細行が表示された場合、個別の名前をクリックするとその詳細行が非表示になります。しかし、個々のトグル機能は「表示」までであるため、その行の詳細を非表示にするには2回のクリックが必要です。

HTML:

<div id="toggleAll"></div> 
<table> 
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr> 
    <tr class="details"></tr> 
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr> 
    <tr class="details"></tr> 
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr> 
    <tr class="details"></tr> 
</table> 

のjavascript:

//toggles beween showing and hiding the details for specific row 
$(
    function(){ 
     //if click on carrier name or address alert symbol 
     $('.name, .addressAlert').toggle(
      function() { 
      //gets the row clicked on, and finds the next row (the details row) 
      detailsTds = $(this).parents("tr.infoRow").next(); 
      //adds the "shown" class, which sets the display to table-row 
      detailsTds.addClass("shown"); 
      }, 
      function(){ 
      //gets the row clicked on, and finds the next row (the details row) 
      detailsTds = $(this).parents("tr.infoRow").next(); 
      //removes the "shown" class, thereby setting the display to none 
      detailsTds.removeClass("shown"); 
      } 
     ); 
    } 
); 

//toggle ALL details 
$(
    function(){ 
     //if click on carrier name or address alert symbol 
     $('#toggleAll').toggle(
      function() { 
      $('.details').addClass("shown"); 
      $('#toggleAll').text('[-] Hide all addresses'); 
      }, 
      function(){ 
      $('.details').removeClass("shown"); 
      $('#toggleAll').text('[+] Show all addresses'); 
      } 
     ); 
    } 
); 
+0

jqueryの価値があるものについては、自分自身を熟知する価値があるかもしれません。同じことをトグルする2つの事柄を直接手助けすることはありませんが、2つのほぼ同じ機能を持たないクリーナーコードを書くことができます(一般的にadd/removeクラスコールによって違いがあります)。 – Chris

答えて

2

あなたは(クリックを使用することができます)の代わりにトグルの()、その後、現在に適用されるクラスに基づいて表示または非表示行。

0

クラスに直接参照できますか?あなたが最初のトグルに機能を置き換えるものではありませんなぜ

+0

私はあなたが意味することを理解していません... – dmr

+0

あなたは$( "。infoRow")のようなCLASSから行のDOMに直接アクセスでき、あなたが意図した通りにmodifyit ...私はそれが溶液 –

0

: 「$切り替える( 『名前、.addressAlert。』)。」

をし、その後に示すクラス - ので、最初にすべてのinfoRowsを初期化しますユーザーが "ALL toggle"を押してもそれが表示されます。

関連する問題