jcpunk / centos / centos.org

Forked from centos/centos.org 10 days ago
Clone

Blame static/assets/js/jquery.csvToTable.js

e8ec7a
/**
e8ec7a
 * CSV to Table plugin
e8ec7a
 * http://code.google.com/p/jquerycsvtotable/
e8ec7a
 *
e8ec7a
 * Copyright (c) 2010 Steve Sobel
e8ec7a
 * http://honestbleeps.com/
e8ec7a
 *
e8ec7a
 * v0.9 - 2010-06-22 - First release.
e8ec7a
 *
e8ec7a
 * Example implementation:
e8ec7a
 * $('#divID').CSVToTable('test.csv');
e8ec7a
 *
e8ec7a
 * The above line would load 'test.csv' via AJAX and render a table.  If 
e8ec7a
 * headers are not specified, the plugin assumes the first line of the CSV
e8ec7a
 * file contains the header names.
e8ec7a
 *
e8ec7a
 * Configurable options:
e8ec7a
 * separator    - separator to use when parsing CSV/TSV data
e8ec7a
 *              - value will almost always be "," or "\t" (comma or tab)
e8ec7a
 *              - if not specified, default value is ","
e8ec7a
 * headers      - an array of headers for the CSV data
e8ec7a
 *              - if not specified, plugin assumes that the first line of the CSV
e8ec7a
 *                file contains the header names.
e8ec7a
 *              - Example: headers: ['Album Title', 'Artist Name', 'Price ($USD)']
e8ec7a
 * tableClass   - class name to apply to the  tag rendered by the plugin.
e8ec7a
 * theadClass   - class name to apply to the  tag rendered by the plugin.
e8ec7a
 * thClass      - class name to apply to the  tag rendered by the plugin.
e8ec7a
 * tbodyClass   - class name to apply to the  tag rendered by the plugin.
e8ec7a
 * trClass      - class name to apply to the  tag rendered by the plugin.
e8ec7a
 * tdClass      - class name to apply to the  tag rendered by the plugin.
e8ec7a
 * loadingImage - path to an image to display while CSV/TSV data is loading
e8ec7a
 * loadingText  - text to display while CSV/TSV is loading
e8ec7a
 *              - if not specified, default value is "Loading CSV data..."
e8ec7a
 *
e8ec7a
 *
e8ec7a
 * Upon completion, the plugin triggers a "loadComplete" event so that you
e8ec7a
 * may perform other manipulation on the table after it has loaded. A
e8ec7a
 * common use of this would be to use the jQuery tablesorter plugin, found
e8ec7a
 * at http://tablesorter.com/
e8ec7a
 *
e8ec7a
 * An example of such a call would be as follows, assuming you have loaded
e8ec7a
 * the tablesorter plugin.
e8ec7a
 *
e8ec7a
 * $('#CSVTable').CSVToTable('test.csv', 
e8ec7a
 *     { 
e8ec7a
 *        loadingImage: 'images/loading.gif', 
e8ec7a
 *        startLine: 1,
e8ec7a
 *        headers: ['Album Title', 'Artist Name', 'Price ($USD)']
e8ec7a
 *     }
e8ec7a
 * ).bind("loadComplete",function() { 
e8ec7a
 *     $('#CSVTable').find('TABLE').tablesorter();
e8ec7a
 * });;
e8ec7a

e8ec7a
 *
e8ec7a
 */
e8ec7a

e8ec7a
 
e8ec7a
 (function($){
e8ec7a

e8ec7a
	/**
e8ec7a
	*
e8ec7a
	* CSV Parser credit goes to Brian Huisman, from his blog entry entitled "CSV String to Array in JavaScript":
e8ec7a
	* http://www.greywyvern.com/?post=258
e8ec7a
	*
e8ec7a
	*/
e8ec7a
	String.prototype.splitCSV = function(sep) {
e8ec7a
		for (var thisCSV = this.split(sep = sep || ","), x = thisCSV.length - 1, tl; x >= 0; x--) {
e8ec7a
			if (thisCSV[x].replace(/"\s+$/, '"').charAt(thisCSV[x].length - 1) == '"') {
e8ec7a
				if ((tl = thisCSV[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
e8ec7a
					thisCSV[x] = thisCSV[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
e8ec7a
				} else if (x) {
e8ec7a
					thisCSV.splice(x - 1, 2, [thisCSV[x - 1], thisCSV[x]].join(sep));
e8ec7a
				} else thisCSV = thisCSV.shift().split(sep).concat(thisCSV);
e8ec7a
			} else thisCSV[x].replace(/""/g, '"');
e8ec7a
		} return thisCSV;
e8ec7a
	};
e8ec7a

e8ec7a
	$.fn.CSVToTable = function(csvFile, options) {
e8ec7a
		var defaults = {
e8ec7a
			tableClass: "CSVTable",
e8ec7a
			theadClass: "",
e8ec7a
			thClass: "",
e8ec7a
			tbodyClass: "",
e8ec7a
			trClass: "",
e8ec7a
			tdClass: "",
e8ec7a
			loadingImage: "",
e8ec7a
			loadingText: "Loading CSV data...",
e8ec7a
			separator: ",",
e8ec7a
			startLine: 0
e8ec7a
		};	
e8ec7a
		var options = $.extend(defaults, options);
e8ec7a
		return this.each(function() {
e8ec7a
			var obj = $(this);
e8ec7a
			var error = '';
e8ec7a
			(options.loadingImage) ? loading = '
' + options.loadingText + '
' + options.loadingText + '
' : loading = options.loadingText;
e8ec7a
			obj.html(loading);
e8ec7a
			$.get(csvFile, function(data) {
e8ec7a
				var tableHTML = '';
e8ec7a
				var lines = data.replace('\r','').split('\n');
e8ec7a
				var printedLines = 0;
e8ec7a
				var headerCount = 0;
e8ec7a
				var headers = new Array();
e8ec7a
				$.each(lines, function(lineCount, line) {
e8ec7a
					if ((lineCount == 0) && (typeof(options.headers) != 'undefined')) {
e8ec7a
						headers = options.headers;
e8ec7a
						headerCount = headers.length;
e8ec7a
						tableHTML += '';
e8ec7a
						$.each(headers, function(headerCount, header) {
e8ec7a
							tableHTML += '' + header + '';
e8ec7a
						});
e8ec7a
						tableHTML += '';
e8ec7a
					}
e8ec7a
					if ((lineCount == options.startLine) && (typeof(options.headers) == 'undefined')) {
e8ec7a
						headers = line.splitCSV(options.separator);
e8ec7a
						headerCount = headers.length;
e8ec7a
						tableHTML += '';
e8ec7a
						$.each(headers, function(headerCount, header) {
e8ec7a
							tableHTML += '' + header + '';
e8ec7a
						});
e8ec7a
						tableHTML += '';
e8ec7a
					} else if (lineCount >= options.startLine) {
e8ec7a
						var items = line.splitCSV(options.separator);
e8ec7a
						if (items.length > 1) {
e8ec7a
							printedLines++;
e8ec7a
							if (items.length != headerCount) {
e8ec7a
								error += 'error on line ' + lineCount + ': Item count (' + items.length + ') does not match header count (' + headerCount + ') \n';
e8ec7a
							}
e8ec7a
							(printedLines % 2) ? oddOrEven = 'odd' : oddOrEven = 'even';
e8ec7a
							tableHTML += '';
e8ec7a
							$.each(items, function(itemCount, item) {
e8ec7a
								tableHTML += '' + item + '';
e8ec7a
							});
e8ec7a
							tableHTML += '';
e8ec7a
						}
e8ec7a
					}
e8ec7a
				});
e8ec7a
				tableHTML += '';
e8ec7a
				if (error) {
e8ec7a
					obj.html(error);
e8ec7a
				} else {
e8ec7a
					obj.fadeOut(500, function() {
e8ec7a
						obj.html(tableHTML)
e8ec7a
					}).fadeIn(function() {
e8ec7a
						// trigger loadComplete
e8ec7a
						setTimeout(function() {
e8ec7a
							obj.trigger("loadComplete");	
e8ec7a
						},0);
e8ec7a
					});
e8ec7a
				}
e8ec7a
			});
e8ec7a
		});
e8ec7a
	};
e8ec7a

e8ec7a
})(jQuery);