Skip to content

Commit 4b96c28

Browse files
committed
fix(directive): Work with static tables
Iterate over all <tr> in order to setup data-title attribute. Fixes #3
1 parent b235959 commit 4b96c28

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

release/angular-responsive-tables.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
restrict: "A",
77
compile: function(element, attrs) {
88
attrs.$addClass("responsive");
9-
var headers = element[0].querySelectorAll("thead th");
10-
var rows = element[0].querySelectorAll("tbody tr");
11-
if (headers.length && rows.length) {
12-
rows = rows[0];
13-
var headerIndex = 0;
14-
Array.prototype.forEach.call(rows.querySelectorAll("td"), function(value, index) {
15-
var title = headers.item(headerIndex).textContent;
16-
if (title && !value.getAttributeNode("data-title")) {
17-
value.setAttribute("data-title", title);
18-
}
19-
var colspan = value.getAttributeNode("colspan");
20-
headerIndex += colspan ? parseInt(colspan.value) : 1;
9+
var headers = element[0].querySelectorAll("tr > th");
10+
if (headers.length) {
11+
var rows = element[0].querySelectorAll("tbody > tr");
12+
Array.prototype.forEach.call(rows, function(row) {
13+
var headerIndex = 0;
14+
Array.prototype.forEach.call(row.querySelectorAll("td"), function(value, index) {
15+
var title = headers.item(headerIndex).textContent;
16+
if (title && !value.getAttributeNode("data-title")) {
17+
value.setAttribute("data-title", title);
18+
}
19+
var colspan = value.getAttributeNode("colspan");
20+
headerIndex += colspan ? parseInt(colspan.value) : 1;
21+
});
2122
});
2223
}
2324
}

release/angular-responsive-tables.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directive.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ function wtResponsiveTable() {
55
restrict: 'A',
66
compile: function (element, attrs) {
77
attrs.$addClass('responsive');
8-
var headers = element[0].querySelectorAll('thead th');
9-
var rows = element[0].querySelectorAll('tbody tr');
10-
if (headers.length && rows.length) {
11-
rows = rows[0];
12-
var headerIndex = 0;
13-
Array.prototype.forEach.call(rows.querySelectorAll('td'), function (value, index) {
14-
var title = headers.item(headerIndex).textContent;
15-
if (title && !value.getAttributeNode('data-title')) {
16-
value.setAttribute('data-title', title);
17-
}
18-
19-
var colspan = value.getAttributeNode('colspan');
20-
headerIndex += colspan ? parseInt(colspan.value) : 1;
8+
var headers = element[0].querySelectorAll('tr > th');
9+
if (headers.length) {
10+
var rows = element[0].querySelectorAll('tbody > tr');
11+
Array.prototype.forEach.call(rows, function(row) {
12+
var headerIndex = 0;
13+
Array.prototype.forEach.call(row.querySelectorAll('td'), function (value, index) {
14+
var title = headers.item(headerIndex).textContent;
15+
if (title && !value.getAttributeNode('data-title')) {
16+
value.setAttribute('data-title', title);
17+
}
18+
19+
var colspan = value.getAttributeNode('colspan');
20+
headerIndex += colspan ? parseInt(colspan.value) : 1;
21+
});
2122
});
2223
}
2324
}

tests/directive.spec.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,47 @@ describe('directive', function () {
4747
expect(firstDataRow.eq(1).attr('data-title')).toBe('Forth title');
4848
});
4949

50+
it('support tables with multiple static rows', function () {
51+
var markup = [
52+
'<table wt-responsive-table>',
53+
' <thead>',
54+
' <tr>',
55+
' <th>First title</th>',
56+
' <th>Second title</th>',
57+
' <th>Third title</th>',
58+
' <th>Forth title</th>',
59+
' </tr>',
60+
' </thead>',
61+
' <tbody>',
62+
' <tr>',
63+
' <td>First column</td>',
64+
' <td>Second column</td>',
65+
' <td>Third column</td>',
66+
' <td>Forth column</td>',
67+
' </tr>',
68+
' <tr>',
69+
' <td>First column</td>',
70+
' <td>Second column</td>',
71+
' <td>Third column</td>',
72+
' <td>Forth column</td>',
73+
' </tr>',
74+
' </tbody>',
75+
'</table>'
76+
].join('');
77+
var element = angular.element(markup);
78+
79+
var rows = element.find('tbody tr');
80+
81+
$compile(element);
82+
83+
rows.each(function (index, element) {
84+
var titles = Array.prototype.map.call(element.querySelectorAll('td'), function (item) {
85+
return item.getAttribute('data-title');
86+
});
87+
expect(titles).toEqual(['First title', 'Second title', 'Third title', 'Forth title']);
88+
});
89+
});
90+
5091
it('supports ng-repeat applied on TR', function () {
5192
var markup = [
5293
'<table wt-responsive-table>',
@@ -83,7 +124,7 @@ describe('directive', function () {
83124
expect(firstDataRow.eq(3).attr('data-title')).toBe('Forth title');
84125
});
85126

86-
fit('supports bootstrap', function () {
127+
it('supports bootstrap', function () {
87128
var markup = [
88129
'<table wt-responsive-table class="table" style="display: none;">',
89130
' <thead>',

0 commit comments

Comments
 (0)