Skip to content

pfTableView: change templateFn to receive the row item as second parameter #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/table/tableview/examples/table-view-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* <ul style='list-style-type: none'>
* <li>.header - (string) Text label for a column header
* <li>.itemField - (string) Item field to associate with a particular column.
* <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>
* <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>
* <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.
* 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.
* 'value' is the item[key] value.
Expand Down Expand Up @@ -110,7 +110,7 @@
{ header: "Status", itemField: "status", htmlTemplate: "status_template.html" },
{ header: "Name", itemField: "name", htmlTemplate: "name_template.html", colActionFn: onNameClick },
{ header: "Address", itemField: "address"},
{ header: "City", itemField: "city", templateFn: function(value) { return '<span class="text-success">' + value + '</span>' } },
{ header: "City", itemField: "city", templateFn: function(value, item) { return '<a href="#' + item.name + '" title="' + item.address + '">' + value + '</span>' } },
{ header: "State", itemField: "state"}
];

Expand Down
2 changes: 1 addition & 1 deletion src/table/tableview/examples/table-view-with-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* <ul style='list-style-type: none'>
* <li>.header - (string) Text label for a column header
* <li>.itemField - (string) Item field to associate with a particular column.
* <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>
* <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>
* <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.
* 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.
* 'value' is the item[key] value.
Expand Down
2 changes: 1 addition & 1 deletion src/table/tableview/table-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<td ng-repeat="col in $ctrl.columns" ng-init="key = col.itemField; value = item[key]">
<span ng-if="!col.htmlTemplate && !col.templateFn">{{value}}</span>
<span ng-if="col.htmlTemplate" ng-include="col.htmlTemplate"></span>
<span ng-if="col.templateFn" ng-bind-html="$ctrl.trustAsHtml(col.templateFn(value))"></span>
<span ng-if="col.templateFn" ng-bind-html="$ctrl.trustAsHtml(col.templateFn(value, item))"></span>
</td>

<td ng-if="$ctrl.actionButtons && $ctrl.actionButtons.length > 0" class="table-view-pf-actions" ng-repeat="actionButton in $ctrl.actionButtons">
Expand Down
5 changes: 3 additions & 2 deletions test/table/tableview/table-view.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Component: pfTableView', function () {
{itemField: 'uuid', header: 'ID'},
{itemField: 'name', header: 'Name', htmlTemplate: "name_template.html", colActionFn: onNameClick},
{itemField: 'size', header: 'Size'},
{itemField: 'capacity', header: 'Capacity', templateFn: function(value) { return '<span class="custom-template2">' + value + '</span>' }}
{itemField: 'capacity', header: 'Capacity', templateFn: function(value, item) { return '<span id="' + item.uuid + '" class="custom-template2">' + value + '</span>' }}
];

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