-
Notifications
You must be signed in to change notification settings - Fork 33
/
sorTable.min.js
1 lines (1 loc) · 1.48 KB
/
sorTable.min.js
1
var sorTablejs=function(a){"use strict";function c(a,b,c){Object.keys(a).forEach(function(d){a[d].addEventListener(b,c,!1)})}function d(a){var b=function(a){var c=a.parentNode;return"TABLE"===c.tagName.toUpperCase()?c:b(c)};return b(a)}function e(a){for(var b=[],c=1,d=a.length;c<d;c++)for(var e=0,f=a[c].cells.length;e<f;e++)"undefined"==typeof b[c]&&(b[c]={},b[c].key=c),b[c][e]=a[c].cells[e].innerText;return b}function f(a,b,c){return a.sort(function(a,d){return a[b]<d[b]?-1*c:a[b]>d[b]?c:0}),a}function g(a,b){var c="";b.forEach(function(b){c+=a.querySelectorAll("tr")[b.key].outerHTML}),a.querySelector("tbody").innerHTML=c}function h(a,c){var d=a.querySelectorAll(b.selectorHeaders);Object.keys(d).forEach(function(a){d[a].classList.remove(b.cssDesc),d[a].classList.remove(b.cssAsc)})}function i(a,c){1===c?a.classList.add(b.cssAsc):a.classList.add(b.cssDesc)}function j(a){var c=d(a);if(c){var j=e(c.querySelectorAll("tr")),k=a.classList.contains(b.cssAsc)?-1:1;j=f(j,a.cellIndex,k),g(c,j),h(c,j),i(a,k)}}var b={targetTable:"table.sortable",cssAsc:"order-asc",cssDesc:"order-desc",cssBg:"sortable",selectorHeaders:"thead th"};return a instanceof String||"string"==typeof a?b.targetTable=a:"object"==typeof a&&Object.keys(a).forEach(function(c){b[c]=a[c]}),window.addEventListener("load",function(){var a=document.querySelector(b.targetTable).querySelectorAll(b.selectorHeaders);document.querySelector(b.targetTable).classList.add(b.cssBg),c(a,"click",function(a){j(a.target)})},!1),this};sorTablejs();