logo

blog

My website can't be that messy, right? git clone https://hacktivis.me/git/blog.git
commit: a25e6f0b8ca0f558048ad31efebc0a50f16e1f42
parent 060661ac604bb3e423c0bbb876c56f2e41e586e7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu,  9 Mar 2023 05:24:37 +0100

javascript/sorttable.js: Add data-type support

Diffstat:

Mjavascript/sorttable.js19+++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/javascript/sorttable.js b/javascript/sorttable.js @@ -97,7 +97,8 @@ sorttable.innerSortFunction = function(tr) { }; // - Use data-value="" attribute if you want to override innerHTML -// Example: <td data-value="1337">Leet</td> +// - Use data-type="" attribute if you want to use non-lexical sorting +// Example: <td data-value="1337" data-type="int">Leet</td> sorttable.getElementValue = function(element) { if(element === undefined) { @@ -105,7 +106,21 @@ sorttable.getElementValue = function(element) { console.log("Warning: Empty cells found"); }; - return element.dataset.value ? element.dataset.value : element.innerText; + let value = element.dataset.value ? element.dataset.value : element.innerText; + switch(element.dataset.type) + { + case undefined: + return value; + case "int": + var v = parseInt(value); + return v != NaN ? v : value; + case "float": + var v = parseFloat(value); + return v != NaN ? v : value; + default: + console.log(`Warning: Unhandled type ${element.dataset.type}`); + return value; + }; }; sorttable.reverse = function(tbody) {