-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagrid.min.js
7 lines (7 loc) · 5.76 KB
/
datagrid.min.js
1
2
3
4
5
6
7
/* Copyright (c) 2014 Michael Eisenbraun (http://jquery.michaeleisenbraun.com)
* Licensed under the MIT License.
*
* Version: 1.0.0
*
* Requires: jQuery 1.7.2+
*/if(!window.console)var console={log:function(){}};(function(e){e.fn.columns=function(n){return this.each(function(){var r=e(this),i=r.data("columns");i||r.data("columns",i=new t(this,n))}).data("columns")};var t=function(t,n){this.$el=e(t);n&&e.extend(this,n);this.init()};t.prototype={table:null,search:null,schema:null,sortBy:null,showRows:[5,10,25,50],reverse:!1,size:5,page:1,pages:1,query:null,range:null,total:null,master:null,templates:{table:'<div class="{{columns_table_class}}"></div>',search:'<div class="{{columns_search_class}}"><input class="ui-table-search" placeholder="Search" type="text" name="query" /></div>',open:'<table class="{{table_class}}"><thead></tr>',th:'<th class="{{sort}}" data-columns-sortby="{{key}}">{{header}} <span class="ui-arrow">{{arrow}}</th>',tbody:"</tr></thead><tbody>",controls:'<div class="{{table_footer_class}}"><span class="{{show_rows_class}}">Show rows: <select>{{show_rows}}</select></span> <span class="{{table_results_class}}">Results: <strong>{{table_range_start}} – {{table_range_end}}</strong> of <strong>{{table_total}}</strong></span> <span class="{{table_controls_class}}"><span class="{{prev_class}}" data-columns-page="{{prev_page}}">{{prev_text}}</span> <span class="{{next_class}}" data-columns-page="{{next_page}}">{{next_text}}</span></span></div>'},sort:function(){function n(e,n,r){n=n?-1:1;return function(i,s){i=i[e];s=s[e];if(t.test(i)&&t.test(s)){i=new Date(i);i=Date.parse(i);s=new Date(s);s=Date.parse(s)}else if(typeof r!="undefined"){i=r(i);s=r(s)}return i<s?n*-1:i>s?n*1:0}}var e=this,t=/^(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December|(0?\d{1})|(10|11|12))(-|\s|\/|\.)(0?[1-9]|(1|2)[0-9]|3(0|1))(-|\s|\/|\.|,\s)(19|20)?\d\d$/i;e.sortBy&&typeof e.data[0][e.sortBy]!="undefined"&&e.data.sort(n(e.sortBy,e.reverse))},filter:function(){var t=this;t.query&&(t.data=e.grep(t.data,function(e,n){for(val in e)if(typeof e[val]=="string"){var r=new RegExp(t.query,"gi");if(e[val].match(r))return!0}else if(typeof e[val]=="number"&&e[val]==t.query)return!0;return!1}));t.total=t.data.length},paginate:function(){var e=this;e.pages=Math.ceil(e.data.length/e.size);e.page=e.page<=e.pages?e.page:1;var t=(e.page-1)*e.size,n=t+e.size<e.total?t+e.size:e.total;e.range={start:t+1,end:n};e.data=e.data.slice(t,n)},condition:function(){var t=this,n=[],r={};if(t.schema){e.each(t.data,function(i,s){r={};e.each(t.schema,function(e,t){if(t.condition&&!t.condition(s[t.key])){r=null;return!1}r[t.key]=s[t.key]});r&&n.push(r)});t.data=n}},create:function(){var t=this;t.data=[];e.extend(t.data,t.master);t.condition();t.sort();t.filter();t.paginate();var n=t.page-1,r=t.page+1<=t.pages?t.page+1:0,i=t.chevron(t.templates.open,{table_class:"ui-table"});t.schema?e.each(t.data,function(n,r){if(n==0){e.each(t.schema,function(e,n){t.sortBy==n.key?t.reverse?i+=t.chevron(t.templates.th,{sort:"ui-table-sort-down",key:n.key,header:n.header,arrow:" ▼"}):i+=t.chevron(t.templates.th,{sort:"ui-table-sort-up",key:n.key,header:n.header,arrow:"▲"}):i+=t.chevron(t.templates.th,{sort:"",key:n.key,header:n.header,arrow:""})});i+=t.chevron(t.templates.tbody,{})}i+=n%2==0?'<tr class="ui-table-rows-even">':'<tr class="ui-table-rows-odd">';e.each(t.schema,function(e,n){n.template?i+="<td>"+t.chevron(n.template,r)+"</td>":i+="<td>"+r[n.key]+"</td>"});i+="</tr>"}):e.each(t.data,function(n,r){if(n==0){e.each(r,function(e,n){t.sortBy==e?t.reverse?i+=t.chevron(t.templates.th,{sort:"ui-table-sort-down",key:e,header:e,arrow:" ▼"}):i+=t.chevron(t.templates.th,{sort:"ui-table-sort-up",key:e,header:e,arrow:"▲"}):i+=t.chevron(t.templates.th,{sort:"",key:e,header:e,arrow:""})});i+=t.chevron(t.templates.tbody,{})}i+=n%2==0?'<tr class="ui-table-rows-even">':'<tr class="ui-table-rows-odd">';e.each(r,function(e,t){i+="<td>"+t+"</td>"});i+="</tr>"});i+="</tbody></table>";var s={table_footer_class:"ui-table-footer",table_controls_class:"ui-table-controls",prev_page:n,prev_text:'<img src="images/arrow-left.png">',next_page:r,next_text:'<img src="images/arrow-right.png">',table_results_class:"ui-table-results",table_range_start:t.range.start,table_range_end:t.range.end,table_total:t.total,show_rows_class:"ui-table-show-rows",show_rows:""};e.each(t.showRows,function(e,n){n==t.size?s.show_rows+='<option value="'+n+'" selected="selected">'+n+"</option>":s.show_rows+='<option value="'+n+'">'+n+"</option>"});n?s.prev_class="ui-table-control-prev":s.prev_class="ui-table-control-disabled";r?s.next_class="ui-table-control-next":s.next_class="ui-table-control-disabled";i+=t.chevron(t.templates.controls,s);t.table.html(i)},chevron:function(t,n){e.each(n,function(e,n){var r=new RegExp("{{"+e+"}}","g");t=t.replace(r,n)});return t},init:function(){var t=this;if(e.isArray(t.data)){t.master=[];e.extend(t.master,t.data);t.$el.addClass("columns");t.$el.append(t.chevron(t.templates.search,{columns_search_class:"ui-columns-search"}));t.$el.append(t.chevron(t.templates.table,{columns_table_class:"ui-columns-table"}));t.search=e(".ui-columns-search",t.$el);t.table=e(".ui-columns-table",t.$el);t.$el.on("click","th",function(){t.sortBy==e(this).data("columns-sortby")&&(t.reverse=t.reverse?!1:!0);t.sortBy=e(this).data("columns-sortby");t.page=1;t.create()});t.$el.on("click",".ui-table-control-prev, .ui-table-control-next",function(){t.page=e(this).data("columns-page");t.create()});t.$el.on("keyup",".ui-table-search",function(){t.query=e(this).val();t.create()});t.$el.on("change",".ui-table-show-rows select",function(){console.log(e(this).val());t.size=parseInt(e(this).val());t.create()});t.create()}else e.error('The "data" parameter must be an array.')}}})(jQuery);