class TableSort {
constructor(tableId, { headerRow = 1, startRow = 2, endRow = 999, upClass, downClass, selectClass } = {}) {
this.table = document.getElementById(tableId);
this.rows = this.table.rows;
this.headers = this.rows[headerRow - 1].cells;
this.startRow = startRow - 1;
this.endRow = endRow === 999 ? this.rows.length : endRow;
this.upClass = upClass;
this.downClass = downClass;
this.selectClass = selectClass;
this.data = this._toArray();
this._bindEvents();
}
_toArray() { /* 將 tbody 內容轉二維陣列 */ }
_bindEvents() { /* 綁定 header click,切換升降序類別 */ }
_compare(a, b, colIdx) { /* 移除 HTML 標籤、千分位,數字用減法、字串用 localeCompare */ }
sort(colIdx, asc) { this.data.sort((a,b)=>asc?this._compare(a,b,colIdx):this._compare(b,a,colIdx)); this._render(colIdx); }
_render(sortedCol) { /* 將排序後陣列寫回 DOM,並更新 selectClass */ }
}
| ` ` | |
|---|---|
` 標準結構。
| `,鍵盤可操作。 | HTML5 教學排行推薦文章
copyright © 萬盛學電腦網 all rights reserved
|