From edd53c30fdeeb7f622f16b84637a62c9ac231fd1 Mon Sep 17 00:00:00 2001 From: Mobius1 Date: Tue, 17 Oct 2017 01:00:02 +0100 Subject: [PATCH] v2.0.0-alpha.21 --- README.md | 2 +- dist/vanilla-dataTables.min.css | 2 +- dist/vanilla-dataTables.min.js | 4 +- src/vanilla-dataTables.css | 2 +- src/vanilla-dataTables.js | 227 +++++++++++++++++--------------- 5 files changed, 124 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index eae86c5..f8c431d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![](http://img.badgesize.io/Mobius1/Vanilla-DataTables/2.0/dist/vanilla-dataTables.min.js) ![](http://img.badgesize.io/Mobius1/Vanilla-DataTables/2.0/dist/vanilla-dataTables.min.js?compression=gzip&label=gzipped) -This is version 2.0 of Vanilla-DataTables. Current version is `2.0.0-alpha.20`. Not production ready. +This is version 2.0 of Vanilla-DataTables. Current version is `2.0.0-alpha.21`. Not production ready. ## Changes diff --git a/dist/vanilla-dataTables.min.css b/dist/vanilla-dataTables.min.css index 6fd4f17..3f51a9e 100644 --- a/dist/vanilla-dataTables.min.css +++ b/dist/vanilla-dataTables.min.css @@ -4,6 +4,6 @@ * Copyright (c) 2015-2017 Karl Saunders (http://mobius.ovh) * Licensed under MIT (http://www.opensource.org/licenses/mit-license.php) * - * Version: 2.0.0-alpha.20 + * Version: 2.0.0-alpha.21 * */.dt-wrapper.no-header .dt-container{border-top:1px solid #d9d9d9}.dt-wrapper.no-footer .dt-container{border-bottom:1px solid #d9d9d9}.dt-top,.dt-bottom{padding:8px 10px}.dt-top>div:first-child,.dt-bottom>div:first-child{float:left}.dt-top>div:last-child,.dt-bottom>div:last-child{float:right}.dt-selector{padding:6px}.dt-input{padding:6px 12px}.dt-info{margin:7px 0}.dt-pagination ul{margin:0;padding-left:0}.dt-pagination li{list-style:none;float:left}.dt-pagination a,.dt-ellipsis span{border:1px solid transparent;float:left;margin-left:2px;padding:6px 12px;position:relative;text-decoration:none;color:inherit}.dt-pagination a:hover{background-color:#d9d9d9}.dt-pagination .active a,.dt-pagination .active a:focus,.dt-pagination .active a:hover{background-color:#d9d9d9;cursor:default}.dt-pagination .dt-ellipsis span,.dt-pagination .disabled a,.dt-pagination .disabled a:focus,.dt-pagination .disabled a:hover{cursor:not-allowed}.dt-pagination .disabled a,.dt-pagination .disabled a:focus,.dt-pagination .disabled a:hover{cursor:not-allowed;opacity:.4}.dt-pagination .pager a{font-weight:bold}.dt-table{max-width:100%;width:100%;border-spacing:0}.dt-table>tbody>tr>td,.dt-table>tbody>tr>th,.dt-table>tfoot>tr>td,.dt-table>tfoot>tr>th,.dt-table>thead>tr>td,.dt-table>thead>tr>th{vertical-align:top;padding:8px 10px}.dt-table>thead>tr>th,.dt-table>thead>tr>td{vertical-align:bottom;text-align:left;border-bottom:1px solid #d9d9d9}.dt-table>tfoot>tr>th{vertical-align:bottom;text-align:left;border-top:1px solid #d9d9d9}.dt-table th{vertical-align:bottom;text-align:left}.dt-sorter{position:relative;cursor:pointer;&::before,&::after{content:"";height:0;width:0;position:absolute;right:12px;border-left:4px solid transparent;border-right:4px solid transparent;opacity:.2}&::before{border-top:4px solid #000;top:18px}&::after{border-bottom:4px solid #000;border-top:4px solid transparent;bottom:22px}&.asc::after,&.desc::before{opacity:.6}&.loading{&::before{opacity:0}&::after{width:15px;height:15px;border-width:3px;border-style:solid;border-color:#ccc #ccc #ccc #999;border-radius:50%;bottom:12px;right:10px;opacity:1;animation:400ms linear 0ms forwards infinite spin}}}.dataTables-empty{text-align:center}.dt-top::after,.dt-bottom::after{clear:both;content:" ";display:table}@keyframes spin{from{transform:rotate(0deg)}to{transform:rotate(360deg)}}.dt-editor-modal{position:absolute;left:0;top:0;width:100%;height:100%;background-color:rgba(0,0,0,0.6);animation:250ms ease 0ms fadeIn}.dt-editor-modal.closed{animation:250ms ease 0ms fadeIn}.dt-editor-modal.closed .dt-editor-inner{animation:250ms ease 0ms slideIn}.dt-editor-inner{width:30%;margin:10% auto;background-color:#fff;border-radius:5px;animation:250ms ease 0ms slideIn}.dt-editor-header{position:relative;border-bottom:1px solid #ccc}.dt-editor-header h4{font-size:20px;margin:0}.dt-editor-header button{position:absolute;right:10px;top:10px;background-color:transparent;border:0;cursor:pointer;font-size:24px;padding:5px;line-height:1;opacity:.6}.dt-editor-header button:hover{opacity:1}.dt-editor-header{padding:15px 30px}.dt-editor-block{padding:15px 60px}.dt-editor-row{margin:0 0 15px}.dt-editor-row:last-child{margin:0 0 5px}.dt-editor-row:last-child{text-align:right}.dt-editor-label{width:25%;text-align:right;padding:0 15px}.dt-editor-label,.dt-editor-input{display:inline-block}.dt-editor-input{padding:4px 6px;border:1px solid #ccc;width:100%;box-sizing:border-box;margin:-5px 0;font-size:inherit;font-family:inherit;font-weight:inherit;color:inherit}.dt-editor-row .dt-editor-input{margin:0;width:75%}.dt-editor-save{background-color:#27ae60;border:1px solid #27ae60;padding:6px 12px;font-size:inherit;font-family:inherit;font-weight:inherit;color:#fff;cursor:pointer;border-radius:3px}.dt-editor-save:hover{background-color:#2cc36b;border-color:#2cc36b}.dt-editor-wrapper{position:absolute}.dt-editor-menu{background:#fff none repeat scroll 0 0;border-radius:3px;margin:0;min-width:220px;padding:5px 0;box-shadow:0 0 10px 2px #aaa}.dt-editor-menu li{list-style:none}.dt-editor-menu a{box-sizing:border-box;color:inherit;display:block;padding:5px 15px;text-decoration:none;width:100%}.dt-editor-menu a:hover{background-color:#ddd}.dt-editor-separator{border-bottom:1px solid #aaa;margin:5px 0}@keyframes fadeIn{from{opacity:0}to{opacity:1}}@keyframes slideIn{from{opacity:0;transform:translate3d(0,-10%,0)}to{opacity:1;transform:translate3d(0,0%,0)}} \ No newline at end of file diff --git a/dist/vanilla-dataTables.min.js b/dist/vanilla-dataTables.min.js index 3eb728a..769323c 100644 --- a/dist/vanilla-dataTables.min.js +++ b/dist/vanilla-dataTables.min.js @@ -4,7 +4,7 @@ * Copyright (c) 2015-2017 Karl Saunders (http://mobius.ovh) * Licensed under MIT (http://www.opensource.org/licenses/mit-license.php) * - * Version: 2.0.0-alpha.20 + * Version: 2.0.0-alpha.21 * */ -!function(e,t){var n="DataTable";"object"==typeof exports?module.exports=t(n):"function"==typeof define&&define.amd?define([],t(n)):e[n]=t(n)}("undefined"!=typeof global?global:this.window||this.global,function(e){"use strict";var t=window,n=document,a=(n.body,{perPage:10,perPageSelect:[5,10,15,20,25],sortable:!0,searchable:!0,nextPrev:!0,firstLast:!1,prevText:"‹",nextText:"›",firstText:"«",lastText:"»",ellipsisText:"…",ascText:"▴",descText:"▾",truncatePager:!0,pagerDelta:2,fixedColumns:!0,fixedHeight:!1,header:!0,footer:!1,search:{includeHiddenColumns:!1},classes:{top:"dt-top",info:"dt-info",input:"dt-input",table:"dt-table",bottom:"dt-bottom",search:"dt-search",sorter:"dt-sorter",wrapper:"dt-wrapper",dropdown:"dt-dropdown",ellipsis:"dt-ellipsis",selector:"dt-selector",container:"dt-container",pagination:"dt-pagination"},labels:{placeholder:"Search...",perPage:"{select} entries per page",noRows:"No entries found",info:"Showing {start} to {end} of {rows} entries"},layout:{top:"{select}{search}",bottom:"{info}{pager}"}}),s=function(e){return"[object Object]"===Object.prototype.toString.call(e)},i=function(e){return Array.isArray(e)},r=function(e){var t=!1;try{t=JSON.parse(e)}catch(e){return!1}return!(null===t||!i(t)&&!s(t))&&t},o=function(e,t){return e.hasOwnProperty(t)},l=function(e,t){for(var n in t)if(t.hasOwnProperty(n)){var a=t[n];a&&s(a)?(e[n]=e[n]||{},l(e[n],a)):e[n]=a}return e},c=function(e,t,n){var a;if(s(e))for(a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.call(n,e[a],a);else if(i(e))for(a=0;a1){var s="pager",i=d("ul"),r=n.onFirstPage?1:n.currentPage-1,o=n.onlastPage?e:n.currentPage+1;a.firstLast&&i.appendChild(t.button(s,1,a.firstText)),a.nextPrev&&i.appendChild(t.button(s,r,a.prevText));var l=t.truncate();c(l,function(e){i.appendChild(e)}),a.nextPrev&&i.appendChild(t.button(s,o,a.nextText)),a.firstLast&&i.appendChild(t.button(s,e,a.lastText)),t.parent.appendChild(i)}},truncate:function(){var e,t=this,n=t.instance.config,a=2*n.pagerDelta,s=t.instance.currentPage,i=s-n.pagerDelta,r=s+n.pagerDelta,o=t.instance.totalPages,l=[],d=[];if(n.truncatePager){s<4-n.pagerDelta+a?r=3+a:s>o-(3-n.pagerDelta+a)&&(i=o-(2+a));for(var h=1;h<=o;h++)(1==h||h==o||h>=i&&h<=r)&&l.push(h);c(l,function(a){e&&(a-e==2?d.push(t.button("",e+1,e+1)):a-e!=1&&d.push(t.button(n.classes.ellipsis,0,n.ellipsisText,!0))),d.push(t.button(a==s?"active":"",a,a)),e=a})}else c(o,function(e){d.push(t.button(e==s?"active":"",e,e))});return d},button:function(e,t,n,a){return d("li",{class:e,html:a?""+n+"":''+n+""})}};var w=function(e){this.instance=e};w.prototype={render:function(e){var t=this.instance;if(e=e||t.currentPage,u(t.table.body),!(e<1||e>t.totalPages)){var n=t.table.header,a=document.createDocumentFragment();t.table.hasHeader&&(u(n.node),c(n.cells,function(e){e.hidden||n.node.appendChild(e.node)})),t.pages.length&&c(t.pages[e-1],function(e){u(e.node),c(e.cells,function(t){t.hidden||e.node.appendChild(t.node)}),a.append(e.node)}),t.table.body.appendChild(a),c(t.pagers,function(e){e.render()}),t.getInfo(),t.emit("rows.render")}},paginate:function(){var e=this.instance.config,t=this.instance.table.rows,n=this.instance;n.searching&&n.searchData&&(t=n.searchData),n.pages=t.map(function(n,a){return a%e.perPage==0?t.slice(a,a+e.perPage):null}).filter(function(e){return e}),n.totalPages=n.pages.length,n.currentPage>n.totalPages&&(n.currentPage=n.totalPages)},add:function(e,t){if(i(e))return t=t||0,i(e[0])?(c(e,function(e){e=this.instance.table.addRow(new m(e,this.instance.table.rows.length+1),t)},this),this.instance.table.update()):e=this.instance.table.addRow(new m(e,this.instance.table.rows.length+1),t,!0),this.instance.update(),e},remove:function(e){var t=!1,n=this.instance;if(i(e)){for(var a=e.length-1;a>=0;a--)n.table.removeRow(this.get(e[a]));n.table.update(),n.update()}else if(t=this.get(e))return n.table.removeRow(t,!0),n.update(),t},get:function(e){var t=this.instance.table.rows;if(e instanceof m||e instanceof Element){for(var n=0;nn.table.header.cells.length-1)return!1;var a=n.table.header.cells[e].node,s=n.table.rows;n.searching&&n.searchData&&(s=n.searchData),n.lastHeading&&p.remove(n.lastHeading,n.lastDirection),n.lastDirection&&p.remove(a,n.lastDirection),p.add(a,t);var i,r;a.hasAttribute("data-type")&&"date"===a.getAttribute("data-type")&&(i=!1,(r=a.hasAttribute("data-format"))&&(i=a.getAttribute("data-format"))),s.sort(function(n,a){var s=n.cells[e].content,o=a.cells[e].content;return r?(s=f(s,i,n.cells[e]),o=f(o,i,a.cells[e])):(s=s.replace(/(\$|\,|\s|%)/g,""),o=o.replace(/(\$|\,|\s|%)/g,"")),s=isNaN(s)?s:parseInt(s,10),o=isNaN(o)?o:parseInt(o,10),"asc"===t?s>o:s=a.cells.length)throw new Error("Column index "+e[r]+" is outside the range of columns.");n.table.hasHeader&&(t=[],c(e,function(e,n){t[n]=a.cells[e],t[n].index=t[n].node.dataIndex=n,a.node.appendChild(t[n].node)}),a.cells=t),c(s,function(n){t=[],c(e,function(e,a){t[a]=n.cells[e],t[a].index=t[a].node.dataIndex=a,n.node.appendChild(t[a].node)}),n.cells=t}),n.update(),n.emit("columns.order",e)}},hide:function(e){var t=this.instance,n=t.table.header,a=t.table.rows;isNaN(e)||(e=[e]);for(var s=0;s=0&&c(e.columnRenderers,function(a){a.columns.indexOf(n.index)>=0&&n.setContent(a.renderer.call(e,n.content,n,t))})})})),e.rows().render(),e.bindEvents(),e.initialised=!0,setTimeout(function(){e.emit("init")},10)}},initExtensions:function(){var e=this;c(["editable","exportable","filterable"],function(t){void 0!==e[t]&&"function"==typeof e[t]&&(e[t]=e[t](e,e.config[t],{each:c,extend:l,isObject:s,classList:p,createElement:d}),e[t].init&&"function"==typeof e[t].init&&e[t].init())})},bindEvents:function(){var e=this,t=e.config;h(e.wrapper,"mousedown",function(e){1===e.which&&t.sortable&&"TH"===e.target.nodeName&&p.add(e.target,"loading")}),h(e.wrapper,"click",function(n){var a=n.target;if(a.hasAttribute("data-page")&&(n.preventDefault(),e.page(parseInt(a.getAttribute("data-page"),10))),t.sortable&&"TH"===a.nodeName){if(a.hasAttribute("data-sortable")&&"false"===a.getAttribute("data-sortable"))return!1;n.preventDefault(),e.columns().sort(a.dataIndex,p.contains(a,"asc")?"desc":"asc")}}),t.perPageSelect&&h(e.wrapper,"change",function(n){var a=n.target;"SELECT"===a.nodeName&&p.contains(a,t.classes.selector)&&(n.preventDefault(),e.config.perPage=parseInt(a.value,10),e.selectors.length>1&&c([].slice.call(e.selectors),function(e){e.selectedIndex=a.selectedIndex}),e.update())}),t.searchable&&h(e.wrapper,"keyup",function(n){"INPUT"===n.target.nodeName&&p.contains(n.target,t.classes.input)&&(n.preventDefault(),e.search(n.target.value))}),t.sortable&&h(e.wrapper,"mousedown",function(e){"TH"===e.target.nodeName&&e.preventDefault()})},render:function(){if(!this.rendered){var e=this,t=e.config;this.table.hasHeader&&t.fixedColumns&&(this.columnWidths=this.table.header.cells.map(function(e){return e.node.offsetWidth})),e.wrapper=d("div",{class:t.classes.wrapper});var n=["
",t.layout.top,"
","
","
",t.layout.bottom,"
"].join("");if(n=n.replace("{info}","
"),t.perPageSelect){var a=["
","","
"].join(""),s=d("select",{class:t.classes.selector});c(t.perPageSelect,function(e){var n=e===t.perPage,a=new Option(e,e,n,n);s.add(a)}),a=a.replace("{select}",s.outerHTML),n=n.replace(/\{select\}/g,a)}else n=n.replace(/\{select\}/g,"");if(t.searchable){var i=["
","","
"].join("");n=n.replace(/\{search\}/g,i)}else n=n.replace(/\{search\}/g,"");e.table.node.classList.add(t.classes.table),c(n.match(/\{pager\}/g),function(e,a){n=n.replace("{pager}",d("div",{class:t.classes.pagination}).outerHTML)}),e.wrapper.innerHTML=n,e.pagers=[].slice.call(e.wrapper.querySelectorAll("."+t.classes.pagination)),c(e.pagers,function(t,n){e.pagers[n]=new b(e,t)}),e.container=e.wrapper.querySelector("."+t.classes.container),e.labels=e.wrapper.querySelectorAll("."+t.classes.info),e.selectors=e.wrapper.querySelectorAll("."+t.classes.selector),e.table.node.parentNode.replaceChild(e.wrapper,e.table.node),e.container.appendChild(e.table.node),e.rect=e.table.node.getBoundingClientRect(),e.rendered=!0}},update:function(){this.rows().paginate(),this.rows().render(),this.emit("update")},getInfo:function(){var e,t=0,n=0,a=0;if(this.totalPages&&(a=(n=(t=this.currentPage-1)*this.config.perPage)+this.pages[t].length,n+=1,e=this.searching?this.searchData.length:this.table.rows.length),this.labels.length&&this.config.labels.info.length){var s=this.config.labels.info.replace("{start}",n).replace("{end}",a).replace("{page}",this.currentPage).replace("{pages}",this.totalPages).replace("{rows}",e);c([].slice.call(this.labels),function(t){t.innerHTML=e?s:""})}},search:function(e,t){var n=this;if(e=e.toLowerCase(),n.currentPage=1,n.searching=!0,n.searchData=[],!e.length)return n.searching=!1,p.remove(n.wrapper,"search-results"),n.update(),!1;c(n.table.rows,function(a){var s=n.searchData.indexOf(a)>=0;void 0!==t?c(a.cells,function(i){void 0===t||i.index!=t||s||i.content.toLowerCase().indexOf(e)>=0&&n.searchData.push(a)}):e.split(" ").reduce(function(e,t){for(var s=!1,i=0;i=0){(!a.cells[i].hidden||a.cells[i].hidden&&n.config.search.includeHiddenColumns)&&(s=!0);break}return e&&s},!0)&&!s&&n.searchData.push(a)}),p.add(n.wrapper,"search-results"),n.searchData.length?n.update():(p.remove(n.wrapper,"search-results"),n.setMessage(n.config.labels.noRows)),this.emit("search",e,this.searchData)},page:function(e){return e!=this.currentPage&&(isNaN(e)||(this.currentPage=parseInt(e,10)),this.onFirstPage=1===this.currentPage,this.onLastPage=this.currentPage===this.totalPages,!(e>this.totalPages||e<0)&&(this.rows().render(e),void this.emit("page",e)))},import:function(e){var t=this,n=!1,a={lineDelimiter:"\n",columnDelimiter:","};if(!s(e))return!1;if((e=l(a,e)).data.length||s(e.data)){if("csv"===e.type){n={data:[]};var i=e.data.split(e.lineDelimiter);i.length&&(e.headings&&(n.headings=i[0].split(e.columnDelimiter),i.shift()),c(i,function(t,a){n.data[a]=[];var s=t.split(e.columnDelimiter);s.length&&c(s,function(e){n.data[a].push(e)})}))}else if("json"===e.type){var o=r(e.data);o?(n={headings:[],data:[]},c(o,function(e,t){n.data[t]=[],c(e,function(e,a){n.headings.indexOf(a)<0&&n.headings.push(a),n.data[t].push(e)})})):console.warn("That's not valid JSON!")}s(e.data)&&(n=e.data),n&&(c(n.headings,function(e,n){t.table.header.cells[n].setContent(e)}),this.rows().add(n.data))}return!1},setMessage:function(e){var t=1;this.table.rows.length&&(t=this.table.rows[0].cells.length);var n=d("tr",{html:''+e+""});u(this.table.body),this.table.body.appendChild(n)},columns:function(){return this.API.columns},rows:function(){return this.API.rows},on:function(e,t){this.events=this.events||{},this.events[e]=this.events[e]||[],this.events[e].push(t)},off:function(e,t){this.events=this.events||{},e in this.events!=!1&&this.events[e].splice(this.events[e].indexOf(t),1)},emit:function(e){if(this.events=this.events||{},e in this.events!=!1)for(var t=0;t1){var s="pager",i=p("ul"),o=n.onFirstPage?1:n.currentPage-1,r=n.onlastPage?e:n.currentPage+1;a.firstLast&&i.appendChild(t.button(s,1,a.firstText)),a.nextPrev&&i.appendChild(t.button(s,o,a.prevText));var l=t.truncate();u(l,function(e){i.appendChild(e)}),a.nextPrev&&i.appendChild(t.button(s,r,a.nextText)),a.firstLast&&i.appendChild(t.button(s,e,a.lastText)),t.parent.appendChild(i)}},truncate:function(){var e,t=this,n=t.instance.config,a=2*n.pagerDelta,s=t.instance.currentPage,i=s-n.pagerDelta,o=s+n.pagerDelta,r=t.instance.totalPages,l=[],c=[];if(n.truncatePager){s<4-n.pagerDelta+a?o=3+a:s>r-(3-n.pagerDelta+a)&&(i=r-(2+a));for(var d=1;d<=r;d++)(1==d||d==r||d>=i&&d<=o)&&l.push(d);u(l,function(a){e&&(a-e==2?c.push(t.button("",e+1,e+1)):a-e!=1&&c.push(t.button(n.classes.ellipsis,0,n.ellipsisText,!0))),c.push(t.button(a==s?"active":"",a,a)),e=a})}else u(r,function(e){c.push(t.button(e==s?"active":"",e,e))});return c},button:function(e,t,n,a){return p("li",{class:e,html:a?""+n+"":''+n+""})}};var C=function(e){this.instance=e};C.prototype={init:function(){},count:function(){return this.instance.table.rows.length},render:function(e){var t=this.instance;if(e=e||t.currentPage,g(t.table.body),!(e<1||e>t.totalPages)){var a=t.table.header,s=n.createDocumentFragment();t.table.hasHeader&&(g(a.node),u(a.cells,function(e){e.hidden||a.node.appendChild(e.node)})),t.pages.length&&u(t.pages[e-1],function(e){g(e.node),u(e.cells,function(t){t.hidden||e.node.appendChild(t.node)}),s.append(e.node)}),t.table.body.appendChild(s),u(t.pagers,function(e){e.render()}),t.getInfo(),t.emit("rows.render")}},paginate:function(){var e=this.instance.config,t=this.instance.table.rows,n=this.instance;n.searching&&n.searchData&&(t=n.searchData),n.pages=t.map(function(n,a){return a%e.perPage==0?t.slice(a,a+e.perPage):null}).filter(function(e){return e}),n.totalPages=n.pages.length,n.currentPage>n.totalPages&&(n.currentPage=n.totalPages)},add:function(e,t){if(l(e))return t=t||0,l(e[0])?(u(e,function(e){e=this.instance.table.addRow(new w(e,this.instance.columns().count()+1),t)},this),this.instance.table.update()):e=this.instance.table.addRow(new w(e,this.instance.columns().count()+1),t,!0),this.instance.update(),e},remove:function(e){var t=!1,n=this.instance;if(l(e)){for(var a=e.length-1;a>=0;a--)n.table.removeRow(this.get(e[a]));n.table.update(),n.update()}else if(t=this.get(e))return n.table.removeRow(t,!0),n.update(),t},get:function(e){var t=this.instance.table.rows;if(e instanceof w||e instanceof Element){for(var n=0;nn.table.header.cells.length-1)return!1;var a=n.table.header.cells[e].node,s=n.table.rows;n.searching&&n.searchData&&(s=n.searchData),n.lastHeading&&m.remove(n.lastHeading,n.lastDirection),n.lastDirection&&m.remove(a,n.lastDirection),m.add(a,t);var i,o;a.hasAttribute("data-type")&&"date"===a.getAttribute("data-type")&&(i=!1,(o=a.hasAttribute("data-format"))&&(i=a.getAttribute("data-format"))),s.sort(function(n,a){var s=n.cells[e].content,r=a.cells[e].content;return o?(s=v(s,i,n.cells[e]),r=v(r,i,a.cells[e])):(s=s.replace(/(\$|\,|\s|%)/g,""),r=r.replace(/(\$|\,|\s|%)/g,"")),s=isNaN(s)?s:parseInt(s,10),r=isNaN(r)?r:parseInt(r,10),"asc"===t?s>r:s=t.columns().count())throw new Error("Column index "+e[n]+" is outside the range of columns.");var a=function(t){var n=[];u(e,function(e,a){n[a]=t.cells[e],n[a].index=n[a].node.dataIndex=a,t.node.appendChild(n[a].node)}),t.cells=n};t.table.hasHeader&&a(t.table.header),t.table.hasFooter&&a(t.table.footer),u(t.table.rows,function(e){a(e)}),t.update(),t.emit("columns.order",e)}},hide:function(e){var t=this.instance,n=t.table.header,a=t.table.rows;isNaN(e)||(e=[e]),u(e,function(e){u(n.cells,function(t){e==t.index&&(t.hidden=!0)}),u(a,function(t){u(t.cells,function(t){e==t.index&&(t.hidden=!0)})})}),this.fix(!0),t.update(),t.emit("columns.hide",e)},show:function(e){var t=this.instance,n=t.table.header,a=t.table.rows;isNaN(e)||(e=[e]),u(e,function(e){u(n.cells,function(t){e==t.index&&(t.hidden=!1)}),u(a,function(t){u(t.cells,function(t){e==t.index&&(t.hidden=!1)})})}),this.fix(!0),t.update(),t.emit("columns.show",e)},visible:function(e){var t,n=this.instance.table.header;return void 0===e&&(e=n.cells.map(function(e){return e.index})),isNaN(e)?l(e)&&(t=[],u(e,function(e){t.push(!n.cells[e].hidden)})):t=!n.cells[e].hidden,t},add:function(e){var t=this.instance;if(r(e)){if(d(e,"heading")){var n=new b(p("th"),t.columns().count());n.setContent(e.heading),t.table.header.node.appendChild(n.node),t.table.header.cells.push(n)}d(e,"data")&&l(e.data)&&u(t.table.rows,function(t,n){var a=new b(p("td"),t.cells.length);a.setContent(e.data[n]||""),t.node.appendChild(a.node),t.cells.push(a)})}this.fix(!0),t.update(),t.emit("columns.add")},remove:function(e,t){var n=this.instance,a=n.table,s=a.header;if(l(e))return e.sort(function(e,t){return t-e}),void u(e,function(t,n){this.remove(t,n=0&&u(e.columnRenderers,function(a){a.columns.indexOf(n.index)>=0&&n.setContent(a.renderer.call(e,n.content,n,t))})})})),e.rows().render(),e.bindEvents(),e.initialised=!0,setTimeout(function(){e.API.rows.init(),e.API.columns.init(),e.emit("init")},10)}},initExtensions:function(){var e=this;u(["editable","exportable","filterable"],function(t){void 0!==e[t]&&"function"==typeof e[t]&&(e[t]=e[t](e,e.config[t],{each:u,extend:h,isObject:r,classList:m,createElement:p}),e[t].init&&"function"==typeof e[t].init&&e[t].init())})},bindEvents:function(){var e=this,t=e.config;f(e.wrapper,"mousedown",function(e){1===e.which&&t.sortable&&"TH"===e.target.nodeName&&m.add(e.target,"loading")}),f(e.wrapper,"click",function(n){var a=n.target;if(a.hasAttribute("data-page")&&(n.preventDefault(),e.page(parseInt(a.getAttribute("data-page"),10))),t.sortable&&"TH"===a.nodeName&&m.contains(a,t.classes.sorter)){if(a.hasAttribute("data-sortable")&&"false"===a.getAttribute("data-sortable"))return!1;n.preventDefault(),e.columns().sort(a.dataIndex,m.contains(a,"asc")?"desc":"asc")}}),t.perPageSelect&&f(e.wrapper,"change",function(n){var a=n.target;"SELECT"===a.nodeName&&m.contains(a,t.classes.selector)&&(n.preventDefault(),e.config.perPage=parseInt(a.value,10),e.selectors.length>1&&u([].slice.call(e.selectors),function(e){e.selectedIndex=a.selectedIndex}),e.update())}),t.searchable&&f(e.wrapper,"keyup",function(n){"INPUT"===n.target.nodeName&&m.contains(n.target,t.classes.input)&&(n.preventDefault(),e.search(n.target.value))}),t.sortable&&f(e.wrapper,"mousedown",function(e){"TH"===e.target.nodeName&&e.preventDefault()})},render:function(){if(!this.rendered){var e=this,t=e.config;this.table.hasHeader&&t.fixedColumns&&t.header&&(this.columnWidths=this.table.header.cells.map(function(e){return e.node.offsetWidth})),e.wrapper=p("div",{class:t.classes.wrapper});var n=["
",t.layout.top,"
","
","
",t.layout.bottom,"
"].join("");if(n=n.replace("{info}","
"),t.perPageSelect){var a=["
","","
"].join(""),s=p("select",{class:t.classes.selector});u(t.perPageSelect,function(e){var n=e===t.perPage,a=new Option(e,e,n,n);s.add(a)}),a=a.replace("{select}",s.outerHTML),n=n.replace(/\{select\}/g,a)}else n=n.replace(/\{select\}/g,"");if(t.searchable){var i=["
","","
"].join("");n=n.replace(/\{search\}/g,i)}else n=n.replace(/\{search\}/g,"");e.table.node.classList.add(t.classes.table),u(n.match(/\{pager\}/g),function(e,a){n=n.replace("{pager}",p("div",{class:t.classes.pagination}).outerHTML)}),e.wrapper.innerHTML=n,e.pagers=[].slice.call(e.wrapper.querySelectorAll("."+t.classes.pagination)),u(e.pagers,function(t,n){e.pagers[n]=new y(e,t)}),e.container=e.wrapper.querySelector("."+t.classes.container),e.labels=e.wrapper.querySelectorAll("."+t.classes.info),e.selectors=e.wrapper.querySelectorAll("."+t.classes.selector),e.table.node.parentNode.replaceChild(e.wrapper,e.table.node),e.container.appendChild(e.table.node),e.rect=e.table.node.getBoundingClientRect(),e.rendered=!0}},update:function(){this.rows().paginate(),this.rows().render(),this.emit("update")},getInfo:function(){var e,t=0,n=0,a=0;if(this.totalPages&&(a=(n=(t=this.currentPage-1)*this.config.perPage)+this.pages[t].length,n+=1,e=this.searching?this.searchData.length:this.rows().count()),this.labels.length&&this.config.labels.info.length){var s=this.config.labels.info.replace("{start}",n).replace("{end}",a).replace("{page}",this.currentPage).replace("{pages}",this.totalPages).replace("{rows}",e);u([].slice.call(this.labels),function(t){t.innerHTML=e?s:""})}},search:function(e,t){var n=this;if(e=e.toLowerCase(),n.currentPage=1,n.searching=!0,n.searchData=[],!e.length)return n.searching=!1,m.remove(n.wrapper,"search-results"),n.update(),!1;u(n.table.rows,function(a){var s=n.searchData.indexOf(a)>=0;void 0!==t?u(a.cells,function(i){void 0===t||i.index!=t||s||i.content.toLowerCase().indexOf(e)>=0&&n.searchData.push(a)}):e.split(" ").reduce(function(e,t){for(var s=!1,i=0;i=0){(!a.cells[i].hidden||a.cells[i].hidden&&n.config.search.includeHiddenColumns)&&(s=!0);break}return e&&s},!0)&&!s&&n.searchData.push(a)}),m.add(n.wrapper,"search-results"),n.searchData.length?n.update():(m.remove(n.wrapper,"search-results"),n.setMessage(n.config.labels.noRows)),this.emit("search",e,this.searchData)},page:function(e){return e!=this.currentPage&&(isNaN(e)||(this.currentPage=parseInt(e,10)),this.onFirstPage=1===this.currentPage,this.onLastPage=this.currentPage===this.totalPages,!(e>this.totalPages||e<0)&&(this.rows().render(e),void this.emit("page",e)))},import:function(e){var t=this,n=!1,a={lineDelimiter:"\n",columnDelimiter:","};if(!r(e))return!1;if((e=h(a,e)).data.length||r(e.data)){if("csv"===e.type){n={data:[]};var s=e.data.split(e.lineDelimiter);s.length&&(e.headings&&(n.headings=s[0].split(e.columnDelimiter),s.shift()),u(s,function(t,a){n.data[a]=[];var s=t.split(e.columnDelimiter);s.length&&u(s,function(e){n.data[a].push(e)})}))}else if("json"===e.type){var i=c(e.data);if(!i)throw new Error("That's not valid JSON!");n={headings:[],data:[]},u(i,function(e,t){n.data[t]=[],u(e,function(e,a){n.headings.indexOf(a)<0&&n.headings.push(a),n.data[t].push(e)})})}r(e.data)&&(n=e.data),n&&(u(n.headings,function(e,n){t.table.header.cells[n].setContent(e)}),this.rows().add(n.data))}return!1},setMessage:function(e){var t=1;this.rows().count()&&(t=this.columns().count());var n=p("tr",{html:''+e+""});g(this.table.body),this.table.body.appendChild(n)},columns:function(){return this.API.columns},rows:function(){return this.API.rows},on:function(e,t){this.events=this.events||{},this.events[e]=this.events[e]||[],this.events[e].push(t)},off:function(e,t){this.events=this.events||{},e in this.events!=!1&&this.events[e].splice(this.events[e].indexOf(t),1)},emit:function(e){if(this.events=this.events||{},e in this.events!=!1)for(var t=0;t 1) { var c = "pager", ul = createElement("ul"), @@ -603,6 +616,12 @@ }; Rows.prototype = { + init: function() {}, + + count: function() { + return this.instance.table.rows.length; + }, + render: function(page) { var that = this, dt = that.instance; @@ -612,9 +631,8 @@ if (page < 1 || page > dt.totalPages) return; - var that = this, - head = dt.table.header, - fragment = document.createDocumentFragment(); + var head = dt.table.header, + fragment = doc.createDocumentFragment(); if (dt.table.hasHeader) { empty(head.node); @@ -680,13 +698,13 @@ at = at || 0; if (isArray(row[0])) { each(row, function(tr) { - tr = this.instance.table.addRow(new Row(tr, this.instance.table.rows.length + 1), at); + tr = this.instance.table.addRow(new Row(tr, this.instance.columns().count() + 1), at); }, this); // only update after adding multiple rows // to keep performance hit to a minimum this.instance.table.update(); } else { - row = this.instance.table.addRow(new Row(row, this.instance.table.rows.length + 1), at, true); + row = this.instance.table.addRow(new Row(row, this.instance.columns().count() + 1), at, true); } this.instance.update(); @@ -707,7 +725,8 @@ dt.table.update(); dt.update(); } else { - if (row = this.get(obj)) { + row = this.get(obj); + if (row) { dt.table.removeRow(row, true); dt.update(); @@ -739,6 +758,12 @@ }; Columns.prototype = { + init: function() {}, + + count: function() { + return this.instance.table.header.cells.length; + }, + sort: function(column, direction) { var dt = this.instance; @@ -816,44 +841,39 @@ }, order: function(order) { - var dt = this.instance, - head = dt.table.header, - rows = dt.table.rows, - arr; + var dt = this.instance; if (isArray(order)) { // Check for erroneous indexes for (var n = 0; n < order.length; n++) { - if (order[n] >= head.cells.length) { + if (order[n] >= dt.columns().count()) { throw new Error("Column index " + order[n] + " is outside the range of columns."); } } - // Reorder the header - if (dt.table.hasHeader) { - arr = []; + var reorder = function(node) { + var arr = []; each(order, function(column, i) { - arr[i] = head.cells[column]; - + arr[i] = node.cells[column]; arr[i].index = arr[i].node.dataIndex = i; - - // rearrange the tr node cells for rendering - head.node.appendChild(arr[i].node); + node.node.appendChild(arr[i].node); }); - head.cells = arr; - } + node.cells = arr; + }; - // Reorder the body - each(rows, function(row) { - arr = []; - each(order, function(column, i) { - arr[i] = row.cells[column]; + // Reorder the header + if (dt.table.hasHeader) { + reorder(dt.table.header); + } - arr[i].index = arr[i].node.dataIndex = i; + // Reorder the footer + if (dt.table.hasFooter) { + reorder(dt.table.footer); + } - row.node.appendChild(arr[i].node); - }); - row.cells = arr; + // Reorder the rows + each(dt.table.rows, function(row) { + reorder(row); }); dt.update(); @@ -863,8 +883,7 @@ }, hide: function(columns) { - var that = this, - dt = this.instance, + var dt = this.instance, head = dt.table.header, rows = dt.table.rows; @@ -872,21 +891,21 @@ columns = [columns]; } - for (var n = 0; n < columns.length; n++) { + each(columns, function(column) { each(head.cells, function(cell) { - if (columns[n] == cell.index) { + if (column == cell.index) { cell.hidden = true; } }); each(rows, function(row) { each(row.cells, function(cell) { - if (columns[n] == cell.index) { + if (column == cell.index) { cell.hidden = true; } }); }); - } + }); this.fix(true); dt.update(); @@ -895,8 +914,7 @@ }, show: function(columns) { - var that = this, - dt = this.instance, + var dt = this.instance, head = dt.table.header, rows = dt.table.rows; @@ -904,21 +922,21 @@ columns = [columns]; } - for (var n = 0; n < columns.length; n++) { + each(columns, function(column) { each(head.cells, function(cell) { - if (columns[n] == cell.index) { + if (column == cell.index) { cell.hidden = false; } }); each(rows, function(row) { each(row.cells, function(cell) { - if (columns[n] == cell.index) { + if (column == cell.index) { cell.hidden = false; } }); }); - } + }); this.fix(true); dt.update(); @@ -927,8 +945,7 @@ }, visible: function(columns) { - var that = this, - dt = this.instance, + var dt = this.instance, head = dt.table.header, cols; @@ -955,7 +972,7 @@ if (isObject(obj)) { if (isset(obj, "heading")) { - var cell = new Cell(createElement("th"), dt.table.header.cells.length); + var cell = new Cell(createElement("th"), dt.columns().count()); cell.setContent(obj.heading); dt.table.header.node.appendChild(cell.node); @@ -1035,14 +1052,9 @@ } } - each( - dt.columnWidths, - function(size, cell) { - var w = size / dt.rect.width * 100; - head.cells[cell].node.style.width = w + "%"; - }, - this - ); + each(dt.columnWidths, function(size, cell) { + head.cells[cell].node.style.width = (size / dt.rect.width * 100) + "%"; + }); } }; @@ -1050,6 +1062,11 @@ var DataTable = function(table, config) { this.config = extend(defaultConfig, config); + this.API = { + rows: new Rows(this), + columns: new Columns(this) + }; + if (this.config.ajax) { var that = this, ajax = this.config.ajax; @@ -1097,18 +1114,10 @@ var that = this, o = that.config; - // IE detection - that.isIE = !!/(msie|trident)/i.test(navigator.userAgent); - that.currentPage = 1; that.onFirstPage = true; that.onLastPage = false; - that.API = { - rows: new Rows(that), - columns: new Columns(that) - }; - that.rows().paginate(); that.totalPages = that.pages.length; @@ -1213,6 +1222,8 @@ that.initialised = true; setTimeout(function() { + that.API.rows.init(); + that.API.columns.init(); that.emit("init"); }, 10); }, @@ -1261,7 +1272,7 @@ that.page(parseInt(node.getAttribute("data-page"), 10)); } - if (o.sortable && node.nodeName === "TH") { + if (o.sortable && node.nodeName === "TH" && classList.contains(node, o.classes.sorter)) { if (node.hasAttribute("data-sortable") && node.getAttribute("data-sortable") === "false") return false; e.preventDefault(); @@ -1320,7 +1331,7 @@ var that = this, o = that.config; - if (this.table.hasHeader && o.fixedColumns) { + if (this.table.hasHeader && o.fixedColumns && o.header) { this.columnWidths = this.table.header.cells.map(function(cell) { return cell.node.offsetWidth; }); @@ -1414,7 +1425,7 @@ that.labels = that.wrapper.querySelectorAll("." + o.classes.info); - that.selectors = that.wrapper.querySelectorAll("." + o.classes.selector) + that.selectors = that.wrapper.querySelectorAll("." + o.classes.selector); // Insert in to DOM tree that.table.node.parentNode.replaceChild(that.wrapper, that.table.node); @@ -1445,7 +1456,7 @@ f = current * this.config.perPage; t = f + this.pages[current].length; f = f + 1; - items = !!this.searching ? this.searchData.length : this.table.rows.length; + items = !!this.searching ? this.searchData.length : this.rows().count(); } if (this.labels.length && this.config.labels.info.length) { @@ -1619,7 +1630,7 @@ }); }); } else { - console.warn("That's not valid JSON!"); + throw new Error("That's not valid JSON!"); } } @@ -1642,8 +1653,8 @@ setMessage: function(message) { var colspan = 1; - if (this.table.rows.length) { - colspan = this.table.rows[0].cells.length; + if (this.rows().count()) { + colspan = this.columns().count(); } var node = createElement("tr", {