|
229 | 229 | infoContainer.data('message-empty-rows') : |
230 | 230 | "Sorry, we've got nothing here." |
231 | 231 | }, |
| 232 | + columnTemplates: {}, |
232 | 233 | tablesorter: { |
233 | 234 | widgetOptions: { |
234 | 235 | // possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows} |
|
242 | 243 |
|
243 | 244 | this.settings = $.extend(true, {}, dataAttributeDefaults, this.settings); |
244 | 245 |
|
| 246 | + // Copy over dataUrl to pager_ajaxUrl |
245 | 247 | this.settings.tablesorter.widgetOptions.pager_ajaxUrl = this.settings.dataUrl; |
246 | 248 |
|
247 | 249 | var tableElement = this.$element.find('.tablesorter'); |
|
259 | 261 | // Set up tablesorter and pager |
260 | 262 | this.ts = tableElement.tablesorter(this.settings.tablesorter); |
261 | 263 |
|
| 264 | + // Map default column template selectors based on data-column-template attribute in each column header |
| 265 | + var columns = this.ts[0].config.headerList; |
| 266 | + var columnTemplates = {}; |
| 267 | + for (var col = 0; col < columns.length; col++) { |
| 268 | + var columnName = $(columns[col]).data('column-name'); |
| 269 | + if (!columnName && this.settings.DEBUG) { |
| 270 | + console.error('Column number ' + col + ' is missing a data-column-name attribute.'); |
| 271 | + } |
| 272 | + columnTemplates[columnName] = $(columns[col]).data('column-template'); |
| 273 | + } |
| 274 | + |
| 275 | + // Merge in any column template selectors that were set in the ctor options |
| 276 | + columnTemplates = $.extend(true, columnTemplates, this.settings.columnTemplates); |
| 277 | + |
| 278 | + // Locate and compile templates for any string-identified column renderers |
| 279 | + // At the same time, build out a numerically indexed array of templates |
| 280 | + this.columnTemplates = []; |
| 281 | + for (var col = 0; col < columns.length; col++) { |
| 282 | + var columnName = $(columns[col]).data('column-name'); |
| 283 | + if (!columnTemplates[columnName] && this.settings.DEBUG) { |
| 284 | + console.error("No template found for column '" + columnName + "'."); |
| 285 | + } |
| 286 | + var columnTemplate = columnTemplates[columnName]; |
| 287 | + if (typeof columnTemplate === 'string') { |
| 288 | + this.columnTemplates.push(Handlebars.compile($(columnTemplate).html())); |
| 289 | + } else { |
| 290 | + this.columnTemplates.push(columnTemplate); |
| 291 | + } |
| 292 | + } |
| 293 | + |
262 | 294 | // Link CSV download button |
263 | 295 | this.settings.downloadButton.on('click', this.settings.onDownload); |
264 | 296 |
|
|
395 | 427 | rowTemplate = Handlebars.compile($(this.settings.rowTemplateSelector).html()); |
396 | 428 | } |
397 | 429 |
|
398 | | - // Build Handlebars templates based on column-template attribute in each column header |
399 | | - var columns = ts.config.headerList; |
400 | | - var columnTemplates = []; |
401 | | - for (var col = 0; col < columns.length; col++) { |
402 | | - var columnName = $(columns[col]).data('column-template'); |
403 | | - columnTemplates.push(Handlebars.compile($(columnName).html())); |
404 | | - } |
405 | | - |
406 | 430 | // Render table rows and cells via Handlebars |
| 431 | + var columns = ts.config.headerList; |
407 | 432 | for (var row = 0; row < size; row++) { |
408 | 433 | var cellData = { |
409 | 434 | rownum: row, |
|
413 | 438 |
|
414 | 439 | rows += rowTemplate(cellData); |
415 | 440 |
|
416 | | - for (col = 0; col < columns.length; col++) { |
417 | | - rows += columnTemplates[col](cellData); |
| 441 | + for (var col = 0; col < columns.length; col++) { |
| 442 | + rows += this.columnTemplates[col](cellData); |
418 | 443 | } |
419 | 444 |
|
420 | 445 | rows += '</tr>'; |
|
0 commit comments