/* * Grid for jQuery * * Copyright (c) 2011 Acatl Pacheco https://github.com/acatl/jquery.grid * Licensed under the MIT License. * http://jquery.org/license */ (function(){if(document.itemrenderer===undefined)document.itemrenderer={};if(document.itemrenderer.classes===undefined)document.itemrenderer.classes={};document.itemrenderer.classes.Grid={options:{dataProvider:null,columns:[],rowFunction:null},_create:function(){this.element.addClass("ui-grid");this.elements={};this.redraw()},redraw:function(a){if(typeof a!="undefined"){if(typeof a.columns!="undefined")this.options.columns=a.columns;if(typeof a.rowFunction!="undefined")this.options.rowFunction=a.rowFunction; if(typeof a.dataProvider!="undefined")this.options.dataProvider=a.dataProvider}if(this.options.id==null||this.options.id=="")this.options.id=this._guid();this._removeTable();this.elements.table=$("
").attr("id",this.options.id).addClass("grid").appendTo(this.element);this.elements.tableHead=$("").appendTo(this.elements.table);this.elements.tableBody=$("").appendTo(this.elements.table);this._createTableHeader(this.options.columns);this.options.dataProvider!= null&&this._updateData()},_createTableHeader:function(a){tr=$("");for(var c in a)this._createHeaderCell(tr,c,a[c]).appendTo(tr);tr.appendTo(this.elements.tableHead)},_createHeaderCell:function(a,c,b){var e=$("");e.addClass("col"+c);e.addClass("header");e.attr("data-id",b.id||b.field);e.attr("data-field",b.field);if(b.customHeaderAttributes!=null)try{e.attr(b.customHeaderAttributes)}catch(d){}var f=null;if(b.headerRenderer!=null)f=b.headerRenderer;else if(this.options.headerRenderer!= null)f=this.options.headerRenderer;if(f!=null){a={listData:{owner:this,data:null,column:b,index:c,th:e,tr:a}};b.itemRendererOptions!=null&&jQuery.extend(a,b.headerOptions);e[f](a)}else e.html(b.label);b.headerAttr!=null&&e.attr(b.headerAttr);b.headerStyle!=null&&this._applyStyle(e,b.headerStyle);if(b.width!=null)try{e.css("width",b.width)}catch(g){}b.visible==false&&e.hide();return e},_updateData:function(){$(this.elements.tableBody).children().remove();var a=this.options.dataProvider,c;for(c in a){var b= a[c],e=this._buildRow(c,b,this.elements.tableBody),d;for(d in this.options.columns){var f=this._buildCell(e,c,d,b,this.options.columns[d]);e.append(f)}}},_buildRow:function(a,c,b){b=$("").appendTo(b);var e=this._guid();b.attr("data-uid",e);this.options.rowFunction!=null&&this.options.rowFunction(b,a,c);return b},_buildCell:function(a,c,b,e,d){var f=$("");f.addClass("col"+b);f.addClass("column");d.id=d.id||d.field;f.attr("data-id",d.id||d.field);f.attr("data-field",d.field);this._applyStyle(f, d.style);if(d.customAttributes!=null)try{f.attr(d.customAttributes)}catch(g){}b=this._assignlabelData(e,d);d.visible==false&&f.hide();if(d.itemRenderer!=null||d.rendererFunction!=null){a={data:b,listData:{owner:this,data:e,column:d,index:c,td:f,tr:a}};d.itemRendererOptions!=null&&jQuery.extend(a,d.itemRendererOptions);if(d.itemRenderer!=null)f[d.itemRenderer](a);else d.rendererFunction!=null&&d.rendererFunction(f,a)}else f.html(b);typeof b=="string"&&d.showToolTip==true&&f.attr("title",b);d.styleFunction!= null&&d.styleFunction(f,c,e,d);return f},_assignlabelData:function(a,c){if(c.labelFunction!=null)return c.labelFunction(a,c);if(c.labelField!=null)return a[c.field][c.labelField];if(a[c.field]!=null)return a[c.field];return""},_setOption:function(a,c){switch(a){case "dataProvider":this.options.dataProvider=c;this._updateData();break;case "columns":this.options.columns=c;this._updateData()}$.Widget.prototype._setOption.apply(this,arguments)},showLoading:function(){this.elements.table.addClass("loading"); this.elements.tableBody.animate({opacity:0.1},200)},hideLoading:function(){this.elements.table.removeClass("loading");this.elements.tableBody.animate({opacity:1},200)},_applyStyle:function(a,c){a==null||c==null||(typeof c=="string"?a.addClass(c):a.css(c))},_guid:function(){var a=function(){return((1+Math.random())*65536|0).toString(16).substring(1)};return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()},_removeTable:function(){if(typeof this.elements.table!="undefined"){this.elements.table.remove(); delete this.elements.table;delete this.elements.tableBody;delete this.elements.tableHead}},destroy:function(){$.Widget.prototype.destroy.apply(this,arguments);this._removeTable()}}})();$.widget("ir.grid",document.itemrenderer.classes.Grid);