2012-01-03 31 views
1

私は折りたたみ式テーブル・スクリプトを持って折りたたみ可能/拡張可能なテーブルの動作

..いくつかのjQuery/JS/JSPの機能を助ける必要があります。

(function($){ 
$.fn.extend({ 
    setupCollapstable: function(options) { 
     var defaults = { 
       displayStyle: "Image", 
       autoCheckbox: true, 
       toggleMinus: "/resource/images/collapse_close.gif", 
       togglePlus: "/resource/images/collapse_open.gif" 
     }; 

     var options = $.extend(defaults, options); 

     var toggleMinus = options.toggleMinus 
     var togglePlus = options.togglePlus 

     return this.each(function() { 

      //Creating a reference to the object 
      var obj = $(this); 

      //Break out the individual tbody elements 
      var rowGroups = $('tbody', obj); 

      //Loop through every tbody to setup collapsable and click events 
      $.each(rowGroups, function(intIndex, objValue){ 

       parentRow = $('th:first-child', objValue); 
       childCount = parentRow.parent().siblings().length; 

       if (childCount != 0) { 
        //console.log ("ParentRow: " + parentRow + " - ChildCount is: " + childCount); 

        parentRow.prepend('<img src="' + togglePlus + '" alt="collapse this section" />'); 

        parentRow.parent().siblings().fadeOut('fast'); 

        $('img', parentRow).addClass('clickable').click(function(e) { 
         e.preventDefault(); 
         var toggleSrc = $(this).attr('src'); 

         if (toggleSrc == toggleMinus) { 
          $(this).attr('src', togglePlus).parents('tr').siblings().fadeOut('fast'); 
         } else{ 
          $(this).attr('src', toggleMinus).parents('tr').siblings().fadeIn('fast'); 
         }; 
        }); 

        var parentCheckbox = $(':checkbox', parentRow.siblings()) 

        var childCheckboxes = $(':checkbox', parentRow.parent().siblings()); 

        parentCheckbox.click(function(e){ 

         var checked_status = parentCheckbox.attr('checked')?1:0; 

         childCheckboxes.each(function(){ 
          this.checked = checked_status;   
         }); 
        }); 

       } else { 
        parentRow.addClass("indented"); 
       } 

      }); 
       //console.dir (objValue); 

     }); 
    } 
}); 
})(jQuery); 

私のJSPから、私はそのように呼んで....

<script type="text/javascript" src="/resource/js/jquery.collapstable.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function(){ 
      $('table.collapstable').setupCollapstable(); 
     }); 
    </script> 

用法:

<table class="collapstable NoTableBorder"> 

イメージアイコンをクリックするとテーブルが適切に折りたたまれて展開されますが、(JSP上で)特定のチェックボックスがオンになっていると、最初に展開されます。

だから、私はonclick = "showOrHide()"機能を使ってテーブルを折りたたんだり展開したりしています。

私はビジュアル表現のために画像を添付しましたが、collpaseテーブルは太字のVISAヘッダーの横にある "+/-"アイコンで、表示用に展開する必要があるかどうかを決定するチェックボックスは "これですべてのヘルプは素晴らしいだろうアクティブ」チェックボックス.. collapse

...

+1

を拡大/だけで崩壊する前に条件を追加することができ、あなたがjsFiddleを投稿してくださいでした何か似たようなもの? – Matmarbon

+0

あなたは何を求めているのですか?showorhide関数を書く方法は? –

+0

ローレンス、本質的にはい... – OakvilleWork

答えて

0

あなたは、関数呼び出し

<script type="text/javascript"> 
    $(document).ready(function(){ 
     if (checkboxname.checked){ 
      // Set flags to show the table expanded 
     }else{ 
      // Set flags to show the table collapsed 
     } 
     $('table.collapstable').setupCollapstable(); 
    }); 
</script> 
+0

ラビ、これはありがとう! どのようにフラグを設定しますか? jQueryに慣れていないので、setupCollapstable関数のフラグを使用する方法がわかりません – OakvilleWork

+0

これは私の関数呼び出しの例です。 OakvilleWork

+0

$( "#[Table Id]")css( "display"、 "block"); //そのテーブルを展開する $( "#[Table Id]")なし"); //テーブルを折りたたむには このテーブルはdiv内にある必要があります。このテーブルを手動で再度展開することができます。 –

関連する問題