Skip to content

Commit 533c9a8

Browse files
committed
Release v0.4.0
1 parent 93ec723 commit 533c9a8

4 files changed

+53
-22
lines changed

release/angular-responsive-tables.css

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99

1010
/* Force table to not be like tables anymore */
1111
/* table.responsive,*/
12-
.responsive thead,
13-
.responsive tbody,
14-
.responsive tr,
15-
.responsive th {
12+
.responsive > thead,
13+
.responsive > tbody,
14+
.responsive > tbody > tr,
15+
.responsive > thead > th {
1616
display: block;
1717
}
1818

1919
/* Hide table headers (but not display: none;, for accessibility) */
20-
.responsive thead tr, .responsive th {
20+
.responsive > thead > tr, .responsive > thead > tr > th, .responsive > tbody > tr > th {
2121
position: absolute;
2222
top: -9999px;
2323
left: -9999px;
2424
}
2525

26-
.responsive tr {
26+
.responsive > tbody > tr {
2727
border: 1px solid #ccc;
2828
}
2929

30-
.responsive td:nth-child(odd), .responsive td:nth-child(even) {
30+
.responsive > tbody > tr > td {
3131
/* Behave like a "row" */
3232
border: none;
3333
border-bottom: 1px solid #eee;
@@ -43,7 +43,7 @@
4343
min-height: 1em;
4444
}
4545

46-
.responsive td:nth-child(odd)::before, .responsive td:nth-child(even)::before {
46+
.responsive > tbody > tr > td::before {
4747
/* Now like a table header */
4848
position: absolute;
4949
/* Top/left values mimic padding */

release/angular-responsive-tables.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
// https://github.com/awerlang/angular-responsive-tables
22
(function() {
33
"use strict";
4+
function getFirstHeaderInRow(tr) {
5+
var th = tr.firstChild;
6+
while (th) {
7+
if (th.tagName === "TH") break;
8+
if (th.tagName === "TD") {
9+
th = null;
10+
break;
11+
}
12+
th = th.nextSibling;
13+
}
14+
return th;
15+
}
416
function getHeaders(element) {
5-
return element.querySelectorAll("tr > th");
17+
return [].filter.call(element.children().children().children(), function(it) {
18+
return it.tagName === "TH";
19+
});
620
}
721
function updateTitle(td, th) {
822
var title = th && th.textContent;
@@ -18,35 +32,50 @@
1832
function wtResponsiveTable() {
1933
return {
2034
restrict: "A",
21-
controller: ['$element', function($element) {
35+
controller: [ "$element", function($element) {
2236
return {
37+
contains: function(td) {
38+
var tableEl = $element[0];
39+
var el = td;
40+
do {
41+
if (el === tableEl) return true;
42+
if (el.tagName === "TABLE") return false;
43+
el = el.parentElement;
44+
} while (el);
45+
throw new Error("Table element not found for " + td);
46+
},
2347
getHeader: function(td) {
24-
var firstHeader = td.parentElement.querySelector("th");
48+
var firstHeader = getFirstHeaderInRow(td.parentElement);
2549
if (firstHeader) return firstHeader;
26-
var headers = getHeaders($element[0]);
50+
var headers = getHeaders($element);
2751
if (headers.length) {
2852
var row = td.parentElement;
2953
var headerIndex = 0;
30-
var found = Array.prototype.some.call(row.querySelectorAll("td"), function(value, index) {
54+
var found = Array.prototype.some.call(row.children, function(value, index) {
55+
if (value.tagName !== "TD") return false;
3156
if (value === td) {
3257
return true;
3358
}
3459
headerIndex += colspan(value);
3560
});
36-
return found ? headers.item(headerIndex) : null;
61+
return found ? headers[headerIndex] : null;
3762
}
3863
}
3964
};
40-
}],
65+
} ],
4166
compile: function(element, attrs) {
4267
attrs.$addClass("responsive");
43-
var headers = getHeaders(element[0]);
68+
var headers = getHeaders(element);
4469
if (headers.length) {
45-
var rows = element[0].querySelectorAll("tbody > tr");
70+
var rows = [].filter.call(element.children(), function(it) {
71+
return it.tagName === "TBODY";
72+
})[0].children;
4673
Array.prototype.forEach.call(rows, function(row) {
4774
var headerIndex = 0;
48-
Array.prototype.forEach.call(row.querySelectorAll("td"), function(value, index) {
49-
var th = value.parentElement.querySelector("th") || headers.item(headerIndex);
75+
[].forEach.call(row.children, function(value, index) {
76+
if (value.tagName !== "TD") return;
77+
var th = getFirstHeaderInRow(value.parentElement);
78+
th = th || headers[headerIndex];
5079
updateTitle(value, th);
5180
headerIndex += colspan(value);
5281
});
@@ -61,8 +90,10 @@
6190
require: "?^^wtResponsiveTable",
6291
link: function(scope, element, attrs, tableCtrl) {
6392
if (!tableCtrl) return;
93+
if (!tableCtrl.contains(element[0])) return;
6494
setTimeout(function() {
65-
Array.prototype.forEach.call(element[0].parentElement.querySelectorAll("td"), function(td) {
95+
[].forEach.call(element[0].parentElement.children, function(td) {
96+
if (td.tagName !== "TD") return;
6697
var th = tableCtrl.getHeader(td);
6798
updateTitle(td, th);
6899
});

release/angular-responsive-tables.min.css

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

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.

0 commit comments

Comments
 (0)