-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.csvToTable.js
154 lines (148 loc) · 5.98 KB
/
jquery.csvToTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* CSV to Table plugin
* http://code.google.com/p/jquerycsvtotable/
*
* Copyright (c) 2010 Steve Sobel
* http://honestbleeps.com/
*
* v0.9 - 2010-06-22 - First release.
*
* Example implementation:
* $('#divID').CSVToTable('test.csv');
*
* The above line would load 'test.csv' via AJAX and render a table. If
* headers are not specified, the plugin assumes the first line of the CSV
* file contains the header names.
*
* Configurable options:
* separator - separator to use when parsing CSV/TSV data
* - value will almost always be "," or "\t" (comma or tab)
* - if not specified, default value is ","
* headers - an array of headers for the CSV data
* - if not specified, plugin assumes that the first line of the CSV
* file contains the header names.
* - Example: headers: ['Album Title', 'Artist Name', 'Price ($USD)']
* tableClass - class name to apply to the <table> tag rendered by the plugin.
* theadClass - class name to apply to the <thead> tag rendered by the plugin.
* thClass - class name to apply to the <th> tag rendered by the plugin.
* tbodyClass - class name to apply to the <tbody> tag rendered by the plugin.
* trClass - class name to apply to the <tr> tag rendered by the plugin.
* tdClass - class name to apply to the <td> tag rendered by the plugin.
* loadingImage - path to an image to display while CSV/TSV data is loading
* loadingText - text to display while CSV/TSV is loading
* - if not specified, default value is "Loading CSV data..."
*
*
* Upon completion, the plugin triggers a "loadComplete" event so that you
* may perform other manipulation on the table after it has loaded. A
* common use of this would be to use the jQuery tablesorter plugin, found
* at http://tablesorter.com/
*
* An example of such a call would be as follows, assuming you have loaded
* the tablesorter plugin.
*
* $('#CSVTable').CSVToTable('test.csv',
* {
* loadingImage: 'images/loading.gif',
* startLine: 1,
* headers: ['Album Title', 'Artist Name', 'Price ($USD)']
* }
* ).bind("loadComplete",function() {
* $('#CSVTable').find('TABLE').tablesorter();
* });;
*
*/
(function($){
/**
*
* CSV Parser credit goes to Brian Huisman, from his blog entry entitled "CSV String to Array in JavaScript":
* http://www.greywyvern.com/?post=258
*
*/
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) {
var defaults = {
tableClass: "CSVTable",
theadClass: "",
thClass: "",
tbodyClass: "",
trClass: "",
tdClass: "",
loadingImage: "",
loadingText: "Loading CSV data...",
separator: ",",
startLine: 0
};
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();
$.each(lines, function(lineCount, line) {
if ((lineCount == 0) && (typeof(options.headers) != 'undefined')) {
headers = options.headers;
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 + '">';
}
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++;
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() {
// trigger loadComplete
setTimeout(function() {
obj.trigger("loadComplete");
},0);
});
}
});
});
};
})(jQuery);