From 5741843fbc93cfc9a0b7b9ce4010e97b36944f5f Mon Sep 17 00:00:00 2001 From: Mobius1 Date: Tue, 24 Oct 2017 12:30:57 +0100 Subject: [PATCH] v2.0.0-alpha.23 --- README.md | 2 +- dist/vanilla-dataTables.min.css | 2 +- dist/vanilla-dataTables.min.js | 4 +- src/vanilla-dataTables.css | 2 +- src/vanilla-dataTables.js | 423 +++++++++++++++++++++----------- 5 files changed, 289 insertions(+), 144 deletions(-) diff --git a/README.md b/README.md index 3397cda..347e989 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-beta.1`. Not production ready. +This is version 2.0 of Vanilla-DataTables. Current version is `v2.0.0-alpha.23`. Not production ready. ## Changes diff --git a/dist/vanilla-dataTables.min.css b/dist/vanilla-dataTables.min.css index c61e8a6..a9c82aa 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-beta.1 + * Version: v2.0.0-alpha.23 * */.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 45d21c0..a53b6c1 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-beta.1 + * Version: v2.0.0-alpha.23 * */ -!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,s="classList"in a,i=!!/(msie|trident)/i.test(navigator.userAgent),r={perPage:10,perPageSelect:[5,10,15,20,25],sortable:!0,searchable:!0,nextPrev:!0,firstLast:!1,prevText:"‹",nextText:"›",firstText:"«",lastText:"»",ellipsisText:"…",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}"}},o=["editable","exportable","filterable"],l=function(e){return"[object Object]"===Object.prototype.toString.call(e)},c=function(e){return Array.isArray(e)},d=function(e){var t=!1;try{t=JSON.parse(e)}catch(e){return!1}return!(null===t||!c(t)&&!l(t))&&t},h=function(e,t){return e.hasOwnProperty(t)},u=function(e,t){for(var n in t)if(t.hasOwnProperty(n)){var a=t[n];a&&l(a)?(e[n]=e[n]||{},u(e[n],a)):e[n]=a}return e},p=function(e,t,n){var a;if(l(e))for(a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.call(n,e[a],a);else if(c(e))for(a=0;a1){var s="pager",i=f("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();p(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=[],c=[];if(n.truncatePager){s<4-n.pagerDelta+a?r=3+a:s>o-(3-n.pagerDelta+a)&&(i=o-(2+a));for(var d=1;d<=o;d++)(1==d||d==o||d>=i&&d<=r)&&l.push(d);p(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 p(o,function(e){c.push(t.button(e==s?"active":"",e,e))});return c},button:function(e,t,n,a){return f("li",{class:e,html:a?""+n+"":''+n+""})}};var D=function(e){this.instance=e};D.prototype={init:function(){},count:function(){return this.instance.table.rows.length},render:function(e){var t=this.instance;if(e=e||t.currentPage,v(t.table.body),!(e<1||e>t.totalPages)){var a=t.table.header,s=n.createDocumentFragment();t.table.hasHeader&&(v(a.node),p(a.cells,function(e){e.hidden||a.node.appendChild(e.node)})),t.pages.length&&p(t.pages[e-1],function(e){v(e.node),p(e.cells,function(t){t.hidden||e.node.appendChild(t.node)}),s.append(e.node)}),t.table.body.appendChild(s),p(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(c(e))return t=t||0,c(e[0])?(p(e,function(e){e=this.instance.table.addRow(new y(e,this.instance.columns().count()+1),t)},this),this.instance.table.update()):e=this.instance.table.addRow(new y(e,this.instance.columns().count()+1),t,!0),this.instance.update(),e},remove:function(e){var t=!1,n=this.instance;if(c(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 y||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,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=w(s,i,n.cells[e]),o=w(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=t.columns().count())throw new Error("Column index "+e[n]+" is outside the range of columns.");var a=function(t){var n=[];p(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),p(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]),p(e,function(e){p(n.cells,function(t){e==t.index&&(t.hidden=!0)}),p(a,function(t){p(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]),p(e,function(e){p(n.cells,function(t){e==t.index&&(t.hidden=!1)}),p(a,function(t){p(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)?c(e)&&(t=[],p(e,function(e){t.push(!n.cells[e].hidden)})):t=!n.cells[e].hidden,t},add:function(e){var t=this.instance;if(l(e)){if(h(e,"heading")){var n=new x(f("th"),t.columns().count());n.setContent(e.heading),t.table.header.node.appendChild(n.node),t.table.header.cells.push(n)}h(e,"data")&&c(e.data)&&p(t.table.rows,function(t,n){var a=new x(f("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(c(e))return e.sort(function(e,t){return t-e}),void p(e,function(t,n){this.remove(t,n=0&&p(a,function(n){n.columns.indexOf(s.index)>=0&&s.setContent(n.renderer.call(e,s.content,s,t))})})})}e.rows().render(),e.bindEvents(),e.initialised=!0,setTimeout(function(){e.API.rows.init(),e.API.columns.init(),e.emit("init")},10)}},extend:function(){var e=this;p(o,function(t){void 0!==e[t]&&"function"==typeof e[t]&&(e[t]=e[t](e,e.config[t],b),e[t].init&&"function"==typeof e[t].init&&e[t].init())})},bindEvents:function(){var e=this,t=e.config;g(e.wrapper,"mousedown",function(e){1===e.which&&t.sortable&&"TH"===e.target.nodeName&&m.add(e.target,"loading")}),g(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&&g(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&&p([].slice.call(e.selectors),function(e){e.selectedIndex=a.selectedIndex}),e.update())}),t.searchable&&g(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&&g(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=f("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=f("select",{class:t.classes.selector});p(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),p(n.match(/\{pager\}/g),function(e,a){n=n.replace("{pager}",f("div",{class:t.classes.pagination}).outerHTML)}),e.wrapper.innerHTML=n,e.pagers=[].slice.call(e.wrapper.querySelectorAll("."+t.classes.pagination)),p(e.pagers,function(t,n){e.pagers[n]=new P(e,t)}),e.container=e.wrapper.querySelector("."+t.classes.container),e.labels=e.wrapper.querySelectorAll("."+t.classes.info),e.inputs=e.wrapper.querySelectorAll("."+t.classes.input),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);p([].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;p(n.table.rows,function(a){var s=n.searchData.indexOf(a)>=0;void 0!==t?p(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)))},reset:function(){this.searching&&(this.searching=this.searchData=!1,m.remove(this.wrapper,"search-results")),p([].slice.call(this.inputs),function(e){e.value=null,e.blur()}),this.update(),this.emit("reset")},import:function(e){var t=this,n=!1,a={lineDelimiter:"\n",columnDelimiter:","};if(!l(e))return!1;if((e=u(a,e)).data.length||l(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()),p(s,function(t,a){n.data[a]=[];var s=t.split(e.columnDelimiter);s.length&&p(s,function(e){n.data[a].push(e)})}))}else if("json"===e.type){var i=d(e.data);if(!i)throw new Error("That's not valid JSON!");n={headings:[],data:[]},p(i,function(e,t){n.data[t]=[],p(e,function(e,a){n.headings.indexOf(a)<0&&n.headings.push(a),n.data[t].push(e)})})}l(e.data)&&(n=e.data),n&&(p(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=f("tr",{html:''+e+""});v(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 a="pager",i=p("ul"),r=n.onFirstPage?1:n.currentPage-1,o=n.onlastPage?e:n.currentPage+1;s.firstLast&&i.appendChild(t.button(a,1,s.firstText)),s.nextPrev&&i.appendChild(t.button(a,r,s.prevText));var l=t.truncate();f(l,function(e){i.appendChild(e)}),s.nextPrev&&i.appendChild(t.button(a,o,s.nextText)),s.firstLast&&i.appendChild(t.button(a,e,s.lastText)),t.parent.appendChild(i)}},truncate:function(){var e,t=this,n=t.instance.config,s=2*n.pagerDelta,a=t.instance.currentPage,i=a-n.pagerDelta,r=a+n.pagerDelta,o=t.instance.totalPages,l=[],c=[];if(n.truncatePager){a<4-n.pagerDelta+s?r=3+s:a>o-(3-n.pagerDelta+s)&&(i=o-(2+s));for(var d=1;d<=o;d++)(1==d||d==o||d>=i&&d<=r)&&l.push(d);f(l,function(s){e&&(s-e==2?c.push(t.button("",e+1,e+1)):s-e!=1&&c.push(t.button(n.classes.ellipsis,0,n.ellipsisText,!0))),c.push(t.button(s==a?"active":"",s,s)),e=s})}else f(o,function(e){c.push(t.button(e==a?"active":"",e,e))});return c},button:function(e,t,n,s){return p("li",{class:e,html:s?""+n+"":''+n+""})}};var D=function(e,t){this.instance=e,void 0!==t?isNaN(t)?c(t)&&(this.select=t):this.select=[t]:this.select=e.table.rows.map(function(e){return e.index})};D.prototype={init:function(){},count:function(){return this.instance.table.rows.length},render:function(e){var t=this.instance;if(e=e||t.currentPage,b(t.table.body),!(e<1||e>t.totalPages)){var s=t.table.header,a=n.createDocumentFragment();t.table.hasHeader&&(b(s.node),f(s.cells,function(e){e.hidden||s.node.appendChild(e.node)})),t.pages.length&&f(t.pages[e-1],function(e){b(e.node),f(e.cells,function(t){t.hidden||e.node.appendChild(t.node)}),a.append(e.node)}),t.table.body.appendChild(a),f(t.pagers,function(e){e.render()}),t.getInfo(),1==t.currentPage&&t.fixHeight(),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,s){return s%e.perPage==0?t.slice(s,s+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(c(e))return t=t||0,c(e[0])?(f(e,function(e){e=this.instance.table.addRow(new y(e,this.instance.columns().count()+1),t)},this),this.instance.table.update()):e=this.instance.table.addRow(new y(e,this.instance.columns().count()+1),t,!0),this.instance.update(),e},remove:function(e){var t=!1,n=this.instance;if(c(e)){for(var s=e.length-1;s>=0;s--)n.table.removeRow(this.get(e[s]));n.table.update(),n.update()}else if(t=this.get(e))return n.table.removeRow(t,!0),n.update(),t},cells:function(){var e=this,t=[];return 1==this.select.length&&(this.select=this.select[0]),f(this.instance.table.rows,function(n){(c(e.select)&&e.select.indexOf(n.index)>=0||e.select==n.index)&&t.push(n.cells)}),t},get:function(e){var t=this.instance.table.rows;if(e instanceof y||e instanceof Element){for(var n=0;n=0&&f(s,function(n){n.columns.indexOf(a.index)>=0&&a.setContent(n.renderer.call(e,a.content,a,t))})})})}t.fixedColumns&&e.table.hasHeader&&this.fix(!0)},count:function(){return this.instance.table.header.cells.length},sort:function(e,t){var n=this.instance;if(e=e||0,t=t||(t=n.lastDirection&&"asc"===n.lastDirection?"desc":"asc"),e<0||e>n.table.header.cells.length-1)return!1;var s=n.table.header.cells[e].node,a=n.table.rows;n.searching&&n.searchData&&(a=n.searchData),n.lastHeading&&v.remove(n.lastHeading,n.lastDirection),n.lastDirection&&v.remove(s,n.lastDirection),v.add(s,t);var i,r;s.hasAttribute("data-type")&&"date"===s.getAttribute("data-type")&&(i=!1,(r=s.hasAttribute("data-format"))&&(i=s.getAttribute("data-format"))),a.sort(function(n,s){var a=n.cells[e].content,o=s.cells[e].content;return r?(a=w(a,i,n.cells[e]),o=w(o,i,s.cells[e])):(a=a.replace(/(\$|\,|\s|%)/g,""),o=o.replace(/(\$|\,|\s|%)/g,"")),a=isNaN(a)?a:parseInt(a,10),o=isNaN(o)?o:parseInt(o,10),"asc"===t?a>o:a=t.columns().count())throw new Error("Column index "+e[n]+" is outside the range of columns.");var s=function(t){var n=[];f(e,function(e,s){n[s]=t.cells[e],n[s].index=n[s].node.dataIndex=s,t.node.appendChild(n[s].node)}),t.cells=n};t.table.hasHeader&&s(t.table.header),t.table.hasFooter&&s(t.table.footer),f(t.table.rows,function(e){s(e)}),t.update(),t.emit("columns.order",e)}},hide:function(){var e=this.instance,t=e.table.header,n=e.table.rows;f(this.select,function(e){f(t.cells,function(t){e==t.index&&(t.hidden=!0)}),f(n,function(t){f(t.cells,function(t){e==t.index&&(t.hidden=!0)})})}),this.fix(!0),e.update(),e.emit("columns.hide",this.select)},show:function(){var e=this.instance,t=e.table.header,n=e.table.rows;f(this.select,function(e){f(t.cells,function(t){e==t.index&&(t.hidden=!1)}),f(n,function(t){f(t.cells,function(t){e==t.index&&(t.hidden=!1)})})}),this.fix(!0),e.update(),e.emit("columns.show",this.select)},visible:function(){var e=this.instance.table.header,t=[];return f(this.select,function(n){t.push(!e.cells[n].hidden)}),t},add:function(e){var t=this.instance;if(l(e)){if(h(e,"heading")){var n=new x(p("th"),t.columns().count());n.setContent(e.heading),t.table.header.node.appendChild(n.node),t.table.header.cells.push(n)}h(e,"data")&&c(e.data)&&f(t.table.rows,function(t,n){var s=new x(p("td"),t.cells.length);s.setContent(e.data[n]||""),t.node.appendChild(s.node),t.cells.push(s)})}this.fix(!0),t.update(),t.emit("columns.add")},remove:function(e,t){var n=this.instance,s=n.table,a=s.header;if(c(e))return e.sort(function(e,t){return t-e}),void f(e,function(t,n){this.remove(t,n=0?t[s].push(n):e.select==n.index&&t.push(n)})}),t}};var N=function(e,t){if(this.config=u(r,t),this.node=e,this.config.ajax){var n=this,s=this.config.ajax;this.request=new XMLHttpRequest,g(this.request,"load",function(t){if(4===n.request.readyState&&200===n.request.status){var a={};a.data=s.load?s.load.call(n,n.request):n.request.responseText,a.type="json",s.content&&s.content.type&&(a.type=s.content.type,a=u(a,s.content)),n.table=new C(e,a.data,n),n.init()}}),this.request.open("GET","string"==typeof s?n.config.ajax:n.config.ajax.url),this.request.send()}else this.config.data?this.table=new C(e,this.config.data,this):this.table=new C(e,!1,this),this.init()};return N.prototype={init:function(){if(!this.initialised){var e=this,t=e.config;e.sortable=t.sortable,e.searchable=t.searchable,e.currentPage=1,e.onFirstPage=!0,e.onLastPage=!1,e.rows().paginate(),e.totalPages=e.pages.length,e.render(),t.fixedColumns&&e.table.hasHeader&&e.columns().fix(),e.extend(),t.plugins&&f(t.plugins,function(t,n){void 0!==e[n]&&"function"==typeof e[n]&&(e[n]=e[n](e,t,m),t.enabled&&e[n].init&&"function"==typeof e[n].init&&e[n].init())}),e.columns().init(),e.rows().render(),e.bindEvents(),e.setClasses(),e.initialised=!0,setTimeout(function(){e.emit("init")},10)}},setClasses:function(){v.toggle(this.wrapper,"dt-sortable",this.sortable),v.toggle(this.wrapper,"dt-searchable",this.searchable)},extend:function(){var e=this;f(o,function(t){void 0!==e[t]&&"function"==typeof e[t]&&(e[t]=e[t](e,e.config[t],m),e[t].init&&"function"==typeof e[t].init&&e[t].init())})},bindEvents:function(){var e=this,t=e.config;g(e.wrapper,"mousedown",function(t){1===t.which&&e.sortable&&"TH"===t.target.nodeName&&v.add(t.target,"loading")}),g(e.wrapper,"click",function(n){var s=n.target;if(s.hasAttribute("data-page")&&(n.preventDefault(),e.page(parseInt(s.getAttribute("data-page"),10))),e.sortable&&"TH"===s.nodeName&&v.contains(s,t.classes.sorter)){if(s.hasAttribute("data-sortable")&&"false"===s.getAttribute("data-sortable"))return!1;n.preventDefault(),e.columns().sort(s.dataIndex,v.contains(s,"asc")?"desc":"asc")}}),t.perPageSelect&&g(e.wrapper,"change",function(n){var s=n.target;"SELECT"===s.nodeName&&v.contains(s,t.classes.selector)&&(n.preventDefault(),e.setPerPage(s.value))}),e.searchable&&g(e.wrapper,"keyup",function(n){"INPUT"===n.target.nodeName&&v.contains(n.target,t.classes.input)&&(n.preventDefault(),e.search(n.target.value))}),e.sortable&&g(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 s=["
","","
"].join(""),a=p("select",{class:t.classes.selector});f(t.perPageSelect,function(e){var n=e===t.perPage,s=new Option(e,e,n,n);a.add(s)}),s=s.replace("{select}",a.outerHTML),n=n.replace(/\{select\}/g,s)}else n=n.replace(/\{select\}/g,"");if(e.searchable){var i=["
","","
"].join("");n=n.replace(/\{search\}/g,i)}else n=n.replace(/\{search\}/g,"");e.table.node.classList.add(t.classes.table),f(n.match(/\{pager\}/g),function(e,s){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)),f(e.pagers,function(t,n){e.pagers[n]=new P(e,t)}),e.container=e.wrapper.querySelector("."+t.classes.container),e.labels=e.wrapper.querySelectorAll("."+t.classes.info),e.inputs=e.wrapper.querySelectorAll("."+t.classes.input),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")},fixHeight:function(){this.container.style.height=null,this.config.fixedHeight&&(this.rect=this.container.getBoundingClientRect(),this.container.style.height=this.rect.height+"px")},getInfo:function(e){var t,n=0,s=0,a=0;if(e?(a=(s=(n=e.page-1)*e.perpage)+e.data.length,s+=1,t=e.rows):this.totalPages&&(a=(s=(n=this.currentPage-1)*this.config.perPage)+this.pages[n].length,s+=1,t=this.searching?this.searchData.length:this.rows().count()),this.labels.length&&this.config.labels.info.length){var i=this.config.labels.info.replace("{start}",s).replace("{end}",a).replace("{page}",this.currentPage).replace("{pages}",this.totalPages).replace("{rows}",t);f([].slice.call(this.labels),function(e){e.innerHTML=t?i:""})}},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,v.remove(n.wrapper,"search-results"),n.update(),!1;f(n.table.rows,function(s){var a=n.searchData.indexOf(s)>=0;void 0!==t?f(s.cells,function(i){void 0===t||i.index!=t||a||i.content.toLowerCase().indexOf(e)>=0&&n.searchData.push(s)}):e.split(" ").reduce(function(e,t){for(var a=!1,i=0;i=0){(!s.cells[i].hidden||s.cells[i].hidden&&n.config.search.includeHiddenColumns)&&(a=!0);break}return e&&a},!0)&&!a&&n.searchData.push(s)}),v.add(n.wrapper,"search-results"),n.searchData.length?n.update():(v.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(parseInt(e,10)),void this.emit("page",e)))},reset:function(){this.searching&&(this.searching=this.searchData=!1,v.remove(this.wrapper,"search-results")),f([].slice.call(this.inputs),function(e){e.value=null,e.blur()}),this.update(),this.emit("reset")},set:function(e,t){this.hasOwnProperty(e)&&(this[e]=t,v.toggle(this.wrapper,"dt-"+e,this[e]),this.update())},setPerPage:function(e){isNaN(e)||(e=parseInt(e,10),this.config.perPage=e,this.fixHeight(),this.config.perPageSelect.indexOf(e)>=0&&f([].slice.call(this.selectors),function(t){t.value=e}),this.update(),this.emit("perpage",e))},import:function(e){var t=this,n=!1,s={lineDelimiter:"\n",columnDelimiter:","};if(!l(e))return!1;if((e=u(s,e)).data.length||l(e.data)){if("csv"===e.type){n={data:[]};var a=e.data.split(e.lineDelimiter);a.length&&(e.headings&&(n.headings=a[0].split(e.columnDelimiter),a.shift()),f(a,function(t,s){n.data[s]=[];var a=t.split(e.columnDelimiter);a.length&&f(a,function(e){n.data[s].push(e)})}))}else if("json"===e.type){var i=d(e.data);if(!i)throw new Error("That's not valid JSON!");n={headings:[],data:[]},f(i,function(e,t){n.data[t]=[],f(e,function(e,s){n.headings.indexOf(s)<0&&n.headings.push(s),n.data[t].push(e)})})}l(e.data)&&(n=e.data),n&&(n.headings&&(this.table.hasHeader||this.table.addHeader(n.headings,!0),f(n.headings,function(e,n){t.table.header.cells[n].setContent(e)}),this.columns().init()),this.table.rows.length||(this.currentPage=1),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+""});b(this.table.body),this.table.body.appendChild(n)},columns:function(e){return new H(this,e)},rows:function(e){return new D(this,e)},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= 0) || that.select == row.index) { + rows.push(row.cells); + } + }); + + return rows; + }, + get: function(row) { var rows = this.instance.table.rows; if (row instanceof Row || row instanceof Element) { @@ -775,12 +817,110 @@ }; // COLUMNS - var Columns = function(instance) { + var Columns = function(instance, select) { this.instance = instance; + + if (select !== undefined) { + if (!isNaN(select)) { + this.select = [select]; + } else if (isArray(select)) { + this.select = select; + } + } else { + if (instance.table.hasheader) { + this.select = instance.table.header.cells.map(function(cell) { + return cell.index; + }); + } + } }; Columns.prototype = { - init: function() {}, + init: function() { + var that = this.instance, + o = that.config; + + if (that.table.hasHeader) { + each(that.table.header.cells, function(cell) { + classList.add(cell.node, o.classes.sorter); + }); + } + + if (o.columns) { + var selectedColumns = []; + var columnRenderers = []; + + each(o.columns, function(data) { + // convert single column selection to array + if (!isNaN(data.select)) { + data.select = [data.select]; + } + + if (isset(data, "render") && typeof data.render === "function") { + selectedColumns = selectedColumns.concat(data.select); + + columnRenderers.push({ + columns: data.select, + renderer: data.render + }); + } + + // Add the data attributes to the th elements + if (that.table.hasHeader) { + + each(that.table.header.cells, function(cell) { + classList.add(cell.node, o.classes.sorter); + }); + + each(data.select, function(column) { + var cell = that.table.header.cells[column]; + + if (data.type) { + cell.node.setAttribute("data-type", data.type); + } + if (data.format) { + cell.node.setAttribute("data-format", data.format); + } + if (isset(data, "sortable")) { + cell.node.setAttribute("data-sortable", data.sortable); + + if (data.sortable === false) { + classList.remove(cell.node, o.classes.sorter); + } + } + + if (isset(data, "hidden")) { + if (data.hidden !== false) { + that.columns().hide(column); + } + } + + if (isset(data, "sort") && data.select.length === 1) { + that.columns().sort(data.select[0], data.sort); + } + }); + } + }); + + if (selectedColumns.length) { + each(that.table.rows, function(row) { + each(row.cells, function(cell) { + if (selectedColumns.indexOf(cell.index) >= 0) { + each(columnRenderers, function(obj) { + if (obj.columns.indexOf(cell.index) >= 0) { + cell.setContent(obj.renderer.call(that, cell.content, cell, row)); + } + }) + } + }); + }); + } + } + + if (o.fixedColumns && that.table.hasHeader) { + this.fix(true); + } + }, count: function() { return this.instance.table.header.cells.length; @@ -858,7 +998,7 @@ classList.remove(node, "loading"); }, - filter: function(column, query) { + search: function(column, query) { this.instance.search(query, column); }, @@ -904,16 +1044,12 @@ } }, - hide: function(columns) { + hide: function() { var dt = this.instance, head = dt.table.header, rows = dt.table.rows; - if (!isNaN(columns)) { - columns = [columns]; - } - - each(columns, function(column) { + each(this.select, function(column) { each(head.cells, function(cell) { if (column == cell.index) { cell.hidden = true; @@ -932,19 +1068,15 @@ this.fix(true); dt.update(); - dt.emit("columns.hide", columns); + dt.emit("columns.hide", this.select); }, - show: function(columns) { + show: function() { var dt = this.instance, head = dt.table.header, rows = dt.table.rows; - if (!isNaN(columns)) { - columns = [columns]; - } - - each(columns, function(column) { + each(this.select, function(column) { each(head.cells, function(cell) { if (column == cell.index) { cell.hidden = false; @@ -963,28 +1095,17 @@ this.fix(true); dt.update(); - dt.emit("columns.show", columns); + dt.emit("columns.show", this.select); }, - visible: function(columns) { + visible: function() { var dt = this.instance, head = dt.table.header, - cols; - - if (columns === undefined) { - columns = head.cells.map(function(cell) { - return cell.index; - }); - } - - if (!isNaN(columns)) { - cols = !head.cells[columns].hidden; - } else if (isArray(columns)) { cols = []; - each(columns, function(column) { - cols.push(!head.cells[column].hidden); - }); - } + + each(this.select, function(column) { + cols.push(!head.cells[column].hidden); + }); return cols; }, @@ -1077,6 +1198,30 @@ each(dt.columnWidths, function(size, cell) { head.cells[cell].node.style.width = (size / dt.rect.width * 100) + "%"; }); + }, + + cells: function() { + var that = this, + columns = []; + + if (this.select.length == 1) { + this.select = this.select[0]; + } + + each(this.instance.table.rows, function(row, i) { + if (isArray(that.select)) { + columns[i] = []; + } + each(row.cells, function(cell) { + if (isArray(that.select) && that.select.indexOf(cell.index) >= 0) { + columns[i].push(cell); + } else if (that.select == cell.index) { + columns.push(cell); + } + }); + }); + + return columns; } }; @@ -1084,10 +1229,7 @@ var DataTable = function(table, config) { this.config = extend(defaultConfig, config); - this.API = { - rows: new Rows(this), - columns: new Columns(this) - }; + this.node = table; if (this.config.ajax) { var that = this, @@ -1126,6 +1268,7 @@ this.init(); } + }; DataTable.prototype = { @@ -1136,6 +1279,9 @@ var that = this, o = that.config; + that.sortable = o.sortable; + that.searchable = o.searchable; + that.currentPage = 1; that.onFirstPage = true; that.onLastPage = false; @@ -1145,7 +1291,7 @@ that.render(); - if (o.fixedColumns) { + if (o.fixedColumns && that.table.hasHeader) { that.columns().fix(); } @@ -1165,85 +1311,26 @@ } // Check for the columns option - if (o.columns) { - var selectedColumns = []; - var columnRenderers = []; - - each(o.columns, function(data) { - // convert single column selection to array - if (!isArray(data.select)) { - data.select = [data.select]; - } - - if (isset(data, "render") && typeof data.render === "function") { - selectedColumns = selectedColumns.concat(data.select); - - columnRenderers.push({ - columns: data.select, - renderer: data.render - }); - } - - // Add the data attributes to the th elements - if (that.table.hasHeader) { - each(data.select, function(column) { - var cell = that.table.header.cells[column]; - - if (data.type) { - cell.node.setAttribute("data-type", data.type); - } - if (data.format) { - cell.node.setAttribute("data-format", data.format); - } - if (isset(data, "sortable")) { - cell.node.setAttribute("data-sortable", data.sortable); - - if (data.sortable === false) { - classList.remove(cell.node, o.classes.sorter); - } - } - - if (isset(data, "hidden")) { - if (data.hidden !== false) { - that.columns().hide(column); - } - } - - if (isset(data, "sort") && data.select.length === 1) { - that.columns().sort(data.select[0], data.sort); - } - }); - } - }); - - if (selectedColumns.length) { - each(that.table.rows, function(row) { - each(row.cells, function(cell) { - if (selectedColumns.indexOf(cell.index) >= 0) { - each(columnRenderers, function(obj) { - if (obj.columns.indexOf(cell.index) >= 0) { - cell.setContent(obj.renderer.call(that, cell.content, cell, row)); - } - }); - } - }); - }); - } - } + that.columns().init(); that.rows().render(); that.bindEvents(); + that.setClasses(); + that.initialised = true; setTimeout(function() { - that.API.rows.init(); - that.API.columns.init(); that.emit("init"); }, 10); }, + setClasses: function() { + classList.toggle(this.wrapper, "dt-sortable", this.sortable); + classList.toggle(this.wrapper, "dt-searchable", this.searchable); + }, + extend: function() { var that = this; @@ -1264,7 +1351,7 @@ o = that.config; on(that.wrapper, "mousedown", function(e) { - if (e.which === 1 && o.sortable && e.target.nodeName === "TH") { + if (e.which === 1 && that.sortable && e.target.nodeName === "TH") { classList.add(e.target, "loading"); } }); @@ -1277,7 +1364,7 @@ that.page(parseInt(node.getAttribute("data-page"), 10)); } - if (o.sortable && node.nodeName === "TH" && classList.contains(node, o.classes.sorter)) { + if (that.sortable && node.nodeName === "TH" && classList.contains(node, o.classes.sorter)) { if (node.hasAttribute("data-sortable") && node.getAttribute("data-sortable") === "false") return false; e.preventDefault(); @@ -1295,20 +1382,12 @@ classList.contains(node, o.classes.selector) ) { e.preventDefault(); - that.config.perPage = parseInt(node.value, 10); - - if (that.selectors.length > 1) { - each([].slice.call(that.selectors), function(select) { - select.selectedIndex = node.selectedIndex; - }); - } - - that.update(); + that.setPerPage(node.value); } }); } - if (o.searchable) { + if (that.searchable) { on(that.wrapper, "keyup", function(e) { if ( e.target.nodeName === "INPUT" && @@ -1320,7 +1399,7 @@ }); } - if (o.sortable) { + if (that.sortable) { on(that.wrapper, "mousedown", function(e) { if (e.target.nodeName === "TH") { e.preventDefault(); @@ -1390,7 +1469,7 @@ } // Searchable - if (o.searchable) { + if (that.searchable) { var form = [ "
", "", @@ -1450,19 +1529,36 @@ this.emit("update"); }, - getInfo: function() { + fixHeight: function() { + this.container.style.height = null; + if (this.config.fixedHeight) { + this.rect = this.container.getBoundingClientRect(); + this.container.style.height = this.rect.height + "px"; + } + }, + + getInfo: function(data) { // Update the info var current = 0, f = 0, t = 0, + l = 0, items; - if (this.totalPages) { - current = this.currentPage - 1; - f = current * this.config.perPage; - t = f + this.pages[current].length; + if (data) { + current = data.page - 1; + f = current * data.perpage; + t = f + data.data.length; f = f + 1; - items = !!this.searching ? this.searchData.length : this.rows().count(); + items = data.rows; + } else { + if (this.totalPages) { + current = this.currentPage - 1; + f = current * this.config.perPage; + t = f + this.pages[current].length; + f = f + 1; + items = !!this.searching ? this.searchData.length : this.rows().count(); + } } if (this.labels.length && this.config.labels.info.length) { @@ -1563,7 +1659,7 @@ return false; } - this.rows().render(page); + this.rows().render(parseInt(page, 10)); this.emit("page", page); }, @@ -1586,6 +1682,36 @@ this.emit("reset"); }, + set: function(prop, val) { + if (this.hasOwnProperty(prop)) { + this[prop] = val; + + classList.toggle(this.wrapper, "dt-" + prop, this[prop]); + + this.update(); + } + }, + + setPerPage: function(value) { + if (!isNaN(value)) { + value = parseInt(value, 10); + + this.config.perPage = value; + + this.fixHeight(); + + if (this.config.perPageSelect.indexOf(value) >= 0) { + each([].slice.call(this.selectors), function(select) { + select.value = value; + }); + } + + this.update(); + + this.emit("perpage", value); + } + }, + import: function(options) { var that = this, obj = false; @@ -1663,9 +1789,20 @@ } if (obj) { - each(obj.headings, function(heading, i) { - that.table.header.cells[i].setContent(heading); - }); + if (obj.headings) { + if (!this.table.hasHeader) { + this.table.addHeader(obj.headings, true); + } + each(obj.headings, function(heading, i) { + that.table.header.cells[i].setContent(heading); + }); + + this.columns().init(); + } + + if (!this.table.rows.length) { + this.currentPage = 1; + } this.rows().add(obj.data); } @@ -1694,12 +1831,12 @@ this.table.body.appendChild(node); }, - columns: function() { - return this.API.columns; + columns: function(select) { + return new Columns(this, select); }, - rows: function() { - return this.API.rows; + rows: function(select) { + return new Rows(this, select); }, on: function(event, callback) { @@ -1748,6 +1885,14 @@ this.rendered = false; this.initialised = false; + + each(extensions, function(ext) { + if (that[ext] !== undefined && typeof that[ext] === "function") { + if (that[ext].destroy && typeof that[ext].destroy === "function") { + that[ext].destroy(); + } + } + }); } };