/** * 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
tag rendered by the plugin. * tbodyClass - class name to apply to the | tag rendered by the plugin. * trClass - class name to apply to the|||
---|---|---|---|
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 = ' ' + options.loadingText + '
|