Skip to content

Commit

Permalink
Fixed file-size-sort sorting cells rather than rows.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeWannacott committed May 21, 2023
1 parent 8701c00 commit 783c27a
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Instructions:
Click on the table headers to sort them.
*/


function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
function getHTMLTables() {
if (testingTableSortJS === true) {
Expand Down Expand Up @@ -394,34 +395,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}

function updateFilesize(i, tr, column, columnIndex) {
console.log("a", tr.outerHTML);
tr.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
// We do this to sort rows rather than cells:
const template = document.createElement('template')
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
tr = template.content.firstChild
let fileSizeInBytesHTML = column.getColumn(
tr,
column.spanSum,
column.span
).outerHTML;
console.log("bi", fileSizeInBytesHTML);
const fileSizeInBytesText = column.getColumn(
tr,
column.spanSum,
column.span
).textContent;

const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
console.log(fileSize, "file");
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
let replaced = false;
for (let i = 0; i < prefixes.length; ++i) {
let nextPrefixMultiplier = 2 ** (10 * (i + 1));
if (fileSize < nextPrefixMultiplier) {
console.log("[", fileSize, prefixes[i]);
let prefixMultiplier = 2 ** (10 * i);
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(fileSize / prefixMultiplier).toFixed(2)} ${prefixes[i]}B`
);
console.log(fileSizeInBytesHTML);
replaced = true;
break;
}
Expand All @@ -432,21 +431,16 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
"NaN"
);
}
tr.querySelectorAll("td").item(columnIndex).outerHTML = fileSizeInBytesHTML;
console.log("by", fileSizeInBytesHTML);
console.log(2, tr.querySelectorAll("td").item(columnIndex).outerHTML);
tr.querySelectorAll("td").item(columnIndex).innerHTML = fileSizeInBytesHTML;
return tr.outerHTML;

// return tr.outerHTML;
}

function updateTable(tableProperties) {
const { tableRows, column, columnIndex, hasThClass } = tableProperties;
for (let [i, tr] of tableRows.entries()) {
if (hasThClass.fileSize) {
console.log(tr.outerHTML);
tr.outerHTML = updateFilesize(i, tr, column, columnIndex);
console.log(9, tr.outerHTML);
// console.log(9, tr.outerHTML);
} else if (!hasThClass.fileSize) {
tr.outerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
}
Expand Down

0 comments on commit 783c27a

Please sign in to comment.