Skip to content

Commit 7ad5f68

Browse files
committed
fix(directive): error in positional matching
Due to divergence in matching `th` and `td`, an exception would be thrown. Fixes #16
1 parent 6388724 commit 7ad5f68

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/directive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function wtResponsiveTable() {
1212
var headerIndex = 0;
1313
Array.prototype.forEach.call(row.querySelectorAll('td'), function (value, index) {
1414
var th = value.parentElement.querySelector('th') || headers.item(headerIndex);
15-
var title = th.textContent;
15+
var title = th && th.textContent;
1616
if (title && !value.getAttributeNode('data-title')) {
1717
value.setAttribute('data-title', title);
1818
}

tests/directive.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ describe('directive', function () {
233233
expect(firstDataRow.eq(3).attr('data-title')).toBe('Forth title');
234234
});
235235

236+
it('supports ng-if applied on TD with data-title', function () {
237+
var markup = [
238+
'<table wt-responsive-table>',
239+
' <thead>',
240+
' <tr>',
241+
' <th>column</th>',
242+
' </tr>',
243+
' </thead>',
244+
' <tbody>',
245+
' <tr>',
246+
' <td ng-if="!condition">tom</td>',
247+
' <td ng-if="condition" data-title="column">jerry</td>',
248+
' </tr>',
249+
' </tbody>',
250+
'</table>'
251+
].join('');
252+
var element = angular.element(markup);
253+
var scope = $rootScope.$new();
254+
scope.condition = true;
255+
256+
var firstDataRow = element.find('tbody tr:first td');
257+
258+
$compile(element)(scope);
259+
260+
expect(firstDataRow.eq(1).text()).toBe('jerry');
261+
expect(firstDataRow.eq(1).attr('data-title')).toBe('column');
262+
});
263+
236264
it('supports bootstrap', function () {
237265
var markup = [
238266
'<table wt-responsive-table class="table" style="display: none;">',

0 commit comments

Comments
 (0)