Skip to content

Commit 26acd65

Browse files
Merge pull request #666 from akieling/angular-patternfly-665
pfTableView: change templateFn to receive the row item as second parameter
2 parents 254e74a + a3f1aca commit 26acd65

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

src/table/tableview/examples/table-view-basic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* <ul style='list-style-type: none'>
2121
* <li>.header - (string) Text label for a column header
2222
* <li>.itemField - (string) Item field to associate with a particular column.
23-
* <li>.templateFn - (function) (optional) Template function used to render each cell of the column. Pro: more performant than `htmlTemplate`. Con: doesn't support AngularJS directives in the template, therefore it doesn't support things like ng-click. Example: <pre>templateFn: value => `<span class="text-danger">${value}</span>`</pre>
23+
* <li>.templateFn - (function(value, item)) (optional) Template function used to render each cell of the column. Pro: more performant than `htmlTemplate`. Con: doesn't support AngularJS directives in the template, therefore it doesn't support things like ng-click. Example: <pre>templateFn: (value, item) => `<a href="${item.id}">${value}</a>`</pre>
2424
* <li>.htmlTemplate - (string) (optional) id/name of an embedded ng/html template. Pro: supports AngularJS directives in the template. Con: poor performance on large tables. Ex: htmlTemplate="name_template.html". The template will be used to render each cell of the column.
2525
* Use <code>handleColAction(key, value)</code> in the template to call the <code>colActionFn</code> callback function you specify. 'key' is the item attribute name; which should equal the itemFld of a column.
2626
* 'value' is the item[key] value.
@@ -110,7 +110,7 @@
110110
{ header: "Status", itemField: "status", htmlTemplate: "status_template.html" },
111111
{ header: "Name", itemField: "name", htmlTemplate: "name_template.html", colActionFn: onNameClick },
112112
{ header: "Address", itemField: "address"},
113-
{ header: "City", itemField: "city", templateFn: function(value) { return '<span class="text-success">' + value + '</span>' } },
113+
{ header: "City", itemField: "city", templateFn: function(value, item) { return '<a href="#' + item.name + '" title="' + item.address + '">' + value + '</span>' } },
114114
{ header: "State", itemField: "state"}
115115
];
116116

src/table/tableview/examples/table-view-with-toolbar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* <ul style='list-style-type: none'>
2929
* <li>.header - (string) Text label for a column header
3030
* <li>.itemField - (string) Item field to associate with a particular column.
31-
* <li>.templateFn - (function) (optional) Template function used to render each cell of the column. Pro: more performant than `htmlTemplate`. Con: doesn't support AngularJS directives in the template, therefore it doesn't support things like ng-click. Example: <pre>templateFn: value => `<span class="text-danger">${value}</span>`</pre>
31+
* <li>.templateFn - (function(value, item)) (optional) Template function used to render each cell of the column. Pro: more performant than `htmlTemplate`. Con: doesn't support AngularJS directives in the template, therefore it doesn't support things like ng-click. Example: <pre>templateFn: (value, item) => `<a href="${item.id}">${value}</a>`</pre>
3232
* <li>.htmlTemplate - (string) (optional) id/name of an embedded ng/html template. Pro: supports AngularJS directives in the template. Con: poor performance on large tables. Ex: htmlTemplate="name_template.html". The template will be used to render each cell of the column.
3333
* Use <code>handleColAction(key, value)</code> in the template to call the <code>colActionFn</code> callback function you specify. 'key' is the item attribute name; which should equal the itemFld of a column.
3434
* 'value' is the item[key] value.

src/table/tableview/table-view.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<td ng-repeat="col in $ctrl.columns" ng-init="key = col.itemField; value = item[key]">
2121
<span ng-if="!col.htmlTemplate && !col.templateFn">{{value}}</span>
2222
<span ng-if="col.htmlTemplate" ng-include="col.htmlTemplate"></span>
23-
<span ng-if="col.templateFn" ng-bind-html="$ctrl.trustAsHtml(col.templateFn(value))"></span>
23+
<span ng-if="col.templateFn" ng-bind-html="$ctrl.trustAsHtml(col.templateFn(value, item))"></span>
2424
</td>
2525

2626
<td ng-if="$ctrl.actionButtons && $ctrl.actionButtons.length > 0" class="table-view-pf-actions" ng-repeat="actionButton in $ctrl.actionButtons">

test/table/tableview/table-view.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Component: pfTableView', function () {
3333
{itemField: 'uuid', header: 'ID'},
3434
{itemField: 'name', header: 'Name', htmlTemplate: "name_template.html", colActionFn: onNameClick},
3535
{itemField: 'size', header: 'Size'},
36-
{itemField: 'capacity', header: 'Capacity', templateFn: function(value) { return '<span class="custom-template2">' + value + '</span>' }}
36+
{itemField: 'capacity', header: 'Capacity', templateFn: function(value, item) { return '<span id="' + item.uuid + '" class="custom-template2">' + value + '</span>' }}
3737
];
3838

3939
$scope.items = [
@@ -154,7 +154,8 @@ describe('Component: pfTableView', function () {
154154
expect(customSpans.length).toBe(5);
155155
customSpans.each(function(i) {
156156
var result = $(this).parent().html();
157-
var expected = '<span class="custom-template2">' + $scope.items[i].capacity + '</span>';
157+
var item = $scope.items[i];
158+
var expected = '<span id="' + item.uuid + '" class="custom-template2">' + item.capacity + '</span>';
158159
expect(result).toBe(expected);
159160
});
160161
});

0 commit comments

Comments
 (0)