Skip to content

Commit c31214d

Browse files
committed
fix(directive): Title of first header is repeated for all columns if headers are being used on first column
In order to get the header to decorate a row, use preceding element on the same row, if it is a <th> Fixes #10
1 parent 37a8fa0 commit c31214d

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/directive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function wtResponsiveTable() {
1111
Array.prototype.forEach.call(rows, function(row) {
1212
var headerIndex = 0;
1313
Array.prototype.forEach.call(row.querySelectorAll('td'), function (value, index) {
14-
var title = headers.item(headerIndex).textContent;
14+
var th = value.parentElement.querySelector('th') || headers.item(headerIndex);
15+
var title = th.textContent;
1516
if (title && !value.getAttributeNode('data-title')) {
1617
value.setAttribute('data-title', title);
1718
}

tests/directive.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,44 @@ describe('directive', function () {
8282
expect(headerRow.eq(3).attr('data-title')).toBeUndefined();
8383
});
8484

85+
it('supports <th> as first column of each <tr>', function () {
86+
var markup = [
87+
'<table wt-responsive-table>',
88+
' <tr>',
89+
' <th>First title</th>',
90+
' <td>First column</td>',
91+
' </tr>',
92+
' <tr>',
93+
' <th>Second title</th>',
94+
' <td>Second column</td>',
95+
' </tr>',
96+
' <tr>',
97+
' <th>Third title</th>',
98+
' <td>Third column</td>',
99+
' </tr>',
100+
' <tr>',
101+
' <th>Forth title</th>',
102+
' <td>Forth column</td>',
103+
' </tr>',
104+
'</table>'
105+
].join('');
106+
var element = angular.element(markup);
107+
document.body.appendChild(element[0]);
108+
$compile(element);
109+
110+
var firstDataRow = element.find('tr td');
111+
expect(firstDataRow.eq(0).attr('data-title')).toBe('First title');
112+
expect(firstDataRow.eq(1).attr('data-title')).toBe('Second title');
113+
expect(firstDataRow.eq(2).attr('data-title')).toBe('Third title');
114+
expect(firstDataRow.eq(3).attr('data-title')).toBe('Forth title');
115+
116+
var headerRow = element.find('tr th');
117+
expect(headerRow.eq(0).attr('data-title')).toBeUndefined();
118+
expect(headerRow.eq(1).attr('data-title')).toBeUndefined();
119+
expect(headerRow.eq(2).attr('data-title')).toBeUndefined();
120+
expect(headerRow.eq(3).attr('data-title')).toBeUndefined();
121+
});
122+
85123
it('supports colspan', function () {
86124
var markup = [
87125
'<table wt-responsive-table>',

0 commit comments

Comments
 (0)