1/* 2 * jQuery Basic Table 3 * Author: Jerry Low 4 */ 5 6(function ($) { 7 $.fn.basictable = function (options) { 8 var setup = function (table, data) { 9 var headings = []; 10 11 if (data.tableWrap) { 12 table.wrap('<div class="bt-wrapper"></div>'); 13 } 14 15 var format = ""; 16 17 if (table.find("thead tr th").length) { 18 format = "thead th"; 19 } else if (table.find("tbody tr th").length) { 20 format = "tbody tr th"; 21 } else if (table.find("th").length) { 22 format = "tr:first th"; 23 } else { 24 format = "tr:first td"; 25 } 26 27 $.each(table.find(format), function () { 28 var $heading = $(this); 29 var colspan = parseInt($heading.attr("colspan"), 10) || 1; 30 var row = $heading.closest("tr").index(); 31 32 if (!headings[row]) { 33 headings[row] = []; 34 } 35 36 for (var i = 0; i < colspan; i++) { 37 headings[row].push($heading); 38 } 39 }); 40 41 // Table Body 42 $.each(table.find("tbody tr"), function () { 43 setupRow($(this), headings, data); 44 }); 45 46 // Table Footer 47 $.each(table.find("tfoot tr"), function () { 48 setupRow($(this), headings, data); 49 }); 50 }; 51 52 var setupRow = function ($row, headings, data) { 53 $row.children().each(function () { 54 var $cell = $(this); 55 56 if ( 57 ($cell.html() === "" || $cell.html() === " ") && 58 !data.showEmptyCells 59 ) { 60 $cell.addClass("bt-hide"); 61 } else { 62 var cellIndex = $cell.index(); 63 var headingText = ""; 64 65 for (var j = 0; j < headings.length; j++) { 66 if (j != 0) { 67 headingText += ": "; 68 } 69 70 var $heading = headings[j][cellIndex]; 71 headingText += $heading.text(); 72 } 73 74 $cell.attr("data-th", headingText); 75 76 if ( 77 data.contentWrap && 78 !$cell.children().hasClass("bt-content") 79 ) { 80 $cell.wrapInner('<span class="bt-content" />'); 81 } 82 } 83 }); 84 }; 85 86 var unwrap = function (table) { 87 $.each(table.find("td"), function () { 88 var $cell = $(this); 89 var content = $cell.children(".bt-content").html(); 90 $cell.html(content); 91 }); 92 }; 93 94 var check = function (table, data) { 95 // Only change when table is larger than parent if force 96 // responsive is turned off. 97 if (!data.forceResponsive) { 98 if ( 99 table.removeClass("bt").outerWidth() > 100 table.parent().width() 101 ) { 102 start(table, data); 103 } else { 104 end(table, data); 105 } 106 } else { 107 if ($(window).width() <= data.breakpoint) { 108 start(table, data); 109 } else { 110 end(table, data); 111 } 112 } 113 }; 114 115 var start = function (table, data) { 116 table.addClass("bt"); 117 118 if (data.tableWrap) { 119 table.parent(".bt-wrapper").addClass("active"); 120 } 121 }; 122 123 var end = function (table, data) { 124 table.removeClass("bt"); 125 126 if (data.tableWrap) { 127 table.parent(".bt-wrapper").removeClass("active"); 128 } 129 }; 130 131 var destroy = function (table, data) { 132 table.find("td").removeAttr("data-th"); 133 134 if (data.tableWrap) { 135 table.unwrap(); 136 } 137 138 if (data.contentWrap) { 139 unwrap(table); 140 } 141 142 table.removeData("basictable"); 143 }; 144 145 var resize = function (table) { 146 if (table.data("basictable")) { 147 check(table, table.data("basictable")); 148 } 149 }; 150 151 // Get table. 152 this.each(function () { 153 var table = $(this); 154 155 // If table has already executed. 156 if (table.length === 0 || table.data("basictable")) { 157 if (table.data("basictable")) { 158 // Destroy basic table.
159 if (options == "destroy") { 160 destroy(table, table.data("basictable")); 161 } 162 // Start responsive mode. 163 else if (options === "start") { 164 start(table, table.data("basictable")); 165 } else if (options === "stop") { 166 end(table, table.data("basictable")); 167 } else { 168 check(table, table.data("basictable")); 169 } 170 } 171 return false; 172 } 173 174 // Extend Settings. 175 var settings = $.extend({}, $.fn.basictable.defaults, options); 176 177 var vars = { 178 breakpoint: settings.breakpoint, 179 contentWrap: settings.contentWrap, 180 forceResponsive: settings.forceResponsive, 181 noResize: settings.noResize, 182 tableWrap: settings.tableWrap, 183 showEmptyCells: settings.showEmptyCells, 184 }; 185 186 // Initiate 187 table.data("basictable", vars); 188 189 setup(table, table.data("basictable")); 190 191 if (!vars.noResize) { 192 check(table, table.data("basictable")); 193 194 $(window).bind("resize.basictable", function () { 195 resize(table); 196 }); 197 } 198 }); 199 }; 200 201 $.fn.basictable.defaults = { 202 breakpoint: 568, 203 contentWrap: true, 204 forceResponsive: true, 205 noResize: false, 206 tableWrap: false, 207 showEmptyCells: false, 208 }; 209})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.