jasonbrooks / centos / centos.org

Forked from centos/centos.org 5 years ago
Clone

Blame assets/js/jquery.csvToTable.js

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

545090
 *
545090
 */
545090

545090
 
545090
 (function($){
545090

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

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

545090
})(jQuery);