Skip to content

Instantly share code, notes, and snippets.

@asafh
Last active September 24, 2018 16:04
Show Gist options
  • Save asafh/9ac39461b314c01029a356ced3f90b66 to your computer and use it in GitHub Desktop.
Save asafh/9ac39461b314c01029a356ced3f90b66 to your computer and use it in GitHub Desktop.
Viewtrade.com sort Portfolio table.
//Paste in console or create a javascript: bookmark
//Dual number cells are sorted by the first number (e.g. absolute value rather than percentage).
//Every refresh of the table disables the sort functionility and needs to be reapplied.
//To sort by a column click its header.
(() => {
const tbl = document.querySelector("#portfolioDiv table.datatable");
const header = tbl.querySelector("#portfolioHeader");
const totals = tbl.querySelector("tr.totalsRow");
const dataRows = Array.from(tbl.querySelectorAll("tr.data"));
Array.from(header.querySelectorAll("td")).reduce((sortByIndex,td) => {
td.onclick = () => {
dataRows.sort((tra,trb) => {
const sel = `td:nth-child(${sortByIndex+1})`;
const [vala,valb] = [tra,trb].map(tr => tr.querySelector(sel)).map(td => td.classList.contains("number") ? Number.parseFloat(td.innerText.replace(",","")) : td.innerText);
return vala-valb;
});
dataRows.forEach(tr => tr.parentNode.appendChild(tr));
totals.parentNode.appendChild(totals);
};
return sortByIndex+(Number(td.colSpan)||1);
}, 0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment