Requires

Provides

HtmlTable.Sort.js

Builds a stripy, sortable table with methods to add rows.

License:
MIT-style license
Authors:
Harald Kirschner, Aaron Newton
  1. 30
  2. 31
  3. 32
  4. 33
  5. 34
  6. 35
  7. 36
  8. 37
  9. 38
  10. 39
  11. 40
  12. 41
  13. 42
  14. 43
  15. 44
  16. 45
  17. 46
  18. 47
  19. 48
  20. 49
  21. 50
  22. 51
  23. 52
  24. 53
  25. 54
  26. 55
  27. 56
  28. 57
  29. 58
  30. 59
  31. 60
  32. 61
  33. 62
  34. 63
  35. 64
  36. 65
  37. 66
  38. 67
  39. 68
  40. 69
  41. 70
  42. 71
  43. 72
  44. 73
  45. 74
  46. 75
  47. 76
HtmlTable = Class.refactor(HtmlTable, { options: {/* onSort: function(){}, */ sortIndex: 0, sortReverse: false, parsers: [], defaultParser: 'string', classSortable: 'table-sortable', classHeadSort: 'table-th-sort', classHeadSortRev: 'table-th-sort-rev', classNoSort: 'table-th-nosort', classGroupHead: 'table-tr-group-head', classGroup: 'table-tr-group', classCellSort: 'table-td-sort', classSortSpan: 'table-th-sort-span', sortable: false }, initialize: function (){ this.previous.apply(this, arguments); if (this.occluded) return this.occluded; this.sorted = {index: null, dir: 1}; this.bound = { headClick: this.headClick.bind(this) }; this.sortSpans = new Elements(); if (this.options.sortable){ this.enableSort(); if (this.options.sortIndex != null) this.sort(this.options.sortIndex, this.options.sortReverse); } }, attachSorts: function(attach){ this.element.removeEvents('click:relay(th)'); this.element[attach !== false ? 'addEvent' : 'removeEvent']('click:relay(th)', this.bound.headClick); }, setHeaders: function(){ this.previous.apply(this, arguments); if (this.sortEnabled) this.detectParsers(); }, detectParsers: function(force){ if (!this.head) return; var parsers = this.options.parsers, rows = this.body.rows;

auto-detect

  1. 79
  2. 80
  3. 81
  4. 82
  5. 83
  6. 84
  7. 85
  8. 86
  9. 87
  10. 88
  11. 89
  12. 90
  13. 91
  14. 92
  15. 93
  16. 94
  17. 95
  18. 96
  19. 97
  20. 98
  21. 99
  22. 100
  23. 101
  24. 102
  25. 103
  26. 104
  27. 105
  28. 106
  29. 107
  30. 108
  31. 109
  32. 110
  33. 111
  34. 112
  35. 113
  36. 114
  37. 115
  38. 116
  39. 117
  40. 118
  41. 119
  42. 120
  43. 121
  44. 122
  45. 123
  46. 124
  47. 125
  48. 126
  49. 127
  50. 128
  51. 129
  52. 130
  53. 131
  54. 132
  55. 133
  56. 134
  57. 135
  58. 136
  59. 137
  60. 138
  61. 139
  62. 140
  63. 141
  64. 142
  65. 143
  66. 144
  67. 145
  68. 146
  69. 147
  70. 148
  71. 149
  72. 150
  73. 151
  74. 152
  75. 153
  76. 154
  77. 155
  78. 156
  79. 157
  80. 158
  81. 159
  82. 160
  83. 161
  84. 162
  85. 163
  86. 164
  87. 165
  88. 166
  89. 167
  90. 168
  91. 169
  92. 170
  93. 171
  94. 172
  95. 173
  96. 174
  97. 175
  98. 176
  99. 177
  100. 178
  101. 179
  102. 180
  103. 181
  104. 182
  105. 183
  106. 184
  107. 185
  108. 186
  109. 187
  110. 188
  111. 189
  112. 190
  113. 191
  114. 192
  115. 193
  116. 194
  117. 195
  118. 196
  119. 197
  120. 198
  121. 199
  122. 200
  123. 201
  124. 202
  125. 203
  126. 204
  127. 205
  128. 206
  129. 207
  130. 208
  131. 209
  132. 210
  133. 211
  134. 212
  135. 213
  136. 214
  137. 215
  138. 216
  139. 217
  140. 218
  141. 219
  142. 220
  143. 221
  144. 222
  145. 223
  146. 224
  147. 225
  148. 226
  149. 227
  150. 228
  151. 229
  152. 230
  153. 231
  154. 232
  155. 233
  156. 234
  157. 235
  158. 236
  159. 237
  160. 238
  161. 239
  162. 240
  163. 241
  164. 242
  165. 243
  166. 244
  167. 245
  168. 246
  169. 247
  170. 248
  171. 249
  172. 250
  173. 251
  174. 252
  175. 253
  176. 254
  177. 255
  178. 256
  179. 257
  180. 258
  181. 259
  182. 260
  183. 261
  184. 262
  185. 263
  186. 264
  187. 265
  188. 266
  189. 267
  190. 268
  191. 269
  192. 270
  193. 271
  194. 272
  195. 273
  196. 274
  197. 275
  198. 276
  199. 277
  200. 278
  201. 279
  202. 280
  203. 281
  204. 282
  205. 283
  206. 284
  207. 285
  208. 286
  209. 287
  210. 288
  211. 289
  212. 290
  213. 291
  214. 292
  215. 293
  216. 294
  217. 295
  218. 296
  219. 297
  220. 298
  221. 299
  222. 300
  223. 301
  224. 302
  225. 303
  226. 304
  227. 305
  228. 306
this.parsers = $$(this.head.cells).map(function(cell, index){ if (!force && (cell.hasClass(this.options.classNoSort) || cell.retrieve('htmltable-parser'))) return cell.retrieve('htmltable-parser'); var thDiv = new Element('div'); Array.each(cell.childNodes, function(node){ thDiv.adopt(node); }); thDiv.inject(cell); var sortSpan = new Element('span', {'html': '&#160;', 'class': this.options.classSortSpan}).inject(thDiv, 'top'); this.sortSpans.push(sortSpan); var parser = parsers[index], cancel; switch (typeOf(parser)){ case 'function': parser = {convert: parser}; cancel = true; break; case 'string': parser = parser; cancel = true; break; } if (!cancel){ Object.some(HtmlTable.Parsers, function(current) { var match = current.match; if (!match) return false; for (var i = 0, j = rows.length; i < j; i++){ var cell = document.id(rows[i].cells[index]); var text = cell ? cell.get('html').clean() : ''; if (text && match.test(text)){ parser = current; return true; } } }); } if (!parser) parser = this.options.defaultParser; cell.store('htmltable-parser', parser); return parser; }, this); }, headClick: function(event, el){ if (!this.head || el.hasClass(this.options.classNoSort)) return; var index = Array.indexOf(this.head.cells, el); this.sort(index); return false; }, sort: function(index, reverse, pre){ if (!this.head) return; var classCellSort = this.options.classCellSort; var classGroup = this.options.classGroup, classGroupHead = this.options.classGroupHead; if (!pre){ if (index != null){ if (this.sorted.index == index){ this.sorted.reverse = !(this.sorted.reverse); } else { if (this.sorted.index != null){ this.sorted.reverse = false; this.head.cells[this.sorted.index].removeClass(this.options.classHeadSort).removeClass(this.options.classHeadSortRev); } else { this.sorted.reverse = true; } this.sorted.index = index; } } else { index = this.sorted.index; } if (reverse != null) this.sorted.reverse = reverse; var head = document.id(this.head.cells[index]); if (head){ head.addClass(this.options.classHeadSort); if (this.sorted.reverse) head.addClass(this.options.classHeadSortRev); else head.removeClass(this.options.classHeadSortRev); } this.body.getElements('td').removeClass(this.options.classCellSort); } var parser = this.parsers[index]; if (typeOf(parser) == 'string') parser = HtmlTable.Parsers[parser]; if (!parser) return; if (!Browser.ie){ var rel = this.body.getParent(); this.body.dispose(); } var data = Array.map(this.body.rows, function(row, i){ var value = parser.convert.call(document.id(row.cells[index])); return { position: i, value: value, toString: function(){ return value.toString(); } }; }, this); data.reverse(true); data.sort(function(a, b){ if (a.value === b.value) return 0; return a.value > b.value ? 1 : -1; }); if (!this.sorted.reverse) data.reverse(true); var i = data.length, body = this.body; var j, position, entry, group; while (i){ var item = data[--i]; position = item.position; var row = body.rows[position]; if (row.disabled) continue; if (!pre){ if (group === item.value){ row.removeClass(classGroupHead).addClass(classGroup); } else { group = item.value; row.removeClass(classGroup).addClass(classGroupHead); } if (this.options.zebra) this.zebra(row, i); row.cells[index].addClass(classCellSort); } body.appendChild(row); for (j = 0; j < i; j++){ if (data[j].position > position) data[j].position--; } }; data = null; if (rel) rel.grab(body); return this.fireEvent('sort', [body, index]); }, reSort: function(){ if (this.sortEnabled) this.sort.call(this, this.sorted.index, this.sorted.reverse); return this; }, enableSort: function(){ this.element.addClass(this.options.classSortable); this.attachSorts(true); this.detectParsers(); this.sortEnabled = true; return this; }, disableSort: function(){ this.element.removeClass(this.options.classSortable); this.attachSorts(false); this.sortSpans.each(function(span){ span.destroy(); }); this.sortSpans.empty(); this.sortEnabled = false; return this; } }); HtmlTable.Parsers = { 'date': { match: /^\d{2}[-\/ ]\d{2}[-\/ ]\d{2,4}$/, convert: function(){ return Date.parse(this.get('text').stripTags()).format('db'); }, type: 'date' }, 'input-checked': { match: / type="(radio|checkbox)" /, convert: function(){ return this.getElement('input').checked; } }, 'input-value': { match: /<input/, convert: function(){ return this.getElement('input').value; } }, 'number': { match: /^\d+[^\d.,]*$/, convert: function(){ return this.get('text').stripTags().toInt(); }, number: true }, 'numberLax': { match: /^[^\d]+\d+$/, convert: function(){ return this.get('text').replace(/[^-?^0-9]/, '').stripTags().toInt(); }, number: true }, 'float': { match: /^[\d]+\.[\d]+/, convert: function(){ return this.get('text').replace(/[^-?^\d.]/, '').stripTags().toFloat(); }, number: true }, 'floatLax': { match: /^[^\d]+[\d]+\.[\d]+$/, convert: function(){ return this.get('text').replace(/[^-?^\d.]/, '').stripTags(); }, number: true }, 'string': { match: null, convert: function(){ return this.get('text').stripTags(); } }, 'title': { match: null, convert: function(){ return this.title; } } };

<1.2compat>

  1. 309
HtmlTable.Parsers = new Hash(HtmlTable.Parsers);

</1.2compat>

  1. 312
  2. 313
  3. 314
HtmlTable.defineParsers = function(parsers){ HtmlTable.Parsers = Object.append(HtmlTable.Parsers, parsers); };