Advertisement
rg443

html-table-sort.js

Dec 12th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://github.com/tristen/tablesort
  2. (function() {
  3.   function Tablesort(el, options) {
  4.     if (!(this instanceof Tablesort)) {
  5.       return new Tablesort(el, options);
  6.     }
  7.     if (!el || el.tagName !== "TABLE") {
  8.       throw new Error("Element must be a table");
  9.     }
  10.     this.init(el, options || {});
  11.   }
  12.   var sortOptions = [];
  13.   var createEvent = function(name) {
  14.     var evt;
  15.     if (!window.CustomEvent || typeof window.CustomEvent !== "function") {
  16.       evt = document.createEvent("CustomEvent");
  17.       evt.initCustomEvent(name, false, false, undefined);
  18.     } else {
  19.       evt = new CustomEvent(name);
  20.     }
  21.     return evt;
  22.   };
  23.   var getInnerText = function(el) {
  24.     return el.getAttribute("data-sort") || el.textContent || el.innerText || "";
  25.   };
  26.   var caseInsensitiveSort = function(a, b) {
  27.     a = a.toLowerCase();
  28.     b = b.toLowerCase();
  29.     if (a === b) {
  30.       return 0;
  31.     }
  32.     if (a < b) {
  33.       return 1;
  34.     }
  35.     return -1;
  36.   };
  37.   var stabilize = function(sort, antiStabilize) {
  38.     return function(a, b) {
  39.       var unstableResult = sort(a.td, b.td);
  40.       if (unstableResult === 0) {
  41.         if (antiStabilize) {
  42.           return b.index - a.index;
  43.         }
  44.         return a.index - b.index;
  45.       }
  46.       return unstableResult;
  47.     };
  48.   };
  49.   Tablesort.extend = function(name, pattern, sort) {
  50.     if (typeof pattern !== "function" || typeof sort !== "function") {
  51.       throw new Error("Pattern and sort must be a function");
  52.     }
  53.     sortOptions.push({name:name, pattern:pattern, sort:sort});
  54.   };
  55.   Tablesort.prototype = {init:function(el, options) {
  56.     var that = this, firstRow, defaultSort, i, cell;
  57.     that.table = el;
  58.     that.thead = false;
  59.     that.options = options;
  60.     if (el.rows && el.rows.length > 0) {
  61.       if (el.tHead && el.tHead.rows.length > 0) {
  62.         firstRow = el.tHead.rows[el.tHead.rows.length - 1];
  63.         that.thead = true;
  64.       } else {
  65.         firstRow = el.rows[0];
  66.       }
  67.     }
  68.     if (!firstRow) {
  69.       return;
  70.     }
  71.     var onClick = function() {
  72.       if (that.current && that.current !== this) {
  73.         that.current.classList.remove("sort-up");
  74.         that.current.classList.remove("sort-down");
  75.       }
  76.       that.current = this;
  77.       that.sortTable(this);
  78.     };
  79.     for (i = 0;i < firstRow.cells.length;i++) {
  80.       cell = firstRow.cells[i];
  81.       if (!cell.classList.contains("no-sort")) {
  82.         cell.classList.add("sort-header");
  83.         cell.tabindex = 0;
  84.         cell.addEventListener("click", onClick, false);
  85.         if (cell.classList.contains("sort-default")) {
  86.           defaultSort = cell;
  87.         }
  88.       }
  89.     }
  90.     if (defaultSort) {
  91.       that.current = defaultSort;
  92.       that.sortTable(defaultSort);
  93.     }
  94.   }, sortTable:function(header, update) {
  95.     var that = this, column = header.cellIndex, sortFunction = caseInsensitiveSort, item = "", items = [], i = that.thead ? 0 : 1, sortDir, sortMethod = header.getAttribute("data-sort-method"), sortOrder = header.getAttribute("data-sort-order");
  96.     that.table.dispatchEvent(createEvent("beforeSort"));
  97.     if (update) {
  98.       sortDir = header.classList.contains("sort-up") ? "sort-up" : "sort-down";
  99.     } else {
  100.       if (header.classList.contains("sort-up")) {
  101.         sortDir = "sort-down";
  102.       } else {
  103.         if (header.classList.contains("sort-down")) {
  104.           sortDir = "sort-up";
  105.         } else {
  106.           if (sortOrder === "asc") {
  107.             sortDir = "sort-down";
  108.           } else {
  109.             if (sortOrder === "desc") {
  110.               sortDir = "sort-up";
  111.             } else {
  112.               sortDir = that.options.descending ? "sort-up" : "sort-down";
  113.             }
  114.           }
  115.         }
  116.       }
  117.       header.classList.remove(sortDir === "sort-down" ? "sort-up" : "sort-down");
  118.       header.classList.add(sortDir);
  119.     }
  120.     if (that.table.rows.length < 2) {
  121.       return;
  122.     }
  123.     if (!sortMethod) {
  124.       while (items.length < 3 && i < that.table.tBodies[0].rows.length) {
  125.         item = getInnerText(that.table.tBodies[0].rows[i].cells[column]);
  126.         item = item.trim();
  127.         if (item.length > 0) {
  128.           items.push(item);
  129.         }
  130.         i++;
  131.       }
  132.       if (!items) {
  133.         return;
  134.       }
  135.     }
  136.     for (i = 0;i < sortOptions.length;i++) {
  137.       item = sortOptions[i];
  138.       if (sortMethod) {
  139.         if (item.name === sortMethod) {
  140.           sortFunction = item.sort;
  141.           break;
  142.         }
  143.       } else {
  144.         if (items.every(item.pattern)) {
  145.           sortFunction = item.sort;
  146.           break;
  147.         }
  148.       }
  149.     }
  150.     that.col = column;
  151.     var newRows = [], noSorts = {}, j, totalRows = 0, noSortsSoFar = 0;
  152.     for (i = 0;i < that.table.tBodies.length;i++) {
  153.       for (j = 0;j < that.table.tBodies[i].rows.length;j++) {
  154.         item = that.table.tBodies[i].rows[j];
  155.         if (item.classList.contains("no-sort")) {
  156.           noSorts[totalRows] = item;
  157.         } else {
  158.           newRows.push({tr:item, td:getInnerText(item.cells[that.col]), index:totalRows});
  159.         }
  160.         totalRows++;
  161.       }
  162.     }
  163.     if (sortDir === "sort-down") {
  164.       newRows.sort(stabilize(sortFunction, true));
  165.       newRows.reverse();
  166.     } else {
  167.       newRows.sort(stabilize(sortFunction, false));
  168.     }
  169.     for (i = 0;i < totalRows;i++) {
  170.       if (noSorts[i]) {
  171.         item = noSorts[i];
  172.         noSortsSoFar++;
  173.       } else {
  174.         item = newRows[i - noSortsSoFar].tr;
  175.       }
  176.       that.table.tBodies[0].appendChild(item);
  177.     }
  178.     that.table.dispatchEvent(createEvent("afterSort"));
  179.   }, refresh:function() {
  180.     if (this.current !== undefined) {
  181.       this.sortTable(this.current, true);
  182.     }
  183.   }};
  184.   if (typeof module !== "undefined" && module.exports) {
  185.     module.exports = Tablesort;
  186.   } else {
  187.     window.Tablesort = Tablesort;
  188.   }
  189. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement