forked from jeffdupont/bootstrap-data-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-datatable.min.js
21 lines (21 loc) · 15.4 KB
/
bootstrap-datatable.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*!
* Bootstrap Data Table Plugin v1.5.3
*
* Author: Jeff Dupont
* ==========================================================
* Copyright 2012
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==========================================================
*/
;(function(a){function c(){var a=this.options;f.call(this),a.filterModal&&e.call(this),a.toggleColumns&&d.call(this),a.allowOverflow&&h.call(this),a.allowTableinfo&&g.call(this),this.toolbar()}function d(){var e=(this.options,this.$element,this.$top_details,a("<a></a>")),f=this;return this.$column_modal||(this.$column_modal=a("<div></div>").attr("id","dt-column-modal_"+Math.floor(100*Math.random()+1)).addClass("modal").hide(),this.$column_modalheader=a("<div></div>").addClass("modal-header").append(a("<button></button>").addClass("close").data("dismiss","modal").text("x").click(function(){f.$column_modal.modal("hide")})).append(a("<h3></h3>").text("Toggle Columns")),this.$column_modalfooter=a("<div></div>").addClass("modal-footer").append(a("<a></a>").attr("href","#").addClass("btn btn-primary").text("Save").click(function(){return p.call(f),!1})),this.$column_modalbody=a("<div></div>").addClass("modal-body"),this.$column_modal.append(this.$column_modalheader,this.$column_modalbody,this.$column_modalfooter).appendTo(document.body)),e.addClass("btn").data("toggle","modal").attr("href","#"+this.$column_modal.attr("id")).html('<i class="icon-cog"></i>').click(function(){return f.$column_modal.on("show",function(){m.call(f,f.$column_modalbody)}).modal(),!1}),this.buttons.unshift(e),this.$column_modal}function e(){var b=this.options,d=(this.$element,a("<a></a>"));b.filterModal.hide(),d.addClass("btn").data("toggle","modal").attr("href","#").html('<i class="icon-filter"></i>').click(function(){return a(b.filterModal).modal(),!1}),this.buttons.unshift(d)}function f(){var b=this.options,d=(this.$element,this),e=a("<a></a>").addClass("btn dropdown-toggle").attr("data-toggle","dropdown").html(b.perPage+" ").css({fontWeight:"normal"}).append(a("<span></span>").addClass("caret"));this.buttons.push(e);var f=a("<ul></ul>").addClass("dropdown-menu").css({fontSize:"initial",fontWeight:"normal"}).append(a('<li data-value="5"><a href="#">5</a></li>').click(function(){return j.call(this,d),!1}),a('<li data-value="10"><a href="#">10</a></li>').click(function(){return j.call(this,d),!1}),a('<li data-value="20"><a href="#">20</a></li>').click(function(){return j.call(this,d),!1}),a('<li data-value="50"><a href="#">50</a></li>').click(function(){return j.call(this,d),!1}),a('<li data-value="100"><a href="#">100</a></li>').click(function(){return j.call(this,d),!1}));this.buttons.push(f)}function g(){var b=this.options,d=(this.$element,a("<a></a>"));d.addClass("btn").attr("href","#").html('<i class="icon-info-sign"></i>').click(function(){return!1});var e=[],f=[];a.each(b.sort,function(a,c){if(c.length){var d;for(var f in b.columns)b.columns[f].field==c[0]&&(d=b.columns[f].title);e.push(d+" "+c[1].toUpperCase())}}),a.each(b.filter,function(a,c){var d;for(var e in b.columns)b.columns[e].field==a&&(d=b.columns[e].title);f.push((d||a)+" = '"+c+"'")}),a(d).popover({placement:"bottom",content:a("<dl></dl>").append(e.length>0?'<dt><i class="icon-th-list"></i> Sort:</dt><dd>'+e.join(", ")+"</dd>":"",f.length>0?'<dt><i class="icon-filter"></i> Filter:</dt><dd>'+f.join(", ")+"</dd>":"")}),this.buttons.unshift(d)}function h(){var c=(this.options,this.$wrapper),d=a("<a></a>");d.addClass("btn").attr("href","#").html('<i class="icon-resize-full"></i>').click(function(){return i.call(this,c),!1}),this.buttons.push(d)}function i(b){"scroll"==b.css("overflow")?(a(this).children("i").attr("class","icon-resize-full"),b.css({overflow:"visible",width:"auto"})):(a(this).children("i").attr("class","icon-resize-small"),b.css({overflow:"scroll",width:b.width()}))}function j(b){var c=b.options;c.perPage=a(this).data("value");for(var d=c.currentPage*c.perPage;d>c.totalRows;)c.currentPage--,d=c.currentPage*c.perPage;return 0===c.currentPage&&(c.currentPage=1),a(this).popover&&a(this).popover("hide"),b.render(),!1}function k(){var a=this.options,b=this.$element;c.call(this),a.tableCallback&&"function"==typeof a.tableCallback&&a.tableCallback.call(this),this.loading(!1),b.empty(),this.$default&&b.append(this.$default)}function l(b){var c=b.options;c.filter[a(this).data("filter")]=a(this).val(),c.debug&&console.log(c.filter),b.render()}function m(b){var c=this.options,d=a("<fieldset></fieldset>"),e=this;for(var f in c.columns)if(""!==c.columns[f].title){var g=a('<div class="control-group" style="float: left" data-column="'+f+'"><label class="control-label">'+c.columns[f].title+'</label><div class="controls"><div class="btn-group" data-toggle="buttons-radio"><a href="#" class="btn '+(c.columns[f].hidden?"":"active")+'"><i class="icon-ok"></i></a><a href="#" class="btn '+(c.columns[f].hidden?"active":"")+'"><i class="icon-remove"></i></a></div></div></div>').click(function(){return n.call(this,e),!1});d.append(g)}b.empty(),b.append(a("<form></form>").addClass("form-horizontal").append(d))}function n(b){var c=b.options,d=a(this).data("column"),e=a("."+c.columns[d].classname);return e.is(":visible")?(e.hide(),c.columns[d].hidden=!0):(e.show(),c.columns[d].hidden=!1),a(this).find("a.active").removeClass("active"),c.columns[d].hidden?a(this).find(".icon-remove").parent().addClass("active"):a(this).find(".icon-ok").parent().addClass("active"),!1}function p(){var b=this.options;localStorage&&(localStorage[this.localStorageId]=JSON.stringify(b.columns)),a.ajax({url:b.url,type:"POST",dataType:"json",data:a.extend({},b.post,{action:"save-columns",columns:JSON.stringify(b.columns),sort:JSON.stringify(b.sort),filter:JSON.stringify(b.filter)}),success:function(){b.debug&&console.log("columns saved")}}),this.$column_modal.modal("hide")}var b=function(b,c){this.$element=a(b),this.options=c,this.enabled=!0,this.columns=[],this.rows=[],this.buttons=[],this.localStorageId="datatable_"+(c.id||c.url.replace(/\W/gi,"_"));for(var d in this.options.columns)void 0===typeof this.options.columns[d].sortable&&(this.options.columns[d].sortable=!0);this.$default=this.$element.children().length?this.$element.children():a("<div></div>").addClass("alert alert-error").html("No Results Found"),this.$element.addClass("clearfix"),localStorage&&(localStorage[this.localStorageId]="false"),this.options.autoLoad===!0&&this.render()};b.prototype={constructor:b,render:function(){var b=this.options,d=this.$element;this.loading(!0),this.columns=[],this.rows=[],this.buttons=[],this.$wrapper=void 0,this.$table=void 0,this.$header=void 0,this.$body=void 0,this.$footer=void 0,this.$pagination=void 0,this.$toolbar&&this.$toolbar.remove(),this.$top_details=a("<div></div>").attr("id","dt-top-details"),this.$bottom_details=a("<div></div>").attr("id","dt-bottom-details");var e=this;""!==b.url&&a.ajax({url:b.url,type:"POST",dataType:"json",data:a.extend({},b.post,{currentPage:b.currentPage,perPage:b.perPage,sort:b.sort,filter:b.filter}),success:function(a){return b.debug&&console.log(a),a&&void 0!==a?(e.resultset=a,a.data&&0!==a.data.length?(d.empty(),b.sort=a.sort,b.filter=a.filter,b.totalRows=a.totalRows,a.currentPage&&(b.currentPage=parseInt(a.currentPage)),b.tablePreRender&&"function"==typeof b.tablePreRender&&b.tablePreRender.call(e),d.append(e.table()),d.prepend(e.$top_details),d.append(e.$bottom_details),b.showHeader&&e.header(),b.showFooter&&e.footer(),e.body(),b.showTopPagination&&e.pagination()&&e.$top_details.append(e.pagination().clone(!0)),b.showPagination&&e.pagination()&&e.$bottom_details.append(e.pagination().clone(!0)),e.details(),c.call(e),b.tableCallback&&"function"==typeof b.tableCallback&&b.tableCallback.call(e),e.loading(!1),void 0):(k.call(e),void 0)):(k.call(e),void 0)},error:function(a){b.debug&&console.log(a),k.call(e),e.loading(!1)}})},loading:function(b){var c=this.$element;this.$loading||(this.$loading=a("<div></div>").css({position:"absolute",top:parseInt(c.position().top)+5,left:parseInt(c.position().left)+parseInt(c.css("marginLeft"))+Math.floor(c.width()/4),width:Math.floor(c.width()/2)+"px"}).append(a("<div></div>").addClass("progress progress-striped active").append(a('<div class="bar" style="width: 100%"></div>'))).appendTo(c.parent())),b?c.css({opacity:.2}):(c.css({opacity:1}),this.$loading.remove(),this.$loading=void 0)},toolbar:function(){var b=this.options,c=this.$element,d=this;this.$toolbar=a("<div></div>").addClass("dt-toolbar btn-toolbar pull-right"),this.$button_group=a("<div></div>").addClass("btn-group").appendTo(this.$toolbar);for(var e=0;b.buttons.length>e;e++)this.buttons.push(b.buttons[e]);return a.each(this.buttons,function(){d.$button_group.append(this)}),b.sectionHeader?(this.$section_header=a(b.sectionHeader),this.$section_header.append(this.$toolbar)):""===b.title||this.$section_header?(this.$section_header||(this.$section_header=a("<div></div>")),this.$section_header.append(this.$toolbar)):(this.$section_header=a("<h2></h2>").text(b.title).append(this.$toolbar),c.before(this.$section_header)),this.$toolbar},details:function(){var b=this.options,d=(this.resultset,0),e=0;d=b.currentPage*b.perPage-b.perPage+1,e=b.currentPage*b.perPage,e>b.totalRows&&(e=b.totalRows),a('<div class="pull-left"><p>Showing '+d+" to "+e+" of "+b.totalRows+" rows</p></div>").prependTo(this.$bottom_details)},table:function(){var c=(this.$element,this.options);return this.$table_wrapper||(this.$wrapper=a("<div></div>").addClass("dt-table-wrapper")),this.$table||(this.$table=a("<table></table>").addClass(c.class)),this.$wrapper.append(this.$table),this.$wrapper},header:function(){var b=this.options;if(this.resultset,!this.$header){this.$header=a("<thead></thead>");for(var d in b.columns){var e=this.column(d),f=e.data("column_properties");f.sortable&&!f.custom&&e.click(this,this.sort).css({cursor:"pointer"});for(var g=0;b.sort.length>g;g++)b.sort[g][0]==f.field&&("asc"==b.sort[g][1]?(e.append(a(b.ascending)),f.sortOrder="asc"):"desc"==b.sort[g][1]&&(e.append(a(b.descending)),f.sortOrder="desc"));this.$header.append(e),this.columns.push(e)}b.headerCallback&&"function"==typeof b.headerCallback&&b.headerCallback.call(this),this.$table.append(this.$header)}return this.$header},footer:function(){return this.resultset,this.$footer||(this.$footer=a("<tfoot></tfoot>"),o.footerCallback&&"function"==typeof o.footerCallback&&o.footerCallback.call(this),this.$table.append(this.$footer)),this.$footer},body:function(){var b=this.resultset,c=this.options;if(!this.$body){this.$body=a("<tbody></tbody>");for(var d=0;b.data.length>d;d++){var e=this.row(b.data[d]);this.$body.append(e),this.rows.push(e)}c.showFilterRow&&this.$body.prepend(this.filter()),this.$table.append(this.$body)}return this.$body},filter:function(){var b=a("<tr></tr>"),c=this.options,d=this;b.addClass("dt-filter-row");for(var e in c.columns){var f=a("<td></td>").addClass(c.columns[e].classname);c.columns[e].hidden&&f.hide(),c.columns[e].filter&&c.columns[e].field&&f.append(a("<input/>").attr("name","filter_"+c.columns[e].field).data("filter",c.columns[e].field).val(c.filter[c.columns[e].field]||"").change(function(){l.call(this,d)})),b.append(f)}return b},row:function(b){var c=a("<tr></tr>"),d=this.options;for(var e in d.columns){var f=this.cell(b,e);c.append(f)}return d.rowCallback&&"function"==typeof d.rowCallback&&(c=d.rowCallback(c,b)),c},cell:function(b,c){var d=b[this.options.columns[c].field]||this.options.columns[c].custom,e=a("<td></td>"),f=this.options;return f.columns[c].callback&&"function"==typeof f.columns[c].callback&&(d=f.columns[c].callback(b,f.columns[c])),e.data("cell_properties",f.columns[c]).addClass(f.columns[c].classname).html(d||" "),f.columns[c].css&&e.css(f.columns[c].css),f.columns[c].hidden&&e.hide(),e},column:function(b){var c=a("<th></th>"),d=this.options,e="dt-column_"+b+Math.floor(1e3*Math.random()+1);return d.columns[b].classname=e,c.data("column_properties",d.columns[b]).addClass(e).text(d.columns[b].title),d.columns[b].css&&c.css(d.columns[b].css),d.columns[b].hidden&&c.hide(),c},sort:function(b){var c=a(this).data("column_properties"),d=b.data,e=b.data.options,f=!1;if(c.sortOrder=c.sortOrder?"asc"==c.sortOrder?"desc":"":"asc",e.allowMultipleSort){for(var g=0;e.sort.length>g;g++)e.sort[g][0]==c.field&&(e.sort[g][1]=c.sortOrder,""===c.sortOrder&&e.sort.splice(g,1),f=!0);f||e.sort.push([c.field,c.sortOrder])}else e.sort=[],e.sort.push([c.field,c.sortOrder]);e.debug&&console.log(e.sort),d.render()},pagination:function(){var c=(this.$element,this),d=this.options,e=this.resultset;if(!(d.perPage>=e.totalRows)){if(!this.$pagination){this.$pagination=a("<div></div>").addClass("pagination pagination-right"),d.pageCount=Math.ceil(e.totalRows/d.perPage);var l,m,f=a("<ul></ul>"),g=a("<li></li>").append(a("<a></a>").attr("href","#").data("page",1).html("← First").click(function(){return d.currentPage>1&&(d.currentPage=1,c.render()),!1})),h=a("<li></li>").append(a("<a></a>").attr("href","#").data("page",d.currentPage-1).text("Prev").click(function(){return d.currentPage>1&&(d.currentPage-=1,c.render()),!1})),i=a("<li></li>").append(a("<a></a>").attr("href","#").data("page",d.currentPage+1).text("Next").click(function(){return d.currentPage<d.pageCount&&(d.currentPage+=1,c.render()),!1})),j=a("<li></li>").append(a("<a></a>").attr("href","#").data("page",d.pageCount).html("Last →").click(function(){return d.currentPage<d.pageCount&&(d.currentPage=d.pageCount,c.render()),!1})),k=2*d.pagePadding;k>=d.pageCount?(l=1,m=d.pageCount):(l=d.currentPage-d.pagePadding,0>=l&&(l=1),m=l+k,m>d.pageCount&&(m=d.pageCount,l=m-k));for(var n=l;m>=n;n++){var o=a("<li></li>").append(a("<a></a>").attr("href","#").data("page",n).text(n).click(function(){return d.currentPage!==a(this).data("page")&&(d.currentPage=a(this).data("page"),c.render()),!1}));n==d.currentPage&&o.addClass("active"),f.append(o)}1==d.currentPage&&(g.addClass("disabled"),h.addClass("disabled")),d.currentPage==d.pageCount&&(i.addClass("disabled"),j.addClass("disabled")),f.prepend(g,h),f.append(i,j),this.$pagination.append(f)}return this.$pagination}}},a.fn.datatable=function(c){return a.fn.datatable.init.call(this,c,b,"datatable"),this},a.fn.datatable.init=function(b,c,d){function f(e){var f=a.data(e,d);return f||(f=new c(e,a.fn.datatable.elementOptions(e,b)),a.data(e,d,f)),f}var e;return b===!0?this.data(d):"string"==typeof b?(e=this.data(d),e&&e[b](),this):(b=a.extend({},a.fn[d].defaults,b),this.each(function(){f(this)}),this)},a.fn.datatable.DataTable=b,a.fn.datatable.elementOptions=function(b,c){return a.metadata?a.extend({},c,a(b).metadata()):c},a.fn.datatable.defaults={debug:!1,id:void 0,title:"Data Table Results","class":"table table-striped table-bordered",perPage:10,pagePadding:2,sort:[[]],filter:{},post:{},buttons:[],sectionHeader:void 0,totalRows:0,currentPage:1,showPagination:!1,showTopPagination:!1,showHeader:!0,showFooter:!1,showFilterRow:!1,filterModal:void 0,allowExport:!1,allowOverflow:!0,allowMultipleSort:!1,toggleColumns:!0,url:"",columns:[],ascending:'<i class="icon-chevron-up"></i>',descending:'<i class="icon-chevron-down"></i>',rowCallback:void 0,tableCallback:void 0,headerCallback:void 0,footerCallback:void 0,tablePreRender:void 0,autoLoad:!0}})(window.jQuery);