Revert "datatable: simplify and cleanup some code" - jscancer - Javascript crap (relatively small)
(HTM) git clone git://git.codemadness.org/jscancer
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit c1bcedb1c1fc7c5869121f632a5c2ce5c369ae4a
(DIR) parent 75b7571cde3f25490a3042e346f508a4389bcba6
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Thu, 17 Aug 2017 19:04:01 +0200
Revert "datatable: simplify and cleanup some code"
This reverts commit 8f13ca642d42f2de56679e408389922c9f06f214.
Diffstat:
M datatable/datatable.js | 59 ++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 20 deletions(-)
---
(DIR) diff --git a/datatable/datatable.js b/datatable/datatable.js
@@ -8,10 +8,16 @@ function datatable_sort_default(x, y) {
}
function datatable_init(el) {
- var thead = el.tHead,
- tbody = el.tBodies[0];
- var ths = thead.children[0].children,
- cols = [];
+ var thead = el.getElementsByTagName("thead");
+ if (!thead.length)
+ return null;
+ var tbody = el.getElementsByTagName("tbody");
+ if (!tbody.length)
+ return null;
+ var ths = thead[0].children[0].children;
+ if (!ths.length)
+ return null;
+ var cols = [];
for (var i = 0; i < ths.length; i++)
cols.push({
filterable: ["1", "true"].indexOf(ths[i].getAttribute("data-filterable") || "true") != -1,
@@ -21,9 +27,9 @@ function datatable_init(el) {
});
var d = {
table: el,
- thead: thead,
+ thead: thead[0],
ths: ths,
- tbody: tbody,
+ tbody: tbody[0],
cols: cols,
sort: [], // sort options: [colidx, order (ASC = 0, DESC = 1)].
lazyscroll: ["1", "true"].indexOf(el.getAttribute("data-lazyscroll") || "") != -1,
@@ -34,32 +40,45 @@ function datatable_init(el) {
if (d.lazyscroll) {
var bodytable = document.createElement("table");
bodytable.className = el.className;
- bodytable.cellSpacing = bodytable.cellPadding = bodytable.border = "0";
- var headerstable = bodytable.cloneNode(true);
+ bodytable.setAttribute("cellspacing", "0");
+ bodytable.setAttribute("cellpadding", "0");
+ bodytable.setAttribute("border", "0");
- var elthead = document.createElement("thead"),
- tr = thead.children[0].cloneNode(true);
- d.ths = tr.children; // set new columns (for sorting etc)..
+ var tr = document.createElement("tr");
+ for (var i = 0; i < ths.length; i++) {
+ var th = ths[i].cloneNode(true);
+ th.innerHTML = "";
+ tr.appendChild(th);
+ }
+ var bodythead = document.createElement("thead");
+ bodythead.appendChild(tr);
+
+ tr = document.createElement("tr");
+ var newths = [];
+ for (var i = 0; i < ths.length; i++)
+ newths.push(tr.appendChild(ths[i].cloneNode(true)));
+ d.ths = newths; // set new columns (for sorting etc)..
+ var elthead = document.createElement("thead");
elthead.appendChild(tr);
+ var headerstable = document.createElement("table");
+ headerstable.setAttribute("cellspacing", "0");
+ headerstable.setAttribute("cellpadding", "0");
+ headerstable.setAttribute("border", "0");
+ headerstable.className = el.className;
headerstable.appendChild(elthead);
- tr = tr.cloneNode(true);
- for (var i = 0; i < tr.children.length; i++)
- tr.children[i].innerHTML = "";
- var bodythead = document.createElement("thead");
- bodythead.appendChild(tr);
- bodytable.appendChild(bodythead);
-
var headersel = document.createElement("div");
headersel.className = "datatable-lazyscroll-headers";
headersel.appendChild(headerstable);
- var bodyel = headersel.cloneNode(false);
+ bodytable.appendChild(bodythead);
+
+ var bodyel = document.createElement("div");
bodyel.className = "datatable-lazyscroll-body";
bodyel.appendChild(bodytable);
- var containerel = headersel.cloneNode(false);
+ var containerel = document.createElement("div");
containerel.className = "datatable-lazyscroll-container";
containerel.appendChild(headersel);
containerel.appendChild(bodyel);