Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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, optiones) {
- var defaults = {
- tableClass: "CSVTable",
- theadClass: "",
- thClass: "",
- tbodyClass: "",
- trClass: "",
- tdClass: "",
- separator: ",",
- startLine: 0
- };
- var options = $.extend(defaults, optiones);
- return this.each(function() {
- var obj = $(this);
- var error = '';
- $.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 columnCount = 9;
- $.each(lines, function(lineCount, line) {
- if (lineCount >= options.startLine) {
- var items = line.splitCSV(options.separator);
- if (items.length > 1) {
- printedLines++;
- (printedLines % 2) ? oddOrEven = 'odd' : oddOrEven = 'even';
- tableHTML += '<tr class="' + options.trClass + ' ' + oddOrEven + '">';
- $.each(items, function(itemCount, item) {
- var currentColumn = (itemCount % columnCount) + 1// add 1 because of zero index
- tableHTML += '<td class="column' + currentColumn + '">' + item + '</td>'; });
- tableHTML += '</tr>';
- }
- }
- });
- tableHTML += '</tbody></table>';
- if (error) {
- obj.html(error);
- } else {
- obj.fadeOut(500, function() {
- obj.html(tableHTML)
- }).fadeIn(function() {
- // trigger loadComplete
- setTimeout(function() {
- obj.trigger("loadComplete");
- },0);
- });
- }
- });
- });
- };
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement