Skip to content

Commit

Permalink
feat(directive+header): add data-title-text attribute to table cells
Browse files Browse the repository at this point in the history
The *parsed* value of the data-title is now added as a data-title-text attribute to tbody TD's and the TH's of the filter row.

This is useful for displaying the table in "card view" on smal screens. For example:

http://bootsnipp.com/snippets/featured/no-more-tables-respsonsive-table
  • Loading branch information
ccrowhurstram committed Jan 23, 2015
1 parent 68c9f5f commit 43e5c4b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ng-table/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</th>
</tr>
<tr ng-show="show_filter" class="ng-table-filters">
<th ng-repeat="column in $columns" ng-show="column.show(this)" class="filter">
<th data-title-text="{{parse(column.title)}}" ng-repeat="column in $columns" ng-show="column.show(this)" class="filter">
<div ng-repeat="(name, filter) in column.filter">
<div ng-if="column.filterTemplateURL" ng-show="column.filterTemplateURL">
<div ng-include="column.filterTemplateURL"></div>
Expand Down
13 changes: 11 additions & 2 deletions src/scripts/05-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ app.directive('ngTable', ['$compile', '$q', '$parse',
if (el.attr('ignore-cell') && 'true' === el.attr('ignore-cell')) {
return;
}

var getAttrValue = function(attr){
return el.attr('x-data-' + attr) || el.attr('data-' + attr) || el.attr(attr);
};

var parsedAttribute = function(attr, defaultValue) {
return function(scope) {
return $parse(el.attr('x-data-' + attr) || el.attr('data-' + attr) || el.attr(attr))(scope, {
var expr = getAttrValue(attr);
return $parse(expr)(scope, {
$columns: columns
}) || defaultValue;
};
Expand All @@ -69,7 +75,10 @@ app.directive('ngTable', ['$compile', '$q', '$parse',
delete filter.templateURL;
}

el.attr('data-title-text', parsedTitle()); // this used in responsive table
var titleExpr = getAttrValue('title');
if (titleExpr){
el.attr('data-title-text', '{{' + titleExpr + '}}'); // this used in responsive table
}
columns.push({
id: i++,
title: parsedTitle,
Expand Down
29 changes: 29 additions & 0 deletions test/tableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,33 @@ describe('ng-table', function() {
expect(rows.length).toBe(7);
}));

it('should show data-title-text', inject(function($compile, $rootScope, NgTableParams) {
var tbody = elm.find('tbody');

var params = new NgTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
scope.tableParams = params;
scope.$digest();

var filterRow = angular.element(elm.find('thead').find('tr')[1]);
var filterCells = filterRow.find('th');
expect(angular.element(filterCells[0]).attr('data-title-text').trim()).toBe('Name of person');
expect(angular.element(filterCells[1]).attr('data-title-text').trim()).toBe('Age');
expect(angular.element(filterCells[2]).attr('data-title-text').trim()).toBe('Money');

var dataRows = elm.find('tbody').find('tr');
var row = angular.element(dataRows[0]);
var cells = row.find('td');
expect(angular.element(cells[0]).attr('data-title-text').trim()).toBe('Name of person');
expect(angular.element(cells[1]).attr('data-title-text').trim()).toBe('Age');
expect(angular.element(cells[2]).attr('data-title-text').trim()).toBe('Money');
}));

});

0 comments on commit 43e5c4b

Please sign in to comment.