|
1 | 1 | // https://github.com/awerlang/angular-responsive-tables
|
2 | 2 | (function() {
|
3 | 3 | "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 | + } |
4 | 16 | 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 | + }); |
6 | 20 | }
|
7 | 21 | function updateTitle(td, th) {
|
8 | 22 | var title = th && th.textContent;
|
|
18 | 32 | function wtResponsiveTable() {
|
19 | 33 | return {
|
20 | 34 | restrict: "A",
|
21 |
| - controller: ['$element', function($element) { |
| 35 | + controller: [ "$element", function($element) { |
22 | 36 | 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 | + }, |
23 | 47 | getHeader: function(td) {
|
24 |
| - var firstHeader = td.parentElement.querySelector("th"); |
| 48 | + var firstHeader = getFirstHeaderInRow(td.parentElement); |
25 | 49 | if (firstHeader) return firstHeader;
|
26 |
| - var headers = getHeaders($element[0]); |
| 50 | + var headers = getHeaders($element); |
27 | 51 | if (headers.length) {
|
28 | 52 | var row = td.parentElement;
|
29 | 53 | 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; |
31 | 56 | if (value === td) {
|
32 | 57 | return true;
|
33 | 58 | }
|
34 | 59 | headerIndex += colspan(value);
|
35 | 60 | });
|
36 |
| - return found ? headers.item(headerIndex) : null; |
| 61 | + return found ? headers[headerIndex] : null; |
37 | 62 | }
|
38 | 63 | }
|
39 | 64 | };
|
40 |
| - }], |
| 65 | + } ], |
41 | 66 | compile: function(element, attrs) {
|
42 | 67 | attrs.$addClass("responsive");
|
43 |
| - var headers = getHeaders(element[0]); |
| 68 | + var headers = getHeaders(element); |
44 | 69 | 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; |
46 | 73 | Array.prototype.forEach.call(rows, function(row) {
|
47 | 74 | 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]; |
50 | 79 | updateTitle(value, th);
|
51 | 80 | headerIndex += colspan(value);
|
52 | 81 | });
|
|
61 | 90 | require: "?^^wtResponsiveTable",
|
62 | 91 | link: function(scope, element, attrs, tableCtrl) {
|
63 | 92 | if (!tableCtrl) return;
|
| 93 | + if (!tableCtrl.contains(element[0])) return; |
64 | 94 | 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; |
66 | 97 | var th = tableCtrl.getHeader(td);
|
67 | 98 | updateTitle(td, th);
|
68 | 99 | });
|
|
0 commit comments