text: !function(a){a.fn.datagrid=function(b){function h(){a(c).append("<thead></thead><tbody></tbody>")}function i(){(a(c).find("thead").append("<tr id='thead-merge-cells'></tr>"),1==g.rowNumber)&&a(c).find("#thead-merge-cells").append("<th sortable='false' style='text-align: center; width: 20px;' rowspan='2'>No</th>");if(1==g.rowCheck){a(c).find("#thead-merge-cells").append("<th sortable='false' style='text-align: center; width: 20px;' rowspan='2'><input type='checkbox'></th>"),t()}if(!a.isEmptyObject(g.rowDetail)){a(c).find("#thead-merge-cells").append("<th sortable='false' style='text-align: center; width: 20px;' rowspan='2'></th>")}g.columns.forEach(function(b,d){var e=!1,f="",h="",i="";g.mergeCells.forEach(function(a){d==a.index&&(e=!0,f=a.align,h=a.colspan,i=a.title)}),e||g.mergeCells.forEach(function(a){d>a.index&&d<a.index+a.colspan&&(e=null)});var j="";e?j="<th sortable='false' style='text-align: "+f+";' colspan='"+h+"'>"+i+"</th>":0==e&&(b.sortable=void 0==b.sortable||b.sortable,b.width=void 0==b.width?150:b.width,b.align=void 0==b.align?"center":b.align,j="<th title='"+b.field+"' sortable='"+b.sortable+"' style='text-align: "+b.align+"; width: "+b.width+"px;' rowspan='2'>"+b.title+"</th>"),a(c).find("#thead-merge-cells").append(j)})}function j(){(a(c).find("thead").append("<tr id='thead-title'></tr>"),1==g.rowNumber&&0==g.mergeCells.length)&&a(c).find("#thead-title").append("<th sortable='false' style='text-align: center; width: 20px;'>No</th>");if(1==g.rowCheck&&0==g.mergeCells.length){a(c).find("#thead-title").append("<th sortable='false' style='text-align: center; width: 20px;'><input type='checkbox'></th>"),t()}if(!a.isEmptyObject(g.rowDetail)&&0==g.mergeCells.length){a(c).find("#thead-title").append("<th sortable='false' style='text-align: center; width: 20px;'></th>")}g.columns.forEach(function(b,d){var e=!1;g.mergeCells.length>0?g.mergeCells.forEach(function(a){d>=a.index&&d<a.index+a.colspan&&(e=!0)}):e=!0;var f;e&&(b.sortable=void 0==b.sortable||b.sortable,b.width=void 0==b.width?150:b.width,b.align=void 0==b.align?"center":b.align,f="<th title='"+b.field+"' sortable='"+b.sortable+"' style='text-align: "+b.align+"; width: "+b.width+"px;'>"+b.title+"</th>"),a(c).find("#thead-title").append(f)})}function k(b,c,e,f){var h=a.extend({limit:parseInt(b)*c-c,offset:c,sort:g.sortBy,order:g.orderBy,dataSearch:e},g.queryParams);a.ajax({url:g.url,type:"post",dataType:"json",data:h,success:function(a){d=a,f()}})}function l(b){var d=g.columns.length+(p()+1),e="<tr><td colspan='"+d+"' style='text-align: center;'>"+b+"</td></tr>";a(c).find("tbody").html(e)}function m(b,e,f){l("Loading data..."),k(b,e,f,function(){var f=parseInt(b)*e-e+1,h="",i="",j="";if(parseInt(b)>Math.ceil(d.total/e)&&Math.ceil(d.total/e)>0&&(g.activePageNumber=1,m(g.activePageNumber,g.itemsPerPage,w())),d.total>=1){for(var k=0;k<d.rows.length;k++)h+="<tr class='main-row'>",g.rowNumber&&(h+="<td style='text-align: center;'>"+f+"</td>"),g.rowCheck&&(h+="<td class='checkbox-column' style='text-align: center;'><input value='"+d.rows[k][g.primaryField]+"' type='checkbox'></td>"),a.isEmptyObject(g.rowDetail)||(h+="<td style='text-align: center;'><div class='detail-link' data-id='"+k+"' data-expand='false' style='width: 10px; height: 4px; margin-left: auto; margin-right: auto; cursor: pointer; margin-top: 8px; background-color: #595959; border: 1px solid #595959; -border-radius: 0.1em; -moz-border-radius: 0.1em; -webkit-border-radius: 0.1em;'><div style='width: 4px; height: 10px; cursor: pointer; margin-top: -4px; margin-left: 2px; background-color: #595959; border: 1px solid #595959; -border-radius: 0.1em; -moz-border-radius: 0.1em; -webkit-border-radius: 0.1em;'></div></div></td>"),g.columns.forEach(function(a){void 0!==d.rows[k][a.field]?h+="<td style='text-align: "+a.align+"; width: "+a.width+"px;'>"+d.rows[k][a.field]+"</td>":void 0!==a.rowStyler?(i=d.rows[k],j=k,h+="<td style='text-align: "+a.align+"; width: "+a.width+"px;'>"+a.rowStyler(d.rows[k],k)+"</td>"):h+="<td style='text-align: "+a.align+"; width: "+a.width+"px;'>Undefined</td>"}),h+="</tr>",f++;a(c).find("tbody").html(h)}else l("No records found...");n(b,e),q(),x(b,e),A()})}function n(b,c){var e=Math.ceil(d.total/c);a(g.pagingElement).html("");var f=a('<li><a href="javascript:void(0);">First</a></li>');b>1&&a(f).on("click",function(){m(1,c,w())}),a(g.pagingElement).append(f);var h=a('<li><a href="javascript:void(0);">Prev</a></li>');b>1&&a(h).on("click",function(){m(b-1,c,w())}),a(g.pagingElement).append(h);var i;if(b-3<=0)i=1;else{var j=0;if(b+3>e){var k=b+3-e;j=b-3-k>0?k:b-1-3}else j=0;i=b-3-j}for(var l=i;l<b;l++){var n=a('<li data-page="'+l+'"><a href="javascript:void(0);">'+l+"</a></li>");a(n).on("click",function(){m(parseInt(a(this).attr("data-page")),c,w())}),a(g.pagingElement).append(n)}a(g.pagingElement).append('<li class="active"><a href="javascript:void(0);">'+b+"</a></li>");var o;if(b+3>e)o=e;else{var j=0;if(b-3<=0){var k=3-(b-1);j=b+3+k>e?e-b-3:k}else j=0;o=b+3+j}for(var l=b+1;l<=o;l++){var n=a('<li data-page="'+l+'"><a href="javascript:void(0);">'+l+"</a></li>");a(n).on("click",function(){m(parseInt(a(this).attr("data-page")),c,w())}),a(g.pagingElement).append(n)}var p=a('<li><a href="javascript:void(0);">Next</a></li>');b<e&&a(p).on("click",function(){m(b+1,c,w())}),a(g.pagingElement).append(p);var q=a('<li><a href="javascript:void(0);">Last</a></li>');b<e&&a(q).on("click",function(){m(e,c,w())}),a(g.pagingElement).append(q)}function o(){for(var b="",c=0;c<g.itemsPerPageOption.length;c++)b+="<option value='"+g.itemsPerPageOption[c]+"'>"+g.itemsPerPageOption[c]+"</option>";a(g.optionPagingElement).html(b),a(g.optionPagingElement).val(g.itemsPerPage),a(g.optionPagingElement).on("change",function(){g.itemsPerPage=a(g.optionPagingElement).children("option:selected").val(),m(1,g.itemsPerPage,w())})}function p(){var b=-1;return 1==g.rowNumber&&(b+=1),1==g.rowCheck&&(b+=1),a.isEmptyObject(g.rowDetail)||(b+=1),b}function q(){a(c).find(".checkbox-column").each(function(){a(this).children().each(function(b,c){for(var d=0;d<g.rowChecked.length;d++)g.rowChecked[d]==a(this).attr("value")&&a(this).prop("checked",!0);a(this).on("click",function(){for(var a,b=!1,c=0;c<g.rowChecked.length;c++)g.rowChecked[c]==this.value&&(b=!0,a=c);b?g.rowChecked.splice(a,1):g.rowChecked[g.rowChecked.length]=parseInt(this.value),r()})})}),r()}function r(){for(var b=a(c).children("thead").find('input[type="checkbox"]'),d=s(),e=!0,f=0;f<d.length;f++)a(d[f]).prop("checked")||(e=!1);e?b.prop("checked",!0):b.prop("checked",!1)}function s(){var b=[];return a(c).find(".checkbox-column").each(function(){a(this).children().each(function(c,d){b[b.length]=a(this)})}),b}function t(){var b=a(c).children("thead").find('input[type="checkbox"]');a(b).on("click",function(){var b=s();if(a(this).prop("checked"))for(var c=0;c<b.length;c++){a(b[c]).prop("checked",!0);for(var d,e=!1,f=0;f<g.rowChecked.length;f++)g.rowChecked[f]==a(b[c]).attr("value")&&(e=!0,d=f);e||(g.rowChecked[g.rowChecked.length]=parseInt(a(b[c]).attr("value")))}else for(var c=0;c<b.length;c++){a(b[c]).prop("checked",!1);for(var d,e=!1,f=0;f<g.rowChecked.length;f++)g.rowChecked[f]==a(b[c]).attr("value")&&(e=!0,d=f);e&&g.rowChecked.splice(d,1)}})}function u(){e={selector:g.searchInputElement,element:a(g.searchInputElement).clone()},g.columns.forEach(function(b,c){b.search=void 0==b.search||b.search,b.search&&(void 0==b.custom_search?a(g.searchFieldElement).append('<option value="'+b.field+'">'+b.title+"</option>"):a(g.searchFieldElement).append('<option data-option="'+c+'" value="'+b.field+'">'+b.title+"</option>"))}),v(g.searchFieldElement),a(g.searchFieldElement).on("change",function(){v(a(this))})}function v(b){if(void 0==a(b).find("option:selected").attr("data-option"))a(g.searchInputElement).replaceWith(e.element),a(g.searchInputElement).on("keyup",function(){f(function(){m(1,g.itemsPerPage,w())},g.delayTime)});else{var d=a("<select></select>"),h=a(b).find("option:selected").attr("value");g.columns.forEach(function(b,c){if(b.field==h){if(-1==e.selector.indexOf(".")){var f=e.selector.replace("#","");a(d).attr("id",f),a(d).attr("class",b.custom_search.appendClass)}else{var f=e.selector.replace(".","");a(d).attr("class",b.custom_search.appendClass+" "+f)}b.custom_search.option.forEach(function(b,c){a(d).append('<option value="'+b.value+'">'+b.text+"</option>")})}}),a(d).on("change",function(){m(1,g.itemsPerPage,w())}),a(g.searchInputElement).replaceWith(d)}m(1,g.itemsPerPage,w())}function w(){var b=a(g.searchFieldElement).val(),c=a(g.searchInputElement).val(),d={field:b,value:c};return void 0==b||void 0==c?"":d}function x(b,c){if(d.total>=1){var e,f;e=b*c-c+1,f=b==Math.ceil(d.total/c)?b*c-(b*c-d.total):b*c,a(g.pageInfoElement).html("Showing "+e+" - "+f+" of "+d.total+" entries")}else a(g.pageInfoElement).html("Showing 0 - 0 of 0 entries")}function y(){a(c).children("thead").children().children().each(function(b,c){if("false"!=a(c).attr("sortable")){var d=a("<span></span>");a(d).css({width:"0px",height:"0px",border:"4px solid transparent","border-bottom":"5px solid #ccc",position:"absolute","margin-left":"5px","margin-top":"0px"}),a(this).append(d);var e=a("<span></span>");a(e).css({width:"0px",height:"0px",border:"4px solid transparent","border-top":"5px solid #ccc",position:"absolute","margin-left":"5px","margin-top":"11px"}),a(this).append(e)}})}function z(){g.sortBy=g.primaryField,y(),a(c).children("thead").children().children().each(function(b,d){"false"!=a(d).attr("sortable")&&(a(this).css("cursor","pointer"),a(this).on("click",function(){a(c).children("thead").children().children().each(function(b,c){a(this).children("span").remove()}),y();var b=a("<span></span>");a(this).append(b),null==a(this).attr("data-sortby")||"DESC"==a(this).attr("data-sortby")?(a(this).attr("data-sortby","ASC"),a(this).children("span").css({width:"0px",height:"0px",border:"4px solid transparent","border-bottom":"5px solid #333",position:"absolute","margin-left":"5px","margin-top":"3px"})):(a(this).attr("data-sortby","DESC"),a(this).children("span").css({width:"0px",height:"0px",border:"4px solid transparent","border-top":"5px solid #333",position:"absolute","margin-left":"5px","margin-top":"7px"})),g.sortBy=this.title,g.orderBy=a(this).attr("data-sortby"),m(g.activePageNumber,g.itemsPerPage,w())}))})}function A(){a(c).find(".detail-link").each(function(){a(this).off(),a(this).on("click",function(){var b=a(this).attr("data-id");if("false"==a(this).attr("data-expand")){var c="<tr id='detail-row-"+b+"' style='display: none;'><td colspan='"+(p()+1)+"'></td><td colspan='"+g.columns.length+"' class='detail-content'>"+g.rowDetail.formatter(d.rows[b],b);a(c).insertAfter(a(this).closest("tr")).fadeIn(),g.rowDetail.onExpandRow(d.rows[b],b);var e="<div class='detail-link' data-id='"+b+"' data-expand='true' style='width: 10px; height: 4px; margin-left: auto; margin-right: auto; cursor: pointer; margin-top: 8px; background-color: #595959; border: 1px solid #595959; -border-radius: 0.1em; -moz-border-radius: 0.1em; -webkit-border-radius: 0.1em;'></div>";a(this).closest("td").html(e)}else{a(this).closest("tr").next().fadeOut(function(){a(this).remove()});var e="<div class='detail-link' data-id='"+b+"' data-expand='false' style='width: 10px; height: 4px; margin-left: auto; margin-right: auto; cursor: pointer; margin-top: 8px; background-color: #595959; border: 1px solid #595959; -border-radius: 0.1em; -moz-border-radius: 0.1em; -webkit-border-radius: 0.1em;'><div style='width: 4px; height: 10px; cursor: pointer; margin-top: -4px; margin-left: 2px; background-color: #595959; border: 1px solid #595959; -border-radius: 0.1em; -moz-border-radius: 0.1em; -webkit-border-radius: 0.1em;'></div></div>";a(this).closest("td").html(e)}A()})})}function B(b,c,e,f,h,i){if(c>p()){var j,k;if(g.columns.forEach(function(a,b){b+p()+1==c&&(j=a.editable,k=a.field)}),j){var m,j=a(b).html(),l=f(k,j);if(g.columns.forEach(function(a,b){a.editable&&(m=b)}),"active"!=a(b).attr("inline-edit"))a(b).attr("inline-edit","active"),a(b).html("<form>"+l+"</form>"),m+p()+1==c&&h();else{a(b).attr("inline-edit","not-active");var n=a(b).children("form").serializeArray();a(b).html(n[0].value),d.rows[e][k]=n[0].value,m+p()+1==c&&i()}}}}function C(b,c,d,e,f,g){a(b).children().each(function(b,h){"all"==c?B(a(this),b,d,e,f,g):b==c+p()+1&&B(a(this),b,d,e,f,g)})}var c=a(this),d=[],e={},f=function(){var a=0;return function(b,c){clearTimeout(a),a=setTimeout(b,c)}}(),g=a.extend({url:"",primaryField:"",sortBy:"",orderBy:"DESC",pagingElement:"",optionPagingElement:"",searchFieldElement:"",searchInputElement:"",pageInfoElement:"",delayTime:800,rowNumber:!1,rowCheck:!1,columns:[],mergeCells:[],activePageNumber:1,itemsPerPage:5,itemsPerPageOption:[5,10,15,20],rowChecked:[],queryParams:{},rowDetail:{}},b);return this.reload=function(){m(g.activePageNumber,g.itemsPerPage,w())},this.getChecked=function(){return g.rowChecked},this.setChecked=function(a){for(var b=0;b<a.length;b++){for(var c=!1,d=0;d<g.rowChecked.length;d++)g.rowChecked[d]==a[b]&&(c=!0);c||(g.rowChecked[g.rowChecked.length]=a[b])}m(g.activePageNumber,g.itemsPerPage,w())},this.setUnchecked=function(a){for(var b=0;b<a.length;b++){for(var c,d=!1,e=0;e<g.rowChecked.length;e++)g.rowChecked[e]==a[b]&&(d=!0,c=e);d&&g.rowChecked.splice(c,1)}m(g.activePageNumber,g.itemsPerPage,w())},this.getRowData=function(a){return"all"==a?d.rows:d.rows[a]},this.editable=function(b){var d=a.extend({rowIndex:"all",columnIndex:"all",styler:function(a,b){return b},onEdit:function(){console.log("Edited")},onSave:function(){console.log("Saved")}},b);a(c).children("tbody").children(".main-row").each(function(b,c){"all"==d.rowIndex?C(a(this),d.columnIndex,b,d.styler,d.onEdit,d.onSave):b==d.rowIndex&&C(a(this),d.columnIndex,b,d.styler,d.onEdit,d.onSave)})},this.queryParams=function(b){g.queryParams=a.extend(g.queryParams,b)},this.formLoad=function(b,c){a(b).find("input[type=text]").each(function(){for(var b in d.rows[c])this.name==b&&a(this).val(d.rows[c][b])}),a(b).find("input[type=password]").each(function(){for(var b in d.rows[c])this.name==b&&a(this).val(d.rows[c][b])}),a(b).find("textarea").each(function(){for(var b in d.rows[c])this.name==b&&a(this).val(d.rows[c][b])}),a(b).find("select").each(function(){for(var b in d.rows[c])if(this.multiple){if(this.name==b+"[]"){var e=this;a.each(d.rows[c][b].split(","),function(b,c){a(e).children("option").each(function(){this.value==c&&a(this).prop("selected","true")})})}}else this.name==b&&a(this).val(d.rows[c][b])}),a(b).find("input[type=checkbox]").each(function(){for(var b in d.rows[c])this.name==b&&this.value==d.rows[c][b]&&a(this).prop("checked",!0)}),a(b).find("input[type=radio]").each(function(){for(var b in d.rows[c])this.name==b&&this.value==d.rows[c][b]&&a(this).prop("checked",!0)})},this.run=function(){h(),g.mergeCells.length>0&&i(),j(),z(),m(g.activePageNumber,g.itemsPerPage,w()),o(),u()},this}}(jQuery);