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 8f13ca642d42f2de56679e408389922c9f06f214
 (DIR) parent fa112f42ca153afa96c3c8fc251fa1d170de2beb
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Wed, 16 Aug 2017 18:34:42 +0200
       
       datatable: simplify and cleanup some code
       
       just fail loudly if a table has no <thead> or <tbody>.
       
       Diffstat:
         M datatable/datatable.js              |      59 +++++++++++--------------------
       
       1 file changed, 20 insertions(+), 39 deletions(-)
       ---
 (DIR) diff --git a/datatable/datatable.js b/datatable/datatable.js
       @@ -8,16 +8,10 @@ function datatable_sort_default(x, y) {
        }
        
        function datatable_init(el) {
       -        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 = [];
       +        var thead = el.tHead,
       +            tbody = el.tBodies[0];
       +        var ths = thead.children[0].children,
       +            cols = [];
                for (var i = 0; i < ths.length; i++)
                        cols.push({
                                filterable: ["1", "true"].indexOf(ths[i].getAttribute("data-filterable")   || "true") != -1,
       @@ -27,9 +21,9 @@ function datatable_init(el) {
                        });
                var d = {
                        table: el,
       -                thead: thead[0],
       +                thead: thead,
                        ths:   ths,
       -                tbody: tbody[0],
       +                tbody: tbody,
                        cols:  cols,
                        sort:  [], // sort options: [colidx, order (ASC = 0, DESC = 1)].
                        lazyscroll: ["1", "true"].indexOf(el.getAttribute("data-lazyscroll") || "") != -1,
       @@ -40,45 +34,32 @@ function datatable_init(el) {
                if (d.lazyscroll) {
                        var bodytable = document.createElement("table");
                        bodytable.className = el.className;
       -                bodytable.setAttribute("cellspacing", "0");
       -                bodytable.setAttribute("cellpadding", "0");
       -                bodytable.setAttribute("border", "0");
       +                bodytable.cellSpacing = bodytable.cellPadding = bodytable.border = "0";
       +                var headerstable = bodytable.cloneNode(true);
        
       -                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");
       +                var elthead = document.createElement("thead"),
       +                    tr = thead.children[0].cloneNode(true);
       +                d.ths = tr.children; // set new columns (for sorting etc)..
                        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);
        
       -                bodytable.appendChild(bodythead);
       -
       -                var bodyel = document.createElement("div");
       +                var bodyel = headersel.cloneNode(false);
                        bodyel.className = "datatable-lazyscroll-body";
                        bodyel.appendChild(bodytable);
        
       -                var containerel = document.createElement("div");
       +                var containerel = headersel.cloneNode(false);
                        containerel.className = "datatable-lazyscroll-container";
                        containerel.appendChild(headersel);
                        containerel.appendChild(bodyel);