Requires

Provides

HtmlTable.js

Builds table elements with methods to add rows.

License:
MIT-style license
Authors:
Aaron Newton
  1. 26
  2. 27
  3. 28
  4. 29
  5. 30
  6. 31
  7. 32
  8. 33
  9. 34
  10. 35
  11. 36
  12. 37
  13. 38
  14. 39
  15. 40
  16. 41
  17. 42
  18. 43
  19. 44
  20. 45
  21. 46
  22. 47
  23. 48
  24. 49
  25. 50
  26. 51
  27. 52
  28. 53
  29. 54
  30. 55
  31. 56
  32. 57
  33. 58
  34. 59
  35. 60
  36. 61
  37. 62
  38. 63
  39. 64
  40. 65
  41. 66
  42. 67
  43. 68
  44. 69
  45. 70
  46. 71
  47. 72
  48. 73
  49. 74
  50. 75
  51. 76
  52. 77
  53. 78
  54. 79
  55. 80
  56. 81
  57. 82
  58. 83
  59. 84
  60. 85
  61. 86
  62. 87
  63. 88
  64. 89
  65. 90
  66. 91
  67. 92
  68. 93
  69. 94
  70. 95
  71. 96
  72. 97
  73. 98
  74. 99
  75. 100
  76. 101
  77. 102
  78. 103
  79. 104
  80. 105
  81. 106
  82. 107
  83. 108
  84. 109
  85. 110
  86. 111
  87. 112
  88. 113
  89. 114
  90. 115
  91. 116
  92. 117
  93. 118
  94. 119
  95. 120
  96. 121
  97. 122
  98. 123
  99. 124
  100. 125
  101. 126
var HtmlTable = new Class({ Implements: [Options, Events, Class.Occlude], options: { properties: { cellpadding: 0, cellspacing: 0, border: 0 }, rows: [], headers: [], footers: [] }, property: 'HtmlTable', initialize: function(){ var params = Array.link(arguments, {options: Type.isObject, table: Type.isElement}); this.setOptions(params.options); this.element = params.table || new Element('table', this.options.properties); if (this.occlude()) return this.occluded; this.build(); }, build: function(){ this.element.store('HtmlTable', this); this.body = document.id(this.element.tBodies[0]) || new Element('tbody').inject(this.element); $$(this.body.rows); if (this.options.headers.length) this.setHeaders(this.options.headers); else this.thead = document.id(this.element.tHead); if (this.thead) this.head = document.id(this.thead.rows[0]); if (this.options.footers.length) this.setFooters(this.options.footers); this.tfoot = document.id(this.element.tFoot); if (this.tfoot) this.foot = document.id(this.thead.rows[0]); this.options.rows.each(function(row){ this.push(row); }, this); ['adopt', 'inject', 'wraps', 'grab', 'replaces', 'dispose'].each(function(method){ this[method] = this.element[method].bind(this.element); }, this); }, toElement: function(){ return this.element; }, empty: function(){ this.body.empty(); return this; }, set: function(what, items){ var target = (what == 'headers') ? 'tHead' : 'tFoot'; this[target.toLowerCase()] = (document.id(this.element[target]) || new Element(target.toLowerCase()).inject(this.element, 'top')).empty(); var data = this.push(items, {}, this[target.toLowerCase()], what == 'headers' ? 'th' : 'td'); if (what == 'headers') this.head = document.id(this.thead.rows[0]); else this.foot = document.id(this.thead.rows[0]); return data; }, setHeaders: function(headers){ this.set('headers', headers); return this; }, setFooters: function(footers){ this.set('footers', footers); return this; }, push: function(row, rowProperties, target, tag){ if (typeOf(row) == "element" && row.get('tag') == 'tr'){ row.inject(target || this.body); return { tr: row, tds: row.getChildren('td') }; } var tds = row.map(function(data){ var td = new Element(tag || 'td', data ? data.properties : {}), type = (data ? data.content : '') || data, element = document.id(type); if (typeOf(type) != 'string' && element) td.adopt(element); else td.set('html', type); return td; }); return { tr: new Element('tr', rowProperties).inject(target || this.body).adopt(tds), tds: tds }; } });