Skip to content

Commit ca26328

Browse files
committed
Support for passing callbacks for column templates instead of Handlebars templates
1 parent 9e69ff9 commit ca26328

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Change Log
22

33
## v4.1.13-alpha
4-
- Implement `rowTemplateSelector` for customizing how ufTable rows are rendered (#787)
4+
- `ufTable`: Implement `rowTemplateSelector` for customizing how rows are rendered (#787)
5+
- `ufTable`: Support for passing callbacks for column templates instead of Handlebars templates
56
- Remove explicit references to `id` columns (#803)
67
- Fix Docker
78
- Append full name to User API

app/sprinkles/core/assets/userfrosting/js/uf-table.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
infoContainer.data('message-empty-rows') :
230230
"Sorry, we've got nothing here."
231231
},
232+
columnTemplates: {},
232233
tablesorter: {
233234
widgetOptions: {
234235
// possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
@@ -242,6 +243,7 @@
242243

243244
this.settings = $.extend(true, {}, dataAttributeDefaults, this.settings);
244245

246+
// Copy over dataUrl to pager_ajaxUrl
245247
this.settings.tablesorter.widgetOptions.pager_ajaxUrl = this.settings.dataUrl;
246248

247249
var tableElement = this.$element.find('.tablesorter');
@@ -259,6 +261,36 @@
259261
// Set up tablesorter and pager
260262
this.ts = tableElement.tablesorter(this.settings.tablesorter);
261263

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+
262294
// Link CSV download button
263295
this.settings.downloadButton.on('click', this.settings.onDownload);
264296

@@ -395,15 +427,8 @@
395427
rowTemplate = Handlebars.compile($(this.settings.rowTemplateSelector).html());
396428
}
397429

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-
406430
// Render table rows and cells via Handlebars
431+
var columns = ts.config.headerList;
407432
for (var row = 0; row < size; row++) {
408433
var cellData = {
409434
rownum: row,
@@ -413,8 +438,8 @@
413438

414439
rows += rowTemplate(cellData);
415440

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);
418443
}
419444

420445
rows += '</tr>';

0 commit comments

Comments
 (0)