| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

JavaScript 行列を固定したテーブル

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

JavaScript 行列を固定したテーブル

JavaScript |

jQueryが出てきてから、すごく楽になった。。。 ↓こんなことしなくても、jQueryプラグイン使えばよい。

インラインフレーム等を使わずに、行列を固定したテーブルを実現

0708 scrl tbl.jpg テンプレート:Ref scroll table sample.lzh

画面のリサイズ対応

HTML
  1. <body onresize="resizePanel();" ・・・
  2. <div id="HEADER_PANEL" style="overflow:hidden;">
  3. <table styel="table-layout:fixed;" ・・・
  4. </div>
  5. <div id="ITEM_PANEL" style="overflow:hidden;">
  6. <table style="overflow:auto;" ・・・
  7. </div>
JavaSctript
  1. /**
  2. * 画面のサイズ変更から呼び出す
  3. */
  4. function resizePanel() {
  5. resizeTablePanel(
  6. 80
  7. , 30
  8. ,document.getElementById("HEADER_PANEL")
  9. ,document.getElementById("ITEM_PANEL")
  10. );
  11. }
  12.  
  13. /**
  14. /* @param Description : 画面のサイズに合わせて、サイズを変更するテーブル
  15. /* @param Arguments :
  16. /* @param verticalMargin : 垂直マージン
  17. /* @param horizontalMargin : 水平マージン
  18. /* @param headerPanel : テーブルヘッダーパネル(DIV)
  19. /* @param itemPanel : テーブルアイテムパネル(DIV)
  20. /* @param containerHeight : 基準となるコンテナの高さ(省略時、BODYタグのクライアント領域高さ)
  21. /* @param containerWidth : 基準となるコンテナの幅(省略時、BODYタグのクライアント領域幅)
  22. */
  23. function resizeTablePanel( verticalMargin, horizontalMargin, headerPanel, itemPanel
  24. , containerHeight /* document.body.clientHegith */
  25. , containerWidth /* document.body.clientWidth */ ) {
  26. // 規定値の設定
  27. if (containerHeight == undefined) containerHeight = document.body.clientHeight; // コンテナ高さ
  28. if (containerWidth == undefined) containerWidth = document.body.clientWidth; // コンテナ幅
  29. var SCROLL_BAR_MARGIN = 17; // スクロールバー幅 規定値
  30. var isVerticalScrollBar = false; // 垂直スクロールバーが表示されるか
  31. var scrollBarMargin = 0; // 垂直スクロールバーマージン
  32.  
  33. // テーブル幅が変わらない設定(overflow-y:auto or overflow-y:scroll)
  34. var isFixWidth = itemPanel.style.overflowY == 'scroll'
  35. || itemPanel.style.overflowY == 'auto';
  36. // コンテナの高さが、指定マージンより大きければ、テーブル明細パネルの高さを変更する
  37. if (containerHeight > verticalMargin) {
  38. itemPanel.style.height = containerHeight - verticalMargin;
  39. }
  40. // 垂直スクロールバーが表示されるかを判定する
  41. var adjust = 16; // 調整(行が半分隠れている場合等正しく判定されないため)
  42. isVerticalScrollBar = (itemPanel.style.overflow == 'scroll') /* 常時表示 */
  43. || ((totalOffsetHeight(itemPanel)+adjust) >= itemPanel.offsetHeight) /* 子要素のほうが高い */
  44.  
  45. // コンテナの幅が、指定マージンより大きければ、
  46. // テーブルヘッダーパネルおよびテーブル明細パネルの幅を変更する
  47. if (containerWidth > horizontalMargin) {
  48. itemPanel.style.width = containerWidth - horizontalMargin;
  49. // 垂直スクロールバー有無により、ヘッダーの幅を調整
  50. if (isVerticalScrollBar) {
  51. scrollBarMargin = SCROLL_BAR_MARGIN;
  52. }
  53.  
  54. // テーブル幅固定の場合、ヘッダーリサイズしない
  55. if (!isFixWidth) {
  56. var headerMargin = (horizontalMargin + scrollBarMargin);
  57. var headerWidth = containerWidth - headerMargin;
  58. if (containerWidth <= headerMargin ) {
  59. headerWidth = 0;
  60. }
  61. headerPanel.style.width = headerWidth
  62. }
  63. }
  64. return;
  65. }
  66.  
  67. /**
  68. /* @param Description : 指定されたオブジェクトの直接の子どもの高さの合計を取得
  69. /* @param Arguments :
  70. /* @param parent : 対象となる親オブジェクト
  71. */
  72. function totalOffsetHeight( parent ) {
  73. var children = parent.children;
  74. var h = 0;
  75. if (children == null) {
  76. return h;
  77. }
  78. for (var i=0; i<children.length; i++) {
  79. h += children[i].offsetHeight;
  80. }
  81. return h;
  82. }

テンプレート:Ref scrl tbl.jpg テンプレート:Ref scroll table sample.lzh テンプレート:Attach


{{include_html banner_html, "!Javascript"}}