| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| (function($){ |
| |
| |
| |
| |
| |
| |
| |
| String.prototype.splitCSV = function(sep) { |
| for (var thisCSV = this.split(sep = sep || ","), x = thisCSV.length - 1, tl; x >= 0; x--) { |
| if (thisCSV[x].replace(/"\s+$/, '"').charAt(thisCSV[x].length - 1) == '"') { |
| if ((tl = thisCSV[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') { |
| thisCSV[x] = thisCSV[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"'); |
| } else if (x) { |
| thisCSV.splice(x - 1, 2, [thisCSV[x - 1], thisCSV[x]].join(sep)); |
| } else thisCSV = thisCSV.shift().split(sep).concat(thisCSV); |
| } else thisCSV[x].replace(/""/g, '"'); |
| } return thisCSV; |
| }; |
| |
| $.fn.CSVToTable = function(csvFile, options) { |
| |
| this.processRow = function(headers , items){ |
| var outItems = []; |
| var header; |
| for(var h = 0 ; h <= headers.length ; h++){ |
| header = headers[h]; |
| if(typeof(header) == 'string' ){ |
| outItems.push(options.preRenderItem(header , items[h])); |
| }else if(typeof(header) != 'undefined') { |
| if(header.hidden) continue; |
| outItems.push(options.itemMerger(header , items)); |
| } |
| } |
| return outItems; |
| }; |
| this.preRenderItem = function(headerLabel , item) { |
| return item; |
| }; |
| this.itemMerger = function(header , items) { |
| var outItem = []; |
| for(var i in header.items){ |
| outItem.push(options.preRenderItem(items[i])); |
| } |
| return outItem.join(' '); |
| }; |
| var defaults = { |
| tableClass: "CSVTable", |
| theadClass: "", |
| thClass: "", |
| tbodyClass: "", |
| trClass: "", |
| tdClass: "", |
| loadingImage: "", |
| loadingText: "Loading CSV data...", |
| separator: ",", |
| startLine: 0, |
| rowProcessor: this.processRow, |
| preRenderItem: this.preRenderItem, |
| itemMerger: this.itemMerger |
| }; |
| var options = $.extend(defaults, options); |
| return this.each(function() { |
| var obj = $(this); |
| var error = ''; |
| (options.loadingImage) ? loading = '<div style="text-align: center"><img alt="' + options.loadingText + '" src="' + options.loadingImage + '" /><br>' + options.loadingText + '</div>' : loading = options.loadingText; |
| obj.html(loading); |
| $.get(csvFile, function(data) { |
| var tableHTML = '<table class="' + options.tableClass + '">'; |
| var lines = data.replace('\r','').split('\n'); |
| var printedLines = 0; |
| var headerCount = 0; |
| var headers = new Array(); |
| var headerLabel; |
| $.each(lines, function(lineCount, line) { |
| if ((lineCount == 0) && (typeof(options.headers) != 'undefined')) { |
| headers = options.headers; |
| tableHTML += '<thead class="' + options.theadClass + '"><tr class="' + options.trClass + '">'; |
| $.each(headers, function(index, header) { |
| if(typeof(header) != 'string' && typeof(header) != 'undefined') { |
| if(header.hidden) return; |
| } |
| headerCount++; |
| headerLabel = typeof(header) == 'string' ? header : header.label; |
| tableHTML += '<th class="' + options.thClass + '">' + headerLabel + '</th>'; |
| }); |
| tableHTML += '</tr></thead><tbody class="' + options.tbodyClass + '">'; |
| } |
| if ((lineCount == options.startLine) && (typeof(options.headers) == 'undefined')) { |
| headers = line.splitCSV(options.separator); |
| headerCount = headers.length; |
| tableHTML += '<thead class="' + options.theadClass + '"><tr class="' + options.trClass + '">'; |
| $.each(headers, function(headerCount, header) { |
| tableHTML += '<th class="' + options.thClass + '">' + header + '</th>'; |
| }); |
| tableHTML += '</tr></thead><tbody class="' + options.tbodyClass + '">'; |
| } else if (lineCount >= options.startLine) { |
| var items = line.splitCSV(options.separator); |
| if (items.length > 1) { |
| printedLines++; |
| items = options.rowProcessor(headers , items); |
| if (items.length != headerCount) { |
| error += 'error on line ' + lineCount + ': Item count (' + items.length + ') does not match header count (' + headerCount + ') \n'; |
| } |
| (printedLines % 2) ? oddOrEven = 'odd' : oddOrEven = 'even'; |
| tableHTML += '<tr class="' + options.trClass + ' ' + oddOrEven + '">'; |
| $.each(items, function(itemCount, item) { |
| tableHTML += '<td class="' + options.tdClass + '">' + item + '</td>'; |
| }); |
| tableHTML += '</tr>'; |
| } |
| } |
| }); |
| tableHTML += '</tbody></table>'; |
| if (error) { |
| obj.html(error); |
| } else { |
| obj.fadeOut(500, function() { |
| obj.html(tableHTML) |
| }).fadeIn(function() { |
| |
| setTimeout(function() { |
| obj.trigger("loadComplete"); |
| },0); |
| }); |
| } |
| } , "text"); |
| }); |
| }; |
| |
| })(jQuery); |