Guest

Untitled

By: a guest on Jul 17th, 2011  |  syntax: JavaScript  |  size: 1.47 KB  |  hits: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. //@author allez
  2.  
  3. /// HTML table builder
  4.  
  5. function h_tag(tag,s,id,attr)
  6. {
  7.   html = '<' + tag;
  8.   if(id) html += ' id="' + id + '"';
  9.   if(attr) html += ' ' + attr;
  10.   html += '>' + s + '';
  11.   return html;
  12. }
  13.  
  14. function table_captions(o)
  15. {
  16.   html = ''
  17.   for (i in o) {
  18.       if(typeof(o)=='object' && o[i]['content']) {
  19.           html += h_tag('td', o[i]['content'], false, o[i]['attr']);
  20.       } else html += h_tag('td', o[i]);
  21.    
  22.   }
  23.   html = h_tag('tr',html);
  24.   return h_tag('thead', html);
  25. }
  26.  
  27. function o2arr(o)
  28. {
  29.   arr = new Array()
  30.  
  31.   $.each(o, function(i,item){
  32.       arr[i] = item
  33.   });
  34.  
  35.   return arr
  36. }
  37.  
  38. function table_row(line, captions) {
  39.   cont = o2arr(line)
  40.   html = ''
  41.   for (col in captions) {
  42.       if (cont[col]) html += h_tag('td', cont[col])
  43.       else html += h_tag('td', '')
  44.   }
  45.  
  46.   return html
  47. }
  48.  
  49. /*
  50. * Builds html table
  51. *
  52. * @param data - JSON array of table data
  53. * @param captions - table th cations
  54. * @param id -table id
  55. * @param clickhandler - event handler
  56. *
  57. * @return string
  58. */
  59. function build_table(data, captions, id, clickhandler)
  60. {
  61.   html = table_captions(captions)
  62.  
  63.  
  64.   for (i in data) {      
  65.       try {
  66.          
  67.           if (clickhandler) {
  68.                   handler = "onclick=\""+clickhandler+"('" + i + "')\""
  69.           } else handler = ''
  70.           html += h_tag('tr', table_row(data[i], captions), false, handler)
  71.       } catch(e) {}
  72.   }
  73.  
  74.   return h_tag('table',html,id,'class="tableclass"')
  75. }