Skip to content
This repository has been archived by the owner on May 8, 2022. It is now read-only.

Commit

Permalink
Rework table sorting. Fixes: numeric entries are sorted as strings. D…
Browse files Browse the repository at this point in the history
…uplicated numeric entries are lost on sorting. Closes #28
  • Loading branch information
SCP002 committed Apr 16, 2022
1 parent 98a6468 commit ee54eb8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 94 deletions.
72 changes: 29 additions & 43 deletions html/js/base_ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,24 @@ function bulkEdit() {
return;
}
function sortTable(column) {
//console.log(columm);
if (column == COLUMN_TO_SORT) {
return;
}
var table = document.getElementById("content_table");
var tableHead = table.getElementsByTagName("TR")[0];
var tableItems = tableHead.getElementsByTagName("TD");
var sortObj = new Object();
var x, xValue;
var tableHeader;
var sortByString = false;
if (column > 0 && COLUMN_TO_SORT > 0) {
const table = document.getElementById("content_table");
const tableHead = table.getElementsByTagName("TR")[0];
const tableItems = tableHead.getElementsByTagName("TD");
const sortArr = [];
let xValue;
if (column >= 0 && COLUMN_TO_SORT >= 0) {
tableItems[COLUMN_TO_SORT].className = "pointer";
tableItems[column].className = "sortThis";
}
COLUMN_TO_SORT = column;
var rows = table.rows;
const rows = table.rows;
if (rows[1] != undefined) {
tableHeader = rows[0];
x = rows[1].getElementsByTagName("TD")[column];
for (i = 1; i < rows.length; i++) {
const tableHeader = rows[0];
let x = rows[1].getElementsByTagName("TD")[column];
for (let i = 1; i < rows.length; i++) {
x = rows[i].getElementsByTagName("TD")[column];
switch (x.childNodes[0].tagName.toLowerCase()) {
case "input":
Expand All @@ -185,43 +182,32 @@ function sortTable(column) {
case "p":
xValue = x.getElementsByTagName("P")[0].innerText.toLowerCase();
break;
default: console.log(x.childNodes[0].tagName);
}
if (xValue == "" || xValue == NaN) {
xValue = i;
sortObj[i] = rows[i];
}
else {
switch (isNaN(xValue) || typeof xValue == "string") {
case false:
xValue = parseFloat(xValue);
sortObj[xValue] = rows[i];
break;
case true:
sortByString = true;
sortObj[xValue.toLowerCase() + i] = rows[i];
break;
}
default:
console.log(x.childNodes[0].tagName);
}
sortArr.push({ key: xValue ? xValue : i, row: rows[i] });
}
while (table.firstChild) {
table.removeChild(table.firstChild);
}
var sortValues = getObjKeys(sortObj);
if (sortByString == true) {
sortValues.sort();
console.log(sortValues);
}
else {
function sortFloat(a, b) {
return a - b;
sortArr.sort((se1, se2) => {
const se1KeyNum = parseFloat(String(se1.key));
const se2KeyNum = parseFloat(String(se2.key));
if (!isNaN(se1KeyNum) && !isNaN(se2KeyNum)) {
return se1KeyNum - se2KeyNum;
}
sortValues.sort(sortFloat);
}
if (se1.key < se2.key) {
return -1;
}
if (se1.key > se2.key) {
return 1;
}
return 0;
});
table.appendChild(tableHeader);
for (var i = 0; i < sortValues.length; i++) {
table.appendChild(sortObj[sortValues[i]]);
}
sortArr.forEach((se) => {
table.appendChild(se.row);
});
}
return;
}
Expand Down
86 changes: 35 additions & 51 deletions ts/base_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,38 +204,38 @@ function bulkEdit() {
}

function sortTable(column) {
//console.log(columm);

if (column == COLUMN_TO_SORT) {
return;
}

const table = document.getElementById("content_table");
const tableHead = table.getElementsByTagName("TR")[0];
const tableItems = tableHead.getElementsByTagName("TD");

var table = document.getElementById("content_table");
var tableHead = table.getElementsByTagName("TR")[0];
var tableItems = tableHead.getElementsByTagName("TD");
type SortEntry = {
key: string | number;
row: HTMLTableRowElement;
}

var sortObj = new Object();
var x, xValue;
var tableHeader
var sortByString = false
const sortArr: SortEntry[] = [];
let xValue: string | number;

if (column > 0 && COLUMN_TO_SORT > 0) {
if (column >= 0 && COLUMN_TO_SORT >= 0) {
tableItems[COLUMN_TO_SORT].className = "pointer";
tableItems[column].className = "sortThis";
}

COLUMN_TO_SORT = column;


var rows = (table as HTMLTableElement).rows;
const rows = (table as HTMLTableElement).rows;

if (rows[1] != undefined) {
tableHeader = rows[0]
const tableHeader = rows[0];

x = rows[1].getElementsByTagName("TD")[column];
let x: any = rows[1].getElementsByTagName("TD")[column];

for (i = 1; i < rows.length; i++) {
for (let i = 1; i < rows.length; i++) {

x = rows[i].getElementsByTagName("TD")[column];

Expand All @@ -248,58 +248,42 @@ function sortTable(column) {
xValue = x.getElementsByTagName("P")[0].innerText.toLowerCase();
break;

default: console.log(x.childNodes[0].tagName);
default:
console.log(x.childNodes[0].tagName);
}

if (xValue == "" || xValue == NaN) {

xValue = i
sortObj[i] = rows[i];

} else {

switch(isNaN(xValue) || typeof xValue == "string") {
case false:

xValue = parseFloat(xValue);
sortObj[xValue] = rows[i]
break;

case true:

sortByString = true
sortObj[xValue.toLowerCase() + i] = rows[i]
break;

}

}
sortArr.push({key: xValue ? xValue : i, row: rows[i]});

}

while (table.firstChild) {
table.removeChild(table.firstChild);
}

var sortValues = getObjKeys(sortObj)
sortArr.sort((se1: SortEntry, se2: SortEntry): number => {
const se1KeyNum = parseFloat(String(se1.key));
const se2KeyNum = parseFloat(String(se2.key));

if (sortByString == true) {
sortValues.sort()
console.log(sortValues);
} else {
function sortFloat(a, b) {
return a - b;
if (!isNaN(se1KeyNum) && !isNaN(se2KeyNum)) {
return se1KeyNum - se2KeyNum;
}
sortValues.sort(sortFloat);
}

table.appendChild(tableHeader)
if (se1.key < se2.key) {
return -1;
}

for (var i = 0; i < sortValues.length; i++) {
if (se1.key > se2.key) {
return 1;
}

table.appendChild(sortObj[sortValues[i]])
return 0;
});

}
table.appendChild(tableHeader);

sortArr.forEach((se: SortEntry) => {
table.appendChild(se.row);
});

}

Expand Down

0 comments on commit ee54eb8

Please sign in to comment.