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 ( 108 (data.breakpoint !== null && 109 $(window).width() <= data.breakpoint) || 110 (data.containerBreakpoint !== null && 111 table.parent().width() <= data.containerBreakpoint) 112 ) { 113 start(table, data) 114 } else { 115 end(table, data) 116 } 117 } 118 } 119 120 var start = function (table, data) { 121 table.addClass('bt') 122 123 if (data.tableWrap) { 124 table.parent('.bt-wrapper').addClass('active') 125 } 126 } 127 128 var end = function (table, data) { 129 table.removeClass('bt') 130 131 if (data.tableWrap) { 132 table.parent('.bt-wrapper').removeClass('active') 133 } 134 } 135 136 var destroy = function (table, data) { 137 table.find('td').removeAttr('data-th') 138 139 if (data.tableWrap) { 140 table.unwrap() 141 } 142 143 if (data.contentWrap) { 144 unwrap(table) 145 } 146 147 table.removeData('basictable') 148 } 149 150 var resize = function (table) { 151 if (table.data('basictable')) { 152 check(table, table.data('basictable')) 153 } 154 } 155 156 // Get table. 157 this.each(function () { 158 var table = $(this) 159 160 // If table has already executed. 161 if (table.length === 0 || table.data('basictable')) { 162 if (table.data('basictable')) { 163 // Destroy basic table. 164 if (options == 'destroy') { 165 destroy(table, table.data('basictable')) 166 } 167 // Start responsive mode. 168 else if (options === 'start') { 169 start(table, table.data('basictable')) 170 } else if (options === 'stop') { 171 end(table, table.data('basictable')) 172 } else { 173 check(table, table.data('basictable')) 174 } 175 } 176 return false 177 } 178 179 // Extend Settings. 180 var settings = $.extend({}, $.fn.basictable.defaults, options) 181 182 var vars = { 183 breakpoint: settings.breakpoint,
184 containerBreakpoint: settings.containerBreakpoint, 185 contentWrap: settings.contentWrap, 186 forceResponsive: settings.forceResponsive, 187 noResize: settings.noResize, 188 tableWrap: settings.tableWrap, 189 showEmptyCells: settings.showEmptyCells, 190 } 191 // Maintain the original functionality/defaults 192 if (vars.breakpoint === null && vars.containerBreakpoint === null) { 193 vars.breakpoint = 568 194 } 195 196 // Initiate 197 table.data('basictable', vars) 198 199 setup(table, table.data('basictable')) 200 201 if (!vars.noResize) { 202 check(table, table.data('basictable')) 203 204 $(window).bind('resize.basictable', function () { 205 resize(table) 206 }) 207 } 208 }) 209 } 210 211 $.fn.basictable.defaults = { 212 breakpoint: null, 213 containerBreakpoint: null, 214 contentWrap: true, 215 forceResponsive: true, 216 noResize: false, 217 tableWrap: false, 218 showEmptyCells: false, 219 } 220})(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.