forked from gkindel/csv-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.html
118 lines (104 loc) · 3.32 KB
/
csv.html
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
<!DOCTYPE html>
<html>
<head>
<title>CSV.parse() Test</title>
<script src="csv.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<!-- prettify - http://code.google.com/p/google-code-prettify/ -->
<link href="//google-code-prettify.googlecode.com/svn/trunk/src/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="//google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
<script>
$( function () {
$.ajax("csv.txt",
{
success : function (data, stats, xhr) {
$('#input').text(data);
}
})
.pipe( CSV.parse )
.done( function(rows) {
table("#table", rows );
console.log(rows);
$('#out')
.addClass("prettyprint")
.text( JSON.stringify(rows, null, " ") );
window.prettyPrint()
});
// dump to html for visualizing output
function table (el, data){
var t = $("<table />").appendTo( el );
var row, value, tr, td, span, j, i;
for( i = 0; i < data.length; i++ ) {
row = data[i];
tr = $("<tr/>").appendTo(t);
for( j = 0; j < row.length; j++ ) {
td = $("<td />")
.appendTo(tr);
span = $('<span />')
.appendTo(td)
.text( String(row[j]) );
}
}
}
})
</script>
<style>
#input {
background: #f0f0f0;
}
#table td {
background: white;
padding: 5px;
white-space: pre;
}
#table span {
background: #f0f0f0;
}
h2 {
border-bottom: 1px solid black;
}
h3 {
margin-bottom: 2px;
text-decoration: underline;
}
h4 {
font-style: italic;
}
ul,li, pre{
margin: 2px;
}
</style>
</head>
<body>
<h2>JavaScript CSV parser</h2>
<h3>Use:</h3>
<pre><code class="prettyprint">
// simple
var rows = CSV.parse( csv_text );
// w/ jQuery
$.ajax("csv.txt")
.pipe( CSV.parse )
.done( function(rows) {
//... do something
});</code></pre>
<h3>Example:</h3>
<h4>Input (<a href="csv.txt">link</a>):</h4>
<pre id="input"></pre>
<h4>Output as HTML Table:</h4>
<div id="table"></div>
<h4>Output as JSON:</h4>
<pre><code id="out"></code></pre>
<h3>About</h3>
<div>
Author: <a href="https://github.com/gkindel">gkindel</a>
<br>
License: <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a>
<br>
Further Reading:
<ul>
<li><a href="http://en.wikipedia.org/wiki/Comma-separated_values">http://en.wikipedia.org/wiki/Comma-separated_values</a></li>
<li><a href="http://www.ietf.org/rfc/rfc4180.txt">http://www.ietf.org/rfc/rfc4180.txt</a></li>
</ul>
</div>
</body>
</html>