萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

左側固定表頭 JS 表格元件:支援多行表頭、多欄位、固定列、自適應寬度、資料獲取

元件特色

  • 左側固定多行表頭 (Row Headers),右側資料區可水平捲動
  • 支援多層表頭巢狀結構 (rowname 陣列)
  • 表頭寬度固定,資料欄寬度可配置單位寬度 (perUnitWidth)
  • 提侯 addData() 動態更新資料、getData() 取得陣列資料、getHead() 取得表頭陣列
  • 相容 IE / Chrome (原版聲稱),現代瀏覽器建議配合 Polyfill

使用範例

<div id="testdiv" align="center"></div>

<script>
var grid = new LeftHeadGrid({
    id: 'leftHeadGrid',
    width: 800,
    title: '資料表格標題',
    perUnitWidth: 300, // 每單位寬度像素
    rowHeads: [{ // 左側表頭定義
        width: '60',
        rowname: [
            {name: '日期'},
            {name: '數值'}
        ]
    }],
    columnDatas: [{ // 右側資料欄定義
        width: [1, 1], // 相對單位,實際寬度 = 單位 * perUnitWidth
        rows: [
            [10130501, 101011],
            [2, 3]
        ]
    }]
});
grid.RenderTo('testdiv');
</script>

核心類別結構 (LeftHeadGrid.js 重點節錄)

var LeftHeadGrid = function(config) {
    this.rowHeads = config.rowHeads || [];
    this.columnDatas = config.columnDatas || [];
    this.width = config.width || '100%';
    this.id = config.id || 'TreGrid';
    this.perUnitWidth = config.perUnitWidth || 10;
    this.title = config.title || '';
    this.noDataStr = config.noDataStr || 'no data';
    this.innerWidth = 0; this.innerLWidth = 0; this.height = 0;
};

LeftHeadGrid.prototype.createGrid = function() {
    // 1. 產生左側固定表頭 Table HTML
    // 遍歷 rowHeads → rowname,產生 ...
    // 累加表頭總寬度 this.innerLWidth
    // 計算資料區寬度 this.innerWidth = this.width - this.innerLWidth - 5
};

LeftHeadGrid.prototype.RenderTo = function(divId) {
    // 組合外框 HTML:標題列 + 左側表頭區 + 右側資料區 (含捲動容器)
    // 寫入目標容器 innerHTML
    this.show(); // 渲染資料
};

LeftHeadGrid.prototype.show = function() {
    // 根據 columnDatas[0] 產生右側資料 Table
    // 計算各欄實際寬度 (width[i] * perUnitWidth)
    // 若總寬度 < 容器寬度,壓縮容器寬度避免多餘空白
    // 注入 #leftgriddataview innerHTML
};

LeftHeadGrid.prototype.addData = function(newData) {
    this.columnDatas = newData;
    this.show();
};

LeftHeadGrid.prototype.getData = function() {
    // 回傳陣列:每列為逗號分隔字串
};

LeftHeadGrid.prototype.getHead = function() {
    // 回傳表頭陣列
};

CSS 樣式需求 (LeftHeadGrid.css)

.outframecss { position: relative; border: 1px solid #ccc; font-family: 微軟正黑體; }
.grittitle { background: #f0f0f0; border-bottom: 1px solid #ccc; line-height: 30px; padding-left: 10px; }
.leftheadcss { position: absolute; left: 0; top: 30px; bottom: 0; overflow: hidden; background: #fff; z-index: 10; border-right: 1px solid #ccc; }
.leftheadcss table { border-collapse: collapse; width: 100%; }
.bluefirst, .bluelast { border: 1px solid #ccc; padding: 5px; background: #e8f0fe; color: #333; }
.bluelast { border-bottom: 2px solid #0066cc; } /* 最後一列加粗底線 */
.dataframecss { position: absolute; left: [表頭寬度]px; top: 30px; right: 0; bottom: 0; overflow: auto; }
.innerdivcss { min-height: 100%; }

現代化重構建議

  • 改用 class LeftHeadGrid ES6 語法,模組化導出。
  • 使用 position: sticky 實現左側固定 (CSS 原生),僅需 .leftheadcss { position: sticky; left: 0; },無需計算 absolute 位置。
  • 資料渲染改用 DocumentFragment 或虛擬列表 (大量資料時)。
  • TypeScript 定義介面:RowHeadConfig, ColumnDataConfig, GridOptions。
  • 整合 AG Grid、Handsontable、TanStack Table 等成熟表格庫,功能完備 (排序、篩選、編輯、匯出、虛擬滾動)。

總結

LeftHeadGrid 以「左側固定表頭 + 右側捲動資料區」解決寬表格瀏覽痛點。核心在於兩個獨立容器 absolute 定位同步滾動 (或現代 sticky)、動態計算欄寬、資料與表頭分離渲染。適合報表、試算表類介面。

copyright © 萬盛學電腦網 all rights reserved