Advertisement
killrawr

Sorttable Multiple Rows

Jun 27th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   SortTable
  3.   version 2
  4.   7th April 2007
  5.   Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
  6.  
  7.   Instructions:
  8.   Download this file
  9.   Add <script src="sorttable.js"></script> to your HTML
  10.   Add class="sortable" to any table you'd like to make sortable
  11.   Click on the headers to sort
  12.  
  13.   Thanks to many, many people for contributions and suggestions.
  14.   Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
  15.   This basically means: do what you want with it.
  16. */
  17. var stIsIE = /*@cc_on!@*/ false;
  18.  
  19. sorttable = {
  20.  
  21.     init: function() {
  22.         // quit if this function has already been called
  23.         if (arguments.callee.done) return;
  24.         // flag this function so we don't do the same thing twice
  25.         arguments.callee.done = true;
  26.         // kill the timer
  27.         if (_timer) clearInterval(_timer);
  28.  
  29.         if (!document.createElement || !document.getElementsByTagName) return;
  30.  
  31.         sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
  32.  
  33.         forEach(document.getElementsByTagName('table'), function(table) {
  34.             if (table.className.search(/\bsortable\b/) != -1) {
  35.                 sorttable.makeSortable(table);
  36.             }
  37.         });
  38.  
  39.     },
  40.  
  41.     makeSortable: function(table) {
  42.  
  43.         if (typeof(table) === 'undefined') {
  44.             return false;
  45.         }
  46.  
  47.         var jTable, jTableRows, jTableChild, jDocument = false;
  48.  
  49.         jTable = jQuery(table).not(".sortable_nosort");
  50.         jDocument = jQuery(document);
  51.  
  52.         // Do not provide sorttable support, for this table
  53.         if (!jTable.length || jTable.hasClass('sortable_nosort')) {
  54.             return false;
  55.         };
  56.  
  57.         if (jTable.hasClass('stacktable-result')) {
  58.             return false;
  59.         }
  60.  
  61.         // Update the CHILD TABLE with "sortable_nosort"
  62.         if (jTableChild = jTable.find('table.sortable_nosort')) {
  63.             jTableChild.find('tr').addClass('sorttable_row_nosort');
  64.             jTableChild.parents("td").addClass('sorttable_cell_nosort');
  65.             // jTableChild.parents("td").parent('tr').addClass('sorttable_row_nosort');
  66.         };
  67.  
  68.         jTableRows = jTable.find('tr:not(.sorttable_row_nosort)');
  69.  
  70.         if (!jTable.find('thead').length) {
  71.             var the = false;
  72.             // table doesn't have a tHead. Since it should have, create one and
  73.             // put the first table row in it.
  74.             the = jQuery("<thead></thead>");
  75.             the.append(jTableRows);
  76.             jTable.insertBefore(the, jTable.children().first());
  77.         };
  78.  
  79.         // Update the Table VARIABLE
  80.         table = jTable.get(0);
  81.  
  82.         // Safari doesn't support table.tHead, sigh
  83.         if (table.tHead == null) table.tHead = jTable.find('thead')[0];
  84.  
  85.         // can't cope with two header rows
  86.         if (table.tHead.rows.length != 1) return false;
  87.  
  88.         // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
  89.         // "total" rows, for example). This is B&R, since what you're supposed
  90.         // to do is put them in a tfoot. So, if there are sortbottom rows,
  91.         // for backwards compatibility, move them to tfoot (creating it if needed).
  92.         sortbottomrows = [];
  93.         var table_rows = jQuery.makeArray(jTableRows);
  94.  
  95.         for (var i = 0; i < table_rows.length; i++) {
  96.  
  97.             var table_row_item = jQuery(table_rows[i]);
  98.  
  99.             if (table_row_item.prop('class').search(/\bsortbottom\b/) != -1) {
  100.                 sortbottomrows[sortbottomrows.length] = table_row_item;
  101.             };
  102.  
  103.         };
  104.  
  105.         if (sortbottomrows) {
  106.             if (table.tFoot == null) {
  107.                 var tfo = jQuery("<tfoot></tfoot>");
  108.                 // table doesn't have a tfoot. Create one.
  109.                 jTable.append(tfo);
  110.             };
  111.             for (var i = 0; i < sortbottomrows.length; i++) {
  112.                 tfo.append(sortbottomrows[i]);
  113.             };
  114.             delete sortbottomrows;
  115.         };
  116.  
  117.         // work through each column and calculate its type
  118.         var headrow = table.tHead.rows[0].cells;
  119.         var headrowchanges = [];
  120.  
  121.         for (var i = 0; i < headrow.length; i++) {
  122.  
  123.             var headrow_item = headrow[i];
  124.             var j_headrow_item = jQuery(headrow_item);
  125.             var j_headrow_item_className = j_headrow_item.prop('class');
  126.  
  127.             // manually override the type with a sorttable_type attribute
  128.             // skip this col
  129.             if (!j_headrow_item_className.match(/\bsorttable_nosort\b/)) {
  130.  
  131.                 mtch = j_headrow_item_className.match(/\bsorttable_([a-z0-9]+)\b/);
  132.  
  133.                 if (mtch) {
  134.                     override = mtch[1];
  135.                 };
  136.  
  137.                 if (mtch && typeof sorttable["sort_" + override] == 'function') {
  138.                     headrow_item.sorttable_sortfunction = sorttable["sort_" + override];
  139.                 } else {
  140.                     headrow_item.sorttable_sortfunction = sorttable.guessType(table, i);
  141.                 };
  142.  
  143.                 // make it clickable to sort
  144.                 headrow_item.sorttable_columnindex = i;
  145.                 headrow_item.sorttable_tbody = table.tBodies[0];
  146.  
  147.                 // This ROW has not seen any changes
  148.                 headrowchanges[i] = false;
  149.  
  150.                 // Grab TBODY element
  151.                 var headrow_tbody = headrow_item.sorttable_tbody;
  152.  
  153.                 // Locate TH Element, and Relative TR, TD elements.
  154.                 var Table_Header_Cell = jQuery(this);
  155.  
  156.                 // If we have TABLE BODY
  157.                 if (headrow_tbody) {
  158.  
  159.                     // Get the Table Body
  160.                     var Table_TBODY = jQuery(headrow_tbody);
  161.  
  162.                     // http://stackoverflow.com/questions/6139407/getting-td-by-index-with-jquery
  163.                     var Table_Rows_Cells = Table_TBODY.find("tr td:eq(" + i + ")");
  164.                     var Table_Rows_Cells_Inputs = Table_Rows_Cells.find("input:visible, select:visible");
  165.  
  166.                     // Detect changes made in these cells
  167.                     if (Table_Rows_Cells_Inputs && Table_Rows_Cells_Inputs.length && (!Table_Rows_Cells.attr('sorttable_customkey') || Table_Rows_Cells.hasClass('sorttable_keychanges'))) {
  168.                         Table_Rows_Cells_Inputs.change(function() {
  169.                             headrowchanges[i] = true;
  170.                         });
  171.                     };
  172.  
  173.                     delete Table_TBODY;
  174.                     delete Table_Rows_Cells;
  175.                     delete Table_Rows_Cells_Inputs;
  176.  
  177.                 };
  178.  
  179.                 dean_addEvent(headrow_item, "click", function(e) {
  180.  
  181.                     var sortrevind, sortfwdind = false;
  182.  
  183.                     var j_current_headrow = jQuery(this);
  184.                     var headrow_changed = (typeof(headrowchanges[i]) !== 'undefined') ? headrowchanges[i] : false;
  185.  
  186.                     // Regular Sorting with Existing Data
  187.                     var bsorttable_sorted = (this.className.search(/\bsorttable_sorted\b/) != -1);
  188.                     var bsorttable_sorted_match = (bsorttable_sorted && !headrow_changed);
  189.  
  190.                     if (bsorttable_sorted) {
  191.                         this.className = this.className.replace('sorttable_sorted', 'sorttable_sorted_reverse');
  192.                         jDocument.find("#sorttable_sortfwdind").remove();
  193.                         sortrevind = jQuery("<span></span>")[0];
  194.                         sortrevind.id = "sorttable_sortrevind";
  195.                         sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
  196.                         this.appendChild(sortrevind);
  197.                         if (bsorttable_sorted_match) {
  198.                             // if we're already sorted by this column, just
  199.                             // reverse the table, which is quicker
  200.                             // console.log("REVERSE SORT bsorttable_sorted");
  201.                             sorttable.reverse(this.sorttable_tbody);
  202.                             return true;
  203.                         };
  204.                     };
  205.  
  206.                     delete bsorttable_sorted_match;
  207.  
  208.                     // Reverse Sorting with Existing Data
  209.                     var bsorttable_sorted_reverse = (this.className.search(/\bsorttable_sorted_reverse\b/) != -1);
  210.                     var bsorttable_sorted_reverse_match = (bsorttable_sorted_reverse && !headrow_changed);
  211.  
  212.                     if (bsorttable_sorted_reverse) {
  213.                         this.className = this.className.replace('sorttable_sorted_reverse', 'sorttable_sorted');
  214.                         jDocument.find('#sorttable_sortrevind').remove();
  215.                         sortfwdind = jQuery("<span></span>")[0];
  216.                         sortfwdind.id = "sorttable_sortfwdind";
  217.                         sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  218.                         this.appendChild(sortfwdind);
  219.                         if (bsorttable_sorted_reverse_match) {
  220.                             // if we're already sorted by this column in reverse, just
  221.                             // re-reverse the table, which is quicker
  222.                             // console.log("REVERSE SORT bsorttable_sorted_reverse");
  223.                             sorttable.reverse(this.sorttable_tbody);
  224.                             return true;
  225.                         };
  226.                     };
  227.  
  228.                     delete bsorttable_sorted_reverse_match;
  229.  
  230.                     // remove sorttable_sorted classes
  231.                     theadrow = this.parentNode;
  232.                     forEach(theadrow.childNodes, function(cell) {
  233.                         if (cell.nodeType == 1) { // an element
  234.                             cell.className = cell.className.replace('sorttable_sorted_reverse', '');
  235.                             cell.className = cell.className.replace('sorttable_sorted', '');
  236.                         }
  237.                     });
  238.  
  239.                     sortfwdind = jDocument.find("#sorttable_sortfwdind");
  240.                     if (sortfwdind && sortfwdind.length) {
  241.                         sortfwdind.remove();
  242.                     }
  243.  
  244.  
  245.                     sortrevind = jDocument.find("#sorttable_sortrevind");
  246.                     if (sortrevind && sortrevind.length) {
  247.                         sortrevind.remove();
  248.                     };
  249.  
  250.                     j_current_headrow.addClass('sorttable_sorted');
  251.  
  252.                     sortfwdind = jQuery("<span></span>")[0];
  253.                     sortfwdind.id = "sorttable_sortfwdind";
  254.                     sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  255.                     this.appendChild(sortfwdind);
  256.  
  257.                     // build an array to sort. This is a Schwartzian transform thing,
  258.                     // i.e., we "decorate" each row with the actual sort key,
  259.                     // sort based on the sort keys, and then put the rows back in order
  260.                     // which is a lot faster because you only do getInnerText once per row
  261.                     var col, rows, headrow_tbody, row_array = [];
  262.  
  263.                     col = this.sorttable_columnindex;
  264.                     headrow_tbody = jQuery(this.sorttable_tbody);
  265.                     rows = this.sorttable_tbody.rows;
  266.  
  267.                     var row_sorttable_key, row_sorttable_key_checked = false;
  268.  
  269.                     for (var j = 0; j < rows.length; j++) {
  270.  
  271.                         var rowitem, jrowitem, cellitem, jcellitem = false;
  272.  
  273.                         // The Table ROW
  274.                         rowitem = rows[j];
  275.                         jrowitem = jQuery(rowitem);
  276.  
  277.                         // Do not sort this row ....
  278.                         if (jrowitem.hasClass('sorttable_row_nosort')) {
  279.                             console.warn("Skipping no sorttable row");
  280.                             continue;
  281.                         };
  282.  
  283.                         // The Table Row, Cell Item
  284.                         cellitem = rows[j].cells[col];
  285.  
  286.                         if (typeof(cellitem) !== 'undefined') {
  287.                             jcellitem = jQuery(cellitem);
  288.                         } else {
  289.                             jcellitem = jrowitem.find_sorttable_cell_by_index(col);
  290.                             cellitem = jcellitem.get(0);
  291.                         };
  292.  
  293.                         if(!jrowitem.attr('id')) {
  294.                             var new_row_id = 'table-' + jTable.index() + "-row-" + j;
  295.                             jrowitem.attr('id', new_row_id);
  296.                             delete new_row_id;
  297.                         };
  298.  
  299.                         // Check if we are re-sorting this LIST
  300.                         if (!row_sorttable_key && !row_sorttable_key_checked) {
  301.  
  302.                             var jrowitem_data, jrowitem_data_isundefined = false;
  303.  
  304.                             // Get DATA from this ROW ITEM
  305.                             jrowitem_data_is_undefined = typeof(jrowitem.data()) === 'undefined';
  306.                             jrowitem_data = !jrowitem_data_is_undefined ? jrowitem.data() : {};
  307.  
  308.                             // Do not check the DATA any more
  309.                             if (jrowitem_data_is_undefined) {
  310.  
  311.                                 row_sorttable_key_checked = true;
  312.                                 row_sorttable_key = false;
  313.  
  314.                             } else {
  315.  
  316.                                 var jrowitem_sorttable_key, sorttable_original = false;
  317.  
  318.                                 jrowitem_sorttable_key = jrowitem_data['sorttable_row_key'];
  319.                                 jrowitem_sorttable_key = Number(jrowitem_sorttable_key);
  320.                                 sorttable_original = jrowitem_data['sorttable_original'];
  321.  
  322.                                 if (jrowitem_sorttable_key && sorttable_original) {
  323.                                     row_sorttable_key = true;
  324.                                 };
  325.  
  326.                                 delete jrowitem_sorttable_key, sorttable_original;
  327.                             }
  328.  
  329.                             delete jrowitem_data, jrowitem_data_isundefined;
  330.  
  331.                         };
  332.  
  333.  
  334.                         var rowInnerText, getRowInnerText = false;
  335.                         row_sorttable_key = typeof(row_sorttable_key) !== 'undefined' ? row_sorttable_key : false;
  336.  
  337.                         getRowInnerText = (!jcellitem.hasClass('sorttable_cell_nosort') || !row_sorttable_key);
  338.                         // console.log("getRowInnerText = " + getRowInnerText);
  339.  
  340.                         if (getRowInnerText) {
  341.  
  342.                             // Get the INNER TEXT on this ROW, Cell
  343.                             rowInnerText = sorttable.getInnerText(cellitem);
  344.  
  345.                             // Ensure we default this to ""
  346.                             var rowInnerTextType = typeof(rowInnerText);
  347.                             if (typeof(rowInnerText) === 'undefined') {
  348.                                 rowInnerText = '';
  349.                             };
  350.  
  351.                             // Image, Link Sortting
  352.                             if (!rowInnerText.length && (jcellitem && jcellitem.length)) {
  353.  
  354.                                 var jcellitem_img = jcellitem.find('img').first();
  355.                                 if (jcellitem_img && jcellitem_img.length) {
  356.                                     rowInnerText = jcellitem_img.attr('src');
  357.                                 };
  358.  
  359.                                 // If we still do not have rowInnerText
  360.                                 if (!rowInnerText) {
  361.                                     var jcellitem_link = jcellitem.find('a').first();
  362.                                     if (jcellitem_link && jcellitem_link.length) {
  363.                                         rowInnerText = jcellitem_link.text().trim();
  364.                                     };
  365.                                     delete jcellitem_link;
  366.                                 };
  367.                                 delete jcellitem_img;
  368.                             };
  369.  
  370.                         } else {
  371.                             rowInnerText = '';
  372.                         };
  373.  
  374.  
  375.                         // Add the "sorttable_row_index"
  376.                         if(!jrowitem.attr('data-sorttable_row_index')) {
  377.                             jrowitem.attr('data-sorttable_row_index', false);
  378.                         }
  379.                        
  380.  
  381.                         // Update the ROW ARRAY with row_array_item
  382.                         var row_array_item = [rowInnerText, jrowitem];
  383.  
  384.                         // Update row_array, with row_array_item
  385.                         // row_array[row_array.length] = row_array_item;
  386.                         row_array.push(row_array_item);
  387.  
  388.                         delete row_array_item, rowInnerText;
  389.                         delete rowitem, jrowitem, cellitem, jcellitem;
  390.  
  391.                     }
  392.  
  393.                     /* If you want a stable sort, uncomment the following line */
  394.                     // sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
  395.                     /* and comment out this one */
  396.                     row_array.sort(this.sorttable_sortfunction);
  397.                     // console.log(row_array);
  398.  
  399.                     if (row_sorttable_key) {
  400.                         var new_row_array = jQuery.Group_Sorttable_Keys(row_array);
  401.                         // console.log(new_row_array);
  402.                         if (new_row_array && new_row_array.length) {
  403.                             row_array = new_row_array;
  404.                         };
  405.                     };
  406.  
  407.                     // Empty the MEMORY for col, rows
  408.                     delete col, rows;
  409.  
  410.                     // Delete the ROW sorttable key
  411.                     delete row_sorttable_key;
  412.  
  413.                     // The Sorttable TBODY
  414.                     var tb = this.sorttable_tbody;
  415.  
  416.                     // Check if this table contains zebra
  417.                     if (jTable && jTable.length) {
  418.                         var table_zebra = jTable.find('tr.alt').length > 0;
  419.                     } else {
  420.                         var table_zebra = false;
  421.                     }
  422.  
  423.                     var row_array_sorttable = jQuery.Make_TBODY_Sorttable(row_array, table_zebra);
  424.  
  425.                     // Sort in regular fashion :P
  426.                     row_array_sorttable.regular_sort();
  427.                     // console.log(bsorttable_sorted_reverse);
  428.  
  429.                     // Make sure we reverse the search
  430.                     if (bsorttable_sorted_reverse && this.sorttable_tbody) {
  431.                         sorttable.reverse(this.sorttable_tbody);
  432.                     };
  433.  
  434.                     // delete row_array from js memory
  435.                     delete row_array;
  436.  
  437.                     // nothing has changed on this row
  438.                     headrowchanges[i] = false;
  439.  
  440.                 });
  441.  
  442.             };
  443.  
  444.             // Delete this DATA
  445.             delete headrow_item, j_headrow_item, j_headrow_item_className;
  446.  
  447.         };
  448.  
  449.     },
  450.  
  451.     guessType: function(table, column) {
  452.         // guess the type of a column based on its first non-blank row
  453.         sortfn = sorttable.sort_alpha;
  454.         for (var i = 0; i < table.tBodies[0].rows.length; i++) {
  455.             text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
  456.             if (text != '') {
  457.                 if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
  458.                     return sorttable.sort_numeric;
  459.                 }
  460.                 // check for a date: dd/mm/yyyy or dd/mm/yy
  461.                 // can have / or . or - as separator
  462.                 // can be mm/dd as well
  463.                 possdate = text.match(sorttable.DATE_RE)
  464.                 if (possdate) {
  465.                     // looks like a date
  466.                     first = parseInt(possdate[1]);
  467.                     second = parseInt(possdate[2]);
  468.                     if (first > 12) {
  469.                         // definitely dd/mm
  470.                         return sorttable.sort_ddmm;
  471.                     } else if (second > 12) {
  472.                         return sorttable.sort_mmdd;
  473.                     } else {
  474.                         // looks like a date, but we can't tell which, so assume
  475.                         // that it's dd/mm (English imperialism!) and keep looking
  476.                         sortfn = sorttable.sort_ddmm;
  477.                     }
  478.                 }
  479.             }
  480.         }
  481.         return sortfn;
  482.     },
  483.  
  484.     getInnerText: function(node) {
  485.  
  486.         var jNode = jQuery(node);
  487.         var jNodeInnerText = false;
  488.  
  489.         // StackTable Key Column, or we have something that is UNDEFINED
  490.         if (jNode.hasClass('st-key') || typeof(jNode) === 'undefined') {
  491.             return '';
  492.         };
  493.  
  494.         var node_is_jquery_obj = false;
  495.  
  496.         // No node
  497.         if (typeof(node) == 'undefined') {
  498.             return '';
  499.         };
  500.  
  501.         // Validate the Node
  502.         node_is_jquery_obj = (node.getAttribute);
  503.         node_is_jquery_obj = node_is_jquery_obj ? (jNode instanceof jQuery && jNode.length && typeof(jNode.prop('tagName')) !== 'undefined') : false;
  504.  
  505.         // Check whether jNode is jQuery ELEMENT that EXISTS within the current WEBPAGE
  506.         if (node_is_jquery_obj) {
  507.  
  508.             var jNodeElement = false;
  509.  
  510.             // Get only TEXT from this ELEMENT, and EXCLUDE CHILDREN
  511.             if (jNode.prop('tagName') == 'TD') {
  512.                 jNodeElement = jNode.clone().children().remove().end();
  513.             };
  514.  
  515.             // If this element is "TD" (element to be SORTTED).
  516.             if (jNodeElement && !jNode.attr('sorttable_customkey')) {
  517.  
  518.                 var jNodeElementText = jNodeElement.text().trim().length;
  519.  
  520.                 // Look at the SELECTION, for SELECTED OPTION
  521.                 if (jNode.find('select') && !jNodeElementText.length) {
  522.                     jNodeInnerText = jNode.find('select').first().find('option:selected').text().trim();
  523.                 };
  524.  
  525.                 // Clean up mem
  526.                 delete jNodeElementText;
  527.  
  528.             };
  529.  
  530.             // Clean up mem
  531.             delete jNodeElement;
  532.  
  533.         };
  534.  
  535.  
  536.         // We have found "jNodeInnerText", then just return it
  537.         if (jNodeInnerText) {
  538.             return jNodeInnerText;
  539.         };
  540.  
  541.         // Clean up mem, we won't be using these after this point
  542.         delete jNode;
  543.         delete jNodeInnerText;
  544.  
  545.         // gets the text we want to use for sorting for a cell.
  546.         // strips leading and trailing whitespace.
  547.         // this is *not* a generic getInnerText function; it's special to sorttable.
  548.         // for example, you can override the cell text with a customkey attribute.
  549.         // it also gets .value for <input> fields.
  550.  
  551.         hasInputs = (typeof node.getElementsByTagName == 'function') &&
  552.             node.getElementsByTagName('input').length;
  553.  
  554.         if (node.getAttribute && node.getAttribute("sorttable_customkey") != null) {
  555.             return node.getAttribute("sorttable_customkey");
  556.         } else if (typeof node.textContent != 'undefined' && !hasInputs) {
  557.             return node.textContent.replace(/^\s+|\s+$/g, '');
  558.         } else if (typeof node.innerText != 'undefined' && !hasInputs) {
  559.             return node.innerText.replace(/^\s+|\s+$/g, '');
  560.         } else if (typeof node.text != 'undefined' && !hasInputs) {
  561.             return node.text.replace(/^\s+|\s+$/g, '');
  562.         } else {
  563.             switch (node.nodeType) {
  564.                 case 3:
  565.                     if (node.nodeName.toLowerCase() == 'input') {
  566.                         return node.value.replace(/^\s+|\s+$/g, '');
  567.                     }
  568.                 case 4:
  569.                     return node.nodeValue.replace(/^\s+|\s+$/g, '');
  570.                     break;
  571.                 case 1:
  572.                 case 11:
  573.                     var innerText = '';
  574.                     for (var i = 0; i < node.childNodes.length; i++) {
  575.                         innerText += sorttable.getInnerText(node.childNodes[i]);
  576.                     }
  577.                     return innerText.replace(/^\s+|\s+$/g, '');
  578.                     break;
  579.                 default:
  580.                     return '';
  581.             };
  582.         };
  583.  
  584.     },
  585.  
  586.     reverse: function(tbody) {
  587.  
  588.         var sorttable_rows = jQuery.Make_TBODY_Sorttable(tbody);
  589.  
  590.         // console.log("REVERSE");
  591.        
  592.         sorttable_rows.reverse_sort();
  593.  
  594.     },
  595.  
  596.     /* sort functions
  597.      each sort function takes two parameters, a and b
  598.      you are comparing a[0] and b[0] */
  599.     sort_numeric: function(a, b) {
  600.         aa = parseFloat(a[0].replace(/[^0-9.-]/g, ''));
  601.         if (isNaN(aa)) aa = 0;
  602.         bb = parseFloat(b[0].replace(/[^0-9.-]/g, ''));
  603.         if (isNaN(bb)) bb = 0;
  604.         return aa - bb;
  605.     },
  606.     sort_alpha: function(a, b) {
  607.         if (a[0] == b[0]) return 0;
  608.         if (a[0] < b[0]) return -1;
  609.         return 1;
  610.     },
  611.     sort_ddmm: function(a, b) {
  612.         mtch = a[0].match(sorttable.DATE_RE);
  613.         y = mtch[3];
  614.         m = mtch[2];
  615.         d = mtch[1];
  616.         if (m.length == 1) m = '0' + m;
  617.         if (d.length == 1) d = '0' + d;
  618.         dt1 = y + m + d;
  619.         mtch = b[0].match(sorttable.DATE_RE);
  620.         y = mtch[3];
  621.         m = mtch[2];
  622.         d = mtch[1];
  623.         if (m.length == 1) m = '0' + m;
  624.         if (d.length == 1) d = '0' + d;
  625.         dt2 = y + m + d;
  626.         if (dt1 == dt2) return 0;
  627.         if (dt1 < dt2) return -1;
  628.         return 1;
  629.     },
  630.     //kryogenix sorttable - how to sort date columns that have empty cells
  631.     //http://stackoverflow.com/questions/9815426/kryogenix-sorttable-how-to-sort-date-columns-that-have-empty-cells
  632.     sort_mmdd: function(a, b) {
  633.         mtch = a[0].match(sorttable.DATE_RE);
  634.         if ((mtch == null) || (mtch == undefined)) {
  635.             y = 0;
  636.             d = 0;
  637.             m = 0;
  638.         } else {
  639.             y = mtch[3];
  640.             d = mtch[2];
  641.             m = mtch[1];
  642.         }
  643.  
  644.         if (m.length == 1) m = '0' + m;
  645.         if (d.length == 1) d = '0' + d;
  646.         dt1 = y + m + d;
  647.         mtch = b[0].match(sorttable.DATE_RE);
  648.         if ((mtch == null) || (mtch == undefined)) {
  649.             y = 0;
  650.             d = 0;
  651.             m = 0;
  652.         } else {
  653.             y = mtch[3];
  654.             d = mtch[2];
  655.             m = mtch[1];
  656.         }
  657.         if (m.length == 1) m = '0' + m;
  658.         if (d.length == 1) d = '0' + d;
  659.         dt2 = y + m + d;
  660.         if (dt1 == dt2) return 0;
  661.         if (dt1 < dt2) return -1;
  662.         return 1;
  663.     },
  664.     shaker_sort: function(list, comp_func) {
  665.         // A stable sort function to allow multi-level sorting of data
  666.         // see: http://en.wikipedia.org/wiki/Cocktail_sort
  667.         // thanks to Joseph Nahmias
  668.         var b = 0;
  669.         var t = list.length - 1;
  670.         var swap = true;
  671.  
  672.         while (swap) {
  673.             swap = false;
  674.             for (var i = b; i < t; ++i) {
  675.                 if (comp_func(list[i], list[i + 1]) > 0) {
  676.                     var q = list[i];
  677.                     list[i] = list[i + 1];
  678.                     list[i + 1] = q;
  679.                     swap = true;
  680.                 }
  681.             } // for
  682.             t--;
  683.  
  684.             if (!swap) break;
  685.  
  686.             for (var i = t; i > b; --i) {
  687.                 if (comp_func(list[i], list[i - 1]) < 0) {
  688.                     var q = list[i];
  689.                     list[i] = list[i - 1];
  690.                     list[i - 1] = q;
  691.                     swap = true;
  692.                 }
  693.             } // for
  694.             b++;
  695.  
  696.         } // while(swap)
  697.     }
  698. }
  699.  
  700. /* ******************************************************************
  701.    Supporting functions: bundled here to avoid depending on a library
  702.    ****************************************************************** */
  703.  
  704. // Dean Edwards/Matthias Miller/John Resig
  705.  
  706. /* for Mozilla/Opera9 */
  707. if (document.addEventListener) {
  708.     document.addEventListener("DOMContentLoaded", sorttable.init, false);
  709. }
  710.  
  711. /* for Internet Explorer */
  712. /*@cc_on @*/
  713. /*@if (@_win32)
  714.     document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  715.     var script = document.getElementById("__ie_onload");
  716.     script.onreadystatechange = function() {
  717.         if (this.readyState == "complete") {
  718.             sorttable.init(); // call the onload handler
  719.         }
  720.     };
  721. /*@end @*/
  722.  
  723. /* for Safari */
  724. if (/WebKit/i.test(navigator.userAgent)) { // sniff
  725.     var _timer = setInterval(function() {
  726.         if (/loaded|complete/.test(document.readyState)) {
  727.             sorttable.init(); // call the onload handler
  728.         }
  729.     }, 10);
  730. }
  731.  
  732. /* for other browsers */
  733. window.onload = sorttable.init;
  734.  
  735. // written by Dean Edwards, 2005
  736. // with input from Tino Zijdel, Matthias Miller, Diego Perini
  737.  
  738. // http://dean.edwards.name/weblog/2005/10/add-event/
  739.  
  740. function dean_addEvent(element, type, handler) {
  741.     if (element.addEventListener) {
  742.         element.addEventListener(type, handler, false);
  743.     } else {
  744.         // assign each event handler a unique ID
  745.         if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
  746.         // create a hash table of event types for the element
  747.         if (!element.events) element.events = {};
  748.         // create a hash table of event handlers for each element/event pair
  749.         var handlers = element.events[type];
  750.         if (!handlers) {
  751.             handlers = element.events[type] = {};
  752.             // store the existing event handler (if there is one)
  753.             if (element["on" + type]) {
  754.                 handlers[0] = element["on" + type];
  755.             }
  756.         }
  757.         // store the event handler in the hash table
  758.         handlers[handler.$$guid] = handler;
  759.         // assign a global event handler to do all the work
  760.         element["on" + type] = handleEvent;
  761.     }
  762. };
  763. // a counter used to create unique IDs
  764. dean_addEvent.guid = 1;
  765.  
  766. function removeEvent(element, type, handler) {
  767.     if (element.removeEventListener) {
  768.         element.removeEventListener(type, handler, false);
  769.     } else {
  770.         // delete the event handler from the hash table
  771.         if (element.events && element.events[type]) {
  772.             delete element.events[type][handler.$$guid];
  773.         }
  774.     }
  775. };
  776.  
  777. function handleEvent(event) {
  778.     var returnValue = true;
  779.     // grab the event object (IE uses a global event object)
  780.     event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
  781.     // get a reference to the hash table of event handlers
  782.     var handlers = this.events[event.type];
  783.     // execute each event handler
  784.     for (var i in handlers) {
  785.         this.$$handleEvent = handlers[i];
  786.         if (this.$$handleEvent(event) === false) {
  787.             returnValue = false;
  788.         }
  789.     }
  790.     return returnValue;
  791. };
  792.  
  793. function fixEvent(event) {
  794.     // add W3C standard event methods
  795.     event.preventDefault = fixEvent.preventDefault;
  796.     event.stopPropagation = fixEvent.stopPropagation;
  797.     return event;
  798. };
  799. fixEvent.preventDefault = function() {
  800.     this.returnValue = false;
  801. };
  802. fixEvent.stopPropagation = function() {
  803.     this.cancelBubble = true;
  804. }
  805.  
  806. // Dean's forEach: http://dean.edwards.name/base/forEach.js
  807. /*
  808.     forEach, version 1.0
  809.     Copyright 2006, Dean Edwards
  810.     License: http://www.opensource.org/licenses/mit-license.php
  811. */
  812.  
  813. // array-like enumeration
  814. if (!Array.forEach) { // mozilla already supports this
  815.     Array.forEach = function(array, block, context) {
  816.         for (var i = 0; i < array.length; i++) {
  817.             block.call(context, array[i], i, array);
  818.         }
  819.     };
  820. }
  821.  
  822. // generic enumeration
  823. Function.prototype.forEach = function(object, block, context) {
  824.     for (var key in object) {
  825.         if (typeof this.prototype[key] == "undefined") {
  826.             block.call(context, object[key], key, object);
  827.         }
  828.     }
  829. };
  830.  
  831. // character enumeration
  832. String.forEach = function(string, block, context) {
  833.     Array.forEach(string.split(""), function(chr, index) {
  834.         block.call(context, chr, index, string);
  835.     });
  836. };
  837.  
  838. // globally resolve forEach enumeration
  839. var forEach = function(object, block, context) {
  840.     if (object) {
  841.         var resolve = Object; // default
  842.         if (object instanceof Function) {
  843.             // functions have a "length" property
  844.             resolve = Function;
  845.         } else if (object.forEach instanceof Function) {
  846.             // the object implements a custom forEach method so use that
  847.             object.forEach(block, context);
  848.             return;
  849.         } else if (typeof object == "string") {
  850.             // the object is a string
  851.             resolve = String;
  852.         } else if (typeof object.length == "number") {
  853.             // the object is array-like
  854.             resolve = Array;
  855.         }
  856.         resolve.forEach(object, block, context);
  857.     }
  858. };
  859.  
  860.  
  861. jQuery.Make_TBODY_Sorttable = function(sorttable_argument, zebra_table) {
  862.  
  863.  
  864.     var new_rows, new_row_indexes, jtbody, jtbodyrows = false;
  865.  
  866.     new_rows = [];
  867.     new_row_indexes = [];
  868.  
  869.     var sorttable_argument_org = sorttable_argument;
  870.     // console.log(sorttable_argument);
  871.  
  872.     if (sorttable_argument instanceof Array) {
  873.         var first_row = sorttable_argument[0][1];
  874.         jtbodyrows = sorttable_argument; // get the rows
  875.         jtbody = jQuery(first_row).parents('tbody');
  876.     } else {
  877.         jtbody = jQuery(sorttable_argument);
  878.         jtbodyrows = jtbody.find("tr:not(.sorttable_row_nosort)").get();
  879.     };
  880.  
  881.     tbody = jtbody.get(0);
  882.     // console.log(tbody);
  883.  
  884.     // reverse the rows in a tbody
  885.     for (var i = 0; i < jtbodyrows.length; i++) {
  886.  
  887.         var tbodyrow, tbodyrow_index;
  888.  
  889.         tbodyrow = jQuery(tbody.rows[i]);
  890.         tbodyrow_index = tbodyrow.get_sorttable_index();
  891.         var tbodyrow_original = tbodyrow_index ? tbodyrow.is_sorttable_original() : false;
  892.         var skip_tbody_index = ((tbodyrow_index && new_row_indexes.length) && jQuery.inArray(tbodyrow_index, new_row_indexes));
  893.  
  894.         // Check if we should skip this ROW ITEM
  895.         if (skip_tbody_index && !tbodyrow_original) {
  896.             // console.warn("Skipping the TBODY ITEM " + tbodyrow_index);
  897.             continue;
  898.         };
  899.  
  900.         // Add "tbodyrow_index" to "new_row_indexes"
  901.         if (tbodyrow_index) {
  902.             new_row_indexes.push(tbodyrow_index);
  903.         };
  904.  
  905.         // Declare some varaibles ....
  906.         var sorttable_tbody_rows, tbody_item_row = false;
  907.  
  908.         // Get the SORTTABLE ROW ITEMS, relative to the current row
  909.         sorttable_tbody_rows = tbodyrow.get_sorttable_row_items();
  910.  
  911.         // If we have More than ONE row, and this ROW is not our ORIGINAL ROW ...
  912.         if (tbodyrow_index && sorttable_tbody_rows.length > 1 && !tbodyrow_original) {
  913.             // console.warn("Skipping the NON ORIGINAL ROW");
  914.             continue;
  915.         }
  916.  
  917.         tbody_item_row = {
  918.             main: tbodyrow.get(0),
  919.             subs: [],
  920.             index: tbodyrow_index,
  921.         };
  922.  
  923.         if (sorttable_tbody_rows && sorttable_tbody_rows.length) {
  924.             tbody_item_row['subs'] = sorttable_tbody_rows.get();
  925.             tbody_item_row['subs'].shift();
  926.         };
  927.  
  928.         // console.log(tbody_item_row);
  929.  
  930.         new_rows[new_rows.length] = tbody_item_row;
  931.  
  932.         delete tbodyrow, tbodyrow_index;
  933.         delete sorttable_tbody_rows, tbody_item_row;
  934.  
  935.     };
  936.  
  937.     // console.log(new_rows);
  938.  
  939.     var sorttable_row_func = function(jtbody, new_rows, zebra_table) {
  940.  
  941.         var rows = new_rows;
  942.         var tbody = jtbody;
  943.         var zebra_key = 0;
  944.         var zebra_table = typeof(zebra_table) !== 'undefined' ? zebra_table : false;
  945.  
  946.         this.tbody_append_row_item = function(tbody_item_row) {
  947.  
  948.             var main_row_item;
  949.  
  950.             if (zebra_table) {
  951.                 main_row_item = this.zebra_row_item(tbody_item_row.main);
  952.             } else {
  953.                 main_row_item = tbody_item_row.main;
  954.             }
  955.  
  956.             tbody.append(main_row_item);
  957.  
  958.             var main_zebra_class = this.get_zebra_class(main_row_item);
  959.  
  960.             if (tbody_item_row.subs.length > 0) {
  961.                 for (var rs = 0; rs < tbody_item_row.subs.length; rs++) {
  962.  
  963.                     var sub_row_item = tbody_item_row.subs[rs];
  964.  
  965.                     if (zebra_table) {
  966.                         sub_row_item.removeClass('alt');
  967.                         sub_row_item.addClass(main_zebra_class);
  968.                     };
  969.  
  970.                     tbody.append(sub_row_item);
  971.  
  972.                 };
  973.             };
  974.             return true;
  975.         };
  976.  
  977.         this.regular_sort = function() {
  978.             // console.log(rows);          
  979.             for(var r = 0; r < rows.length; r++) {
  980.                 var tbody_item_row = rows[r];
  981.                 this.tbody_append_row_item(tbody_item_row);
  982.                 delete tbody_item_row;
  983.             };
  984.         };
  985.  
  986.         this.reverse_sort = function() {
  987.             var tbody_reverse_sort = [];
  988.             for (var r = rows.length - 1; r >= 0; r--) {
  989.                 var tbody_item_row = rows[r];
  990.                 tbody_reverse_sort.push(tbody_item_row);
  991.                 delete tbody_item_row;
  992.             };
  993.             for(var rb = 0; rb < tbody_reverse_sort.length; rb++) {
  994.                 var tbody_item_row = tbody_reverse_sort[rb];
  995.                 this.tbody_append_row_item(tbody_item_row);
  996.                 delete tbody_item_row;
  997.             };
  998.             delete tbody_reverse_sort;
  999.         };
  1000.  
  1001.         this.get_zebra_class = function(row_item) {
  1002.             var zebra_class = '';
  1003.             row_item = jQuery(row_item);
  1004.             if (row_item && row_item.hasClass('alt')) {
  1005.                 zebra_class = 'alt';
  1006.             };
  1007.             return zebra_class;
  1008.         }
  1009.  
  1010.         this.zebra_row_item = function(row_item) {
  1011.  
  1012.  
  1013.             // Re-Zebra the ROW
  1014.             if ((row_item && row_item.length)) {
  1015.                 row_item.removeClass('alt'); // Remove "alt"
  1016.                 if (zebra_key > 0) {
  1017.                     row_item.addClass('alt');
  1018.                     zebra_key = 0;
  1019.                 } else {
  1020.                     zebra_key = zebra_key + 1;
  1021.                 };
  1022.                 row_item = row_item.get(0);
  1023.             };
  1024.  
  1025.             return row_item;
  1026.  
  1027.         };
  1028.  
  1029.         this.get_zebra_key = function() {
  1030.             return zebra_key;
  1031.         }
  1032.  
  1033.     };
  1034.  
  1035.     var sorttable_row_object = new sorttable_row_func(jtbody, new_rows, zebra_table);
  1036.  
  1037.     return sorttable_row_object;
  1038.  
  1039. }
  1040.  
  1041. jQuery.fn.get_sorttable_indexes = function() {
  1042.  
  1043.     var jrowitems, sorttable_indexes = false;
  1044.  
  1045.     jrowitems = jQuery(this);
  1046.     sorttable_indexes = [];
  1047.  
  1048.     jrowitems.each(function(row_index, row_item) {
  1049.  
  1050.         var sorttable_index_item = jQuery(row_item).attr('data-sorttable_row_key');
  1051.         sorttable_indexes.push(sorttable_index_item);
  1052.  
  1053.         delete sorttable_index_item;
  1054.  
  1055.     });
  1056.  
  1057.     delete jrowitems;
  1058.  
  1059.     return sorttable_indexes;
  1060.  
  1061. };
  1062.  
  1063. jQuery.fn.get_sorttable_index = function() {
  1064.  
  1065.     var jrowitem, sorttable_row_index;
  1066.  
  1067.     jrowitem = jQuery(this);
  1068.  
  1069.     if (!jrowitem.length) {
  1070.         return false;
  1071.     }
  1072.  
  1073.     sorttable_row_index = jrowitem.attr('data-sorttable_row_index');
  1074.  
  1075.     if ((typeof(sorttable_row_index) === 'undefined') || !sorttable_row_index) {
  1076.         console.warn("The sorttable_row_index cannot be approved");
  1077.         return false;
  1078.     };
  1079.  
  1080.     delete jrowitem, sorttable_row_index;
  1081.  
  1082.     return sorttable_row_index;
  1083.  
  1084. };
  1085.  
  1086. jQuery.fn.get_sorttable_row_items = function() {
  1087.  
  1088.     var jrowitem, sorttable_row_index, jrowitems = false;
  1089.  
  1090.     jrowitem = jQuery(this);
  1091.     sorttable_row_index = jrowitem.get_sorttable_index();
  1092.  
  1093.     if (sorttable_row_index) {
  1094.         jrowitems = jrowitem.parent('tbody').find(sorttable_row_index);;
  1095.     };
  1096.  
  1097.     delete jrowitem, sorttable_row_index, jrowitems;
  1098.  
  1099.     return jrowitems;
  1100.  
  1101. };
  1102.  
  1103. jQuery.fn.is_sorttable_original = function() {
  1104.  
  1105.     var jrowitem, is_sorttable_original = false;
  1106.  
  1107.     jrowitem = jQuery(this);
  1108.  
  1109.     is_sorttable_original = jrowitem.attr('data-sorttable_original');
  1110.     is_sorttable_original = (typeof(is_sorttable_original) !== 'undefined') ? is_sorttable_original : false;
  1111.  
  1112.     return is_sorttable_original;
  1113.  
  1114. };
  1115.  
  1116. jQuery.Group_Sorttable_Keys = function(row_array) {
  1117.  
  1118.     var row_item_list = jQuery.Create_Row_Item_List(row_array);
  1119.  
  1120.     if (!row_item_list || !row_item_list.length) {
  1121.         console.warn("row_item_list was EMPTY");
  1122.         return false;
  1123.     };
  1124.  
  1125.     var i, N = 0;
  1126.     var new_row_array = [];
  1127.     var expecting_sorttable_keys = [];
  1128.  
  1129.     N = row_item_list.length;
  1130.  
  1131.     for (i = 0; i < N; i++) {
  1132.  
  1133.         var row_list_item = row_item_list.items[i];
  1134.         var row_tbody = row_list_item.row.parent('tbody');
  1135.  
  1136.         if (!row_list_item.row.is_sorttable_original() || row_list_item.row.hasClass(row_list_item.new_class_index)) {
  1137.             continue;
  1138.         };
  1139.  
  1140.         // Get ROWS linked to this SORTTABLE ROW KEY
  1141.         row_list_item_pattern = "tr[data-sorttable_row_key='" + row_list_item.data['sorttable_row_key'] + "']";
  1142.         linked_row_items = row_tbody.find(row_list_item_pattern);
  1143.         linked_row_items = jQuery(linked_row_items);
  1144.         linked_row_items.addClass(row_list_item.new_class_index);
  1145.         linked_row_items.attr('data-sorttable_row_index', '.' + row_list_item.new_class_index);
  1146.  
  1147.         // Update our new ROW ITEMS
  1148.         new_row_array.push([row_list_item.text, linked_row_items]);
  1149.         expecting_sorttable_keys.push(row_list_item.data['sorttable_row_key']);
  1150.  
  1151.         // Clear the garbage collection
  1152.         delete row_list_item_pattern, linked_row_items;
  1153.  
  1154.     };
  1155.  
  1156.     delete i, N;
  1157.  
  1158.     return new_row_array;
  1159.  
  1160. };
  1161.  
  1162. jQuery.Create_Row_Item_List = function(row_array) {
  1163.  
  1164.     if (typeof(row_array) === 'undefined' || !row_array.length) {
  1165.         console.warn("No row_array supplied to Create_Row_Item_List");
  1166.         return false;
  1167.     };
  1168.  
  1169.     var new_row_item_list = {
  1170.         items: [],
  1171.         length: 0,
  1172.     };
  1173.  
  1174.     var row_index, row_len = 0;
  1175.  
  1176.     row_len = row_array.length;
  1177.  
  1178.     for (row_index = 0; row_index < row_len; row_index++) {
  1179.  
  1180.         var row_index_item, row_text_item, row_html_item = false;
  1181.  
  1182.         row_index_item = row_array[row_index];
  1183.         row_html_item = jQuery(row_index_item[1]);
  1184.         row_text_item = row_index_item[0];
  1185.  
  1186.         var new_row_item = {
  1187.             index: row_index,
  1188.             row: row_html_item,
  1189.             data: row_html_item.data(),
  1190.             text: row_text_item,
  1191.             new_class_index: false,
  1192.         };
  1193.  
  1194.         new_row_item.new_class_index = "sorttable_row_key-index-" + new_row_item.data['sorttable_row_key'];
  1195.  
  1196.         delete row_index_item, row_text_item, row_html_item;
  1197.  
  1198.         new_row_item_list.items.push(new_row_item);
  1199.  
  1200.         delete new_row_item;
  1201.  
  1202.     };
  1203.  
  1204.     delete row_index, row_len;
  1205.  
  1206.     new_row_item_list.length = new_row_item_list.items.length;
  1207.  
  1208.     return new_row_item_list;
  1209.  
  1210. };
  1211.  
  1212. jQuery.fn.find_sorttable_cell_by_index = function(column_index) {
  1213.  
  1214.     var rowItem = jQuery(this);
  1215.  
  1216.     if (rowItem.prop('tagName') != 'TR') {
  1217.         console.error("rowItem is not <TR> Element");
  1218.         return false;
  1219.     };
  1220.  
  1221.     var current_index = 0;
  1222.     var rowItemCells = rowItem.children("td");
  1223.     var located_cell_item = false;
  1224.  
  1225.     rowItemCells.each(function(cell_index, cell_item) {
  1226.  
  1227.         cell_item = jQuery(cell_item);
  1228.  
  1229.         var cell_item_colspan = cell_item.attr('colspan');
  1230.         cell_item_colspan = Number(cell_item_colspan);
  1231.  
  1232.         if (cell_item_colspan && cell_item_colspan > 0) {
  1233.             current_index = current_index + cell_item_colspan;
  1234.             current_index = Number(current_index);
  1235.         } else {
  1236.             current_index = current_index + cell_index;
  1237.         };
  1238.  
  1239.         if (current_index >= column_index || cell_index == column_index) {
  1240.             located_cell_item = cell_item;
  1241.             return false;
  1242.         };
  1243.  
  1244.     });
  1245.  
  1246.     return located_cell_item;
  1247.  
  1248. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement