Skip to content

Instantly share code, notes, and snippets.

@katoy
Created March 10, 2012 03:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katoy/2009925 to your computer and use it in GitHub Desktop.
Save katoy/2009925 to your computer and use it in GitHub Desktop.
csv, html-table, jqgrid
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Treegrid using json</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.8.17.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.7.1");
google.load("jqueryui", "1.8.16");
</script>
<script type="text/javascript" src="js/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="js/i18n/grid.locale-ja.js"></script>
<script src="js/jquery.jqGrid.src.js" type="text/javascript"></script>
<!-- See http://code.google.com/p/jquerycsvtotable/ -->
<script src="js/jquery.csvToTable.js" type="text/javascript"></script>
<style type="text/css">
html, body {
margin: 0;
padding: 0;
}
/* CSV Table Classes */
TABLE.CSVTable {
font: 0.8em Verdana,Arial,Geneva,Helvetica,sans-serif;
border-collapse: collapse;
width: 450px;
}
/* Header */
TABLE.CSVTable THEAD TR {
background: #E8EDFF;
}
TABLE.CSVTable TH {
font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
font-size: 1.2em;
}
/* Table Cells */
TABLE.CSVTable TD, TABLE.CSVTable TH {
padding: 8px;
text-align: left;
border-bottom: 1px solid #FFFFFF;
border-top: 1px solid transparent;
}
/* Default background color for rows */
TABLE.CSVTable TR {
background: #F0F0F0;
}
/* Background color for odd rows */
TABLE.CSVTable TR.odd {
background: #F9F9F9;
}
/* Hover color for all rows */
TABLE.CSVTable TR:hover {
background: #E8EDFF;
}
</style>
<script type="text/javascript">
$(function() {
"use strict";
jQuery.get('data.csv', function(data) {
$('#CSVSource').html('<pre>' + data + '</pre>');
});
$("#export-to-csv").click( function() {
exportExcel('treegrid');
});
$("#table-to-grid").click( function() {
tableToGrid("#table", { width: 300, height: 'auto'});
});
$("#csv-to-table").click( function() {
$('#view-csv').CSVToTable('data.csv');
});
// See http://stackoverflow.com/questions/2188762/php-jqgrid-export-to-excel
function exportExcel(grid_id) {
var grid = $("#" + grid_id)
var mya = new Array();
var colNames = new Array();
var i = 0, j = 0, ii = 0;
var csv = "";
mya = grid.getDataIDs(); // Get All IDs
var data = grid.getRowData(mya[0]); // Get First row to get the labels
for (i in data) { colNames[ii++] = i; } // capture col names
for(i = 0; i <colNames.length; i++) {
csv = csv + colNames[i] + "\t"; // output each Column as tab delimited
}
csv = csv + "\n";
for(i = 0; i < mya.length; i++) {
data = grid.getRowData(mya[i]); // get each row
for( j = 0; j < colNames.length; j++) {
csv = csv + data[colNames[j]]+"\t"; // output each column as tab delimited
}
csv = csv + "\n"; // output each row with end of line
}
csv = csv + "\n"; // end of line at the end
// alert(csv);
document.forms[0].csvBuffer.value = csv;
document.forms[0].method = 'POST';
// send it to server which will open this contents in excel file
document.forms[0].action = 'http://localhost:3000/csvexporter';
document.forms[0].target = '_blank';
document.forms[0].submit();
};
$("#treegrid").jqGrid({
url:'./tree.json',
datatype: 'json',
mtype: 'GET',
treeGrid: true,
treeGridModel: 'adjacency',
ExpandColumn : 'key',
// See http://jquery.keicode.com/ui/icons.php
treeIcons: {leaf:'ui-icon-document'},
colNames:['Key','Value'],
colModel :[
{name:'key', index:'key'},
{name:'value', index:'value'}
],
pager: '#pager',
rowNum:10,
rowList:[10,20,30],
sortname: 'key',
sortorder: 'desc',
viewrecords: true,
gridview: true,
caption: 'Tree Grid',
width: 400,
height: 'auto'
});
});
</script>
</head>
<body>
<div style="margin: 20px;">
<button id="export-to-csv">Export to CSV</button>(TAB separator)
<table id="treegrid"></table>
</div>
<div style="margin: 20px;">
<button id="table-to-grid">Tabel-to-grid</button>
<table id="table" border="1">
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
<tbody>
<tr>
<td>data 1</td>
<td>data 1</td>
</tr>
<tr>
<td>data 2</td>
<td>data 2</td>
</tr>
</tbody>
</table>
</div>
<div style="margin: 20px;">
See <a href="http://code.google.com/p/jquerycsvtotable/">http://code.google.com/p/jquerycsvtotable/</a>
<br/>
CSV Data: <a href="data.csv">data.csv</a>
<div id="CSVSource" style="border: solid 1px;"></div>
<button id="csv-to-table">csv to table</button>
<div id="view-csv"></div>
</div>
<form method="post" action="csvExport.php">
<input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
</form>
</body>
</html>
express = require("express")
app = express.createServer()
app.use express.bodyParser()
app.post "/csvexporter", (req, res) ->
res.attachment 'data.csv'
res.end req.body.csvBuffer, 'UTF-8'
app.listen 3000
console.log "start csvexporter on port 3000"
album artist price
lateralus tool 13.00
aenima tool 12.00
10,000 days tool 14.00
down in it nine inch nails 3.00
broken nine inch nails 6.00
{
"total":5,
"records":5,
"page":1,
"rows":[{
"id":"1",
"cell":[
"a",
"AAA",
0,"",false,true,true
]
},
{
"id":"2",
"cell":[
"a-1",
"BBB",
1,"1",true,true,true
]
},
{
"id":"3",
"cell":[
"a-2",
"CCC",
1,"1",false,true,true
]
},
{
"id":"4",
"cell":[
"a-2-1",
"DDD",
2,"3",true,true,true
]
},
{
"id":"5",
"cell":[
"b",
"EEE",
0,"",true,false,true
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment