Skip to content

Instantly share code, notes, and snippets.

@imtmh
Forked from geothird/printextstore.js
Created July 15, 2013 16:16
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 imtmh/6001258 to your computer and use it in GitHub Desktop.
Save imtmh/6001258 to your computer and use it in GitHub Desktop.
var head = false;
var cols = 0;
function printStore(store,th) {
var a = store.data.items;
var h = new Array();
var d = new Array();
for (var i=0; i < a.length; i++) {
for (var propName in a[i].data) {
if (!(a[i].data[propName] instanceof Function)) {
d.push(a[i].data[propName]);
if(i<1) {
h.push(propName);
cols++;
}
}
}
};
var col = 0;
var rw = new Array();
var rs = new Array();
for (var i=0; i < d.length; i++) {
if(col<cols) {
rw.push(d[i]);
col++;
if(col==cols) {
col = 0;
rs.push(rw);
rw = new Array();
}
}
};
var ret = "";
if(th)
ret = "<html><head><title>Print</title></head><body>";
ret += t(h,rs,th);
if(th)
ret += "</body></html>";
head = false;
cols = 0;
return ret;
}
function t(h,d,th) {
if(th)
return "<table border=\"1\">"+tc(h,d,th)+"</table>";
else
return "<table>"+tc(h,d,th)+"</table>";
}
function tc(h,d,ih) {
var ret = "";
if(!head) {
ret += "<tr>";
for (var i=0; i < h.length; i++) {
if(ih)
ret += th(h[i]);
else
ret += td(h[i]);
};
ret += "</tr>";
head = true;
}
for (var i=0; i < d.length; i++) {
ret += "<tr>"+tdd(d[i])+"</tr>";
};
return ret;
}
function tdd(d) {
var ret = "";
for (var i=0; i < d.length; i++) {
ret += td(d[i]);
};
return ret;
}
function td(d) {
return "<td>"+d+"</td>";
}
function th(h) {
return "<th>"+h+"</th>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment