
Untitled
By: a guest on Jul 17th, 2011 | syntax:
JavaScript | size: 1.47 KB | hits: 44 | expires: Never
//@author allez
/// HTML table builder
function h_tag(tag,s,id,attr)
{
html = '<' + tag;
if(id) html += ' id="' + id + '"';
if(attr) html += ' ' + attr;
html += '>' + s + '';
return html;
}
function table_captions(o)
{
html = ''
for (i in o) {
if(typeof(o)=='object' && o[i]['content']) {
html += h_tag('td', o[i]['content'], false, o[i]['attr']);
} else html += h_tag('td', o[i]);
}
html = h_tag('tr',html);
return h_tag('thead', html);
}
function o2arr(o)
{
arr = new Array()
$.each(o, function(i,item){
arr[i] = item
});
return arr
}
function table_row(line, captions) {
cont = o2arr(line)
html = ''
for (col in captions) {
if (cont[col]) html += h_tag('td', cont[col])
else html += h_tag('td', '')
}
return html
}
/*
* Builds html table
*
* @param data - JSON array of table data
* @param captions - table th cations
* @param id -table id
* @param clickhandler - event handler
*
* @return string
*/
function build_table(data, captions, id, clickhandler)
{
html = table_captions(captions)
for (i in data) {
try {
if (clickhandler) {
handler = "onclick=\""+clickhandler+"('" + i + "')\""
} else handler = ''
html += h_tag('tr', table_row(data[i], captions), false, handler)
} catch(e) {}
}
return h_tag('table',html,id,'class="tableclass"')
}