Created
December 22, 2017 06:33
-
-
Save CMarker/46c65fbe4c00bf5cfc1ee312b5802c4b to your computer and use it in GitHub Desktop.
Bookmarklet to allow sorting a table by column header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function()%7Bfunction getElIndex(e)%7Bfor(var t%3D0%3Be%3De.previousElementSibling%3Bt%2B%2B)%3Breturn t%7Dfunction getNearestTableAncestor(e)%7Bfor(%3Be%3B)if("table"%3D%3D%3D(e%3De.parentNode).tagName.toLowerCase())return e%7Dfunction tableIsEven(e)%7Bif(!e.tHead)return!1%3Breturn e.tHead.getElementsByTagName("th").length%3D%3De.tBodies%5B0%5D.getElementsByTagName("tr")%5B0%5D.getElementsByTagName("td").length%7Dfunction sort_table(e,t,n)%7Bvar r,a,o,l,i%3De.rows,s%3Di.length,c%3D%5B%5D%3B%0A// fill the array with values from the table%0Afor(r%3D0%3Bs>r%3Br%2B%2B)for(l%3D(o%3Di%5Br%5D.cells).length,c%5Br%5D%3D%5B%5D,a%3D0%3Bl>a%3Ba%2B%2B)c%5Br%5D%5Ba%5D%3Do%5Ba%5D.innerHTML%3Bfor(%0A// sort the array by the specified column number (col) and order (asc)%0Ac.sort(function(e,r)%7Bvar a%3De%5Bt%5D.replace(/<%5B%5E>%5D*>/g,"").trim(),o%3Dr%5Bt%5D.replace(/<%5B%5E>%5D*>/g,"").trim()%3Breturn isNaN(parseInt(a))%7C%7CisNaN(parseInt(o))%3Fa%3D%3Do%3F0:a>o%3Fn:-1*n:parseInt(a)%3D%3DparseInt(o)%3F0:parseInt(a)>parseInt(o)%3Fn:-1*n%7D),r%3D0%3Bs>r%3Br%2B%2B)c%5Br%5D%3D"<td>"%2Bc%5Br%5D.join("</td><td>")%2B"</td>"%3Be.innerHTML%3D"<tr>"%2Bc.join("</tr><tr>")%2B"</tr>"%7Dfunction addDownloadLink(e)%7Bvar t%3Ddocument.createElement("a"),n%3Ddocument.createTextNode("Export")%3Bt.appendChild(n),t.title%3D"Export",t.href%3D"%23",t.id%3D"export",t.style.float%3D"right",t.onclick%3Dfunction(t)%7BdownloadCSV(e),t.preventDefault()%7D,e.parentElement.insertBefore(t,e)%7Dfunction downloadCSV(e)%7Bvar t%3Dprompt("Enter a filename:","filename.csv")%3Bif(t%26%26t.length)%7B-1%3D%3Dt.indexOf(".csv")%26%26(t%2B%3D".csv")%3Bvar n%3Dtable2csv(e),r%3D"data:text/csv%3Bcharset%3DUTF-8,"%2BencodeURIComponent(n),a%3Ddocument.createElement("a"),o%3Ddocument.createTextNode("Export")%3Ba.appendChild(o),a.title%3D"Export",a.href%3Dr,a.download%3Dt,a.click()%7D%7Dfunction table2csv(e)%7Bfunction t(e)%7Bvar t%3De.join("")%3B// to remove any blank rows%0A// alert(tmp)%3B%0Aif(e.length>0%26%26""!%3Dt)%7Bvar n%3De.join(",")%3Br%5Br.length%5D%3Dn%7D%7Dfunction n(e)%7B%0A// replace " with %25u201C%0Avar t%3DRegExp(/%5B"%5D/g),n%3De.replace(t,"%25u201C")%3Bt%3DRegExp(/%5C<%5B%5E%5C<%5D%2B%5C>/g)%3Breturn""%3D%3D(n%3Dn.replace(t,""))%3F"":%27"%27%2Bn%2B%27"%27%7Dfor(var r%3D%5B%5D,a%3D%5B%5D,o%3De.getElementsByTagName("th"),l%3D0%3Bl<o.length%3Bl%2B%2B)a%5Ba.length%5D%3Dn(o%5Bl%5D.innerHTML.trim())%3Bt(a)%3Bvar i%3De.getElementsByTagName("tr")%3Bfor(l%3D0%3Bl<i.length%3Bl%2B%2B)%7Ba%3D%5B%5D%3Bfor(var s%3Di%5Bl%5D.getElementsByTagName("td"),c%3D0%3Bc<s.length%3Bc%2B%2B)%7Bvar g%3Ds%5Bc%5D%3Ba%5Ba.length%5D%3Dn(g.innerHTML.trim())%7Dt(a)%7Dreturn r.join("%5Cn")%7Dfor(var tables%3Ddocument.getElementsByTagName("table"),x%3D0%3Bx<tables.length%3Bx%2B%2B)%7Bvar table%3Dtables%5Bx%5D%3Bif(tableIsEven(table))%7Btable.style.borderColor%3D"blue",table.style.borderWidth%3D"5px"%3Bfor(var columns%3Dtable.getElementsByTagName("th"),y%3D0%3By<columns.length%3By%2B%2B)columns%5By%5D.onclick%3Dfunction()%7Bvar e%3DgetNearestTableAncestor(this),t%3DgetElIndex(this)%3Bconsole.log("sorting: "%2Bt),e.sort*%3D-1,sort_table(e.tBodies%5B0%5D,t,e.sort)%7D%3Btable.sort%3D1,addDownloadLink(table)%7D%7D%7D)()%3B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment