From 4f49b16079b27d7ce633619e4cddfdf2bffbc952 Mon Sep 17 00:00:00 2001 From: cashmere Date: Fri, 4 Sep 2026 09:05:24 +0200 Subject: initial commit --- src/sorttable.js | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/sorttable.js (limited to 'src/sorttable.js') diff --git a/src/sorttable.js b/src/sorttable.js new file mode 100644 index 0000000..791910c --- /dev/null +++ b/src/sorttable.js @@ -0,0 +1,172 @@ +/* Javascript code that will enables sorting of the table. This code is +** derived from +** +** http://www.webtoolkit.info/sortable-html-table.html +** +** but with extensive modifications. +** +** All tables with class "sortable" are registered with the SortableTable() +** function. Example: +** +** +** +** Column data types are determined by the data-column-types attribute of +** the table. The value of data-column-types is a string where each +** character of the string represents a datatype for one column in the +** table. +** +** t Sort by text +** n Sort numerically +** k Sort by the data-sortkey property +** x This column is not sortable +** +** Capital letters mean sort in reverse order. +** If there are fewer characters in zColumnTypes[] than their are columns, +** then all extra columns assume type "t" (text). +** +** If a column of the table is initially sorted, then the data-init-sort +** attribute should be set to the 1-based index of that column. (The +** left-most column is 1, the next column to the right is 2, and so forth.) +** A value of 0 in the data-init-sort attribute indicates that no columns +** where initially sorted. +** +** Clicking on the same column header twice in a row inverts the sort. +*/ +function SortableTable(tableEl){ + var columnTypes = tableEl.getAttribute("data-column-types"); + var initSort = tableEl.getAttribute("data-init-sort"); + this.tbody = tableEl.getElementsByTagName('tbody'); + this.columnTypes = columnTypes; + if(tableEl.rows.length==0) return; + var ncols = tableEl.rows[0].cells.length; + for(var i = columnTypes.length; i<=ncols; i++){this.columnTypes += 't';} + this.sort = function (cell) { + var column = cell.cellIndex; + var sortFn; + switch( cell.sortType ){ + case "n": sortFn = this.sortNumeric; break; + case "N": sortFn = this.sortReverseNumeric; break; + case "t": sortFn = this.sortText; break; + case "T": sortFn = this.sortReverseText; break; + case "k": sortFn = this.sortKey; break; + case "K": sortFn = this.sortReverseKey; break; + default: return; + } + this.sortIndex = column; + var newRows = new Array(); + for (j = 0; j < this.tbody[0].rows.length; j++) { + newRows[j] = this.tbody[0].rows[j]; + } + if( this.sortIndex==Math.abs(this.prevColumn)-1 ){ + newRows.reverse(); + this.prevColumn = -this.prevColumn; + }else{ + newRows.sort(sortFn); + this.prevColumn = this.sortIndex+1; + } + for (i=0;i0)){ + return; + } + if(x && x[0].rows && x[0].rows.length > 0) { + this.hdrRow = x[0].rows[0]; + } else { + return; + } + var thisObject = this; + this.prevColumn = initSort; + for (var i=0; i