/*jshint eqeqeq:false */ /*global jQuery */ (function($){ /** * jqGrid extension for SubGrid Data * Tony Tomov tony@trirand.com * http://trirand.com/blog/ * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl-2.0.html **/ "use strict"; $.jgrid.extend({ setSubGrid : function () { return this.each(function (){ var $t = this, cm, i, suboptions = { plusicon : "ui-icon-plus", minusicon : "ui-icon-minus", openicon: "ui-icon-carat-1-sw", expandOnLoad: false, delayOnLoad : 50, selectOnExpand : false, selectOnCollapse : false, reloadOnExpand : true }; $t.p.subGridOptions = $.extend(suboptions, $t.p.subGridOptions || {}); $t.p.colNames.unshift(""); $t.p.colModel.unshift({name:'subgrid',width: $.jgrid.cell_width ? $t.p.subGridWidth+$t.p.cellLayout : $t.p.subGridWidth,sortable: false,resizable:false,hidedlg:true,search:false,fixed:true}); cm = $t.p.subGridModel; if(cm[0]) { cm[0].align = $.extend([],cm[0].align || []); for(i=0;i"; }, addSubGrid : function( pos, sind ) { return this.each(function(){ var ts = this; if (!ts.grid ) { return; } //------------------------- var subGridCell = function(trdiv,cell,pos) { var tddiv = $("").html(cell); $(trdiv).append(tddiv); }; var subGridXml = function(sjxml, sbid){ var tddiv, i, sgmap, dummy = $("
"), trdiv = $(""); for (i = 0; i"); $(tddiv).html(ts.p.subGridModel[0].name[i]); $(tddiv).width( ts.p.subGridModel[0].width[i]); $(trdiv).append(tddiv); } $(dummy).append(trdiv); if (sjxml){ sgmap = ts.p.xmlReader.subgrid; $(sgmap.root+" "+sgmap.row, sjxml).each( function(){ trdiv = $(""); if(sgmap.repeatitems === true) { $(sgmap.cell,this).each( function(i) { subGridCell(trdiv, $(this).text() || ' ',i); }); } else { var f = ts.p.subGridModel[0].mapping || ts.p.subGridModel[0].name; if (f) { for (i=0;i"), trdiv = $(""); for (i = 0; i"); $(tddiv).html(ts.p.subGridModel[0].name[i]); $(tddiv).width( ts.p.subGridModel[0].width[i]); $(trdiv).append(tddiv); } $(dummy).append(trdiv); if (sjxml){ sgmap = ts.p.jsonReader.subgrid; result = $.jgrid.getAccessor(sjxml, sgmap.root); if ( result !== undefined ) { for (i=0;i"); if(sgmap.repeatitems === true) { if(sgmap.cell) { cur=cur[sgmap.cell]; } for (j=0;j 0) { i = sind; len = sind+1; } while(i < len) { if($(ts.rows[i]).hasClass('jqgrow')) { if(ts.p.scroll) { $(ts.rows[i].cells[pos]).unbind('click'); } $(ts.rows[i].cells[pos]).bind('click', function() { var tr = $(this).parent("tr")[0]; r = tr.nextSibling; if($(this).hasClass("sgcollapsed")) { pID = ts.p.id; _id = tr.id; if(ts.p.subGridOptions.reloadOnExpand === true || ( ts.p.subGridOptions.reloadOnExpand === false && !$(r).hasClass('ui-subgrid') ) ) { atd = pos >=1 ? " ":""; bfsc = $(ts).triggerHandler("jqGridSubGridBeforeExpand", [pID + "_" + _id, _id]); bfsc = (bfsc === false || bfsc === 'stop') ? false : true; if(bfsc && $.isFunction(ts.p.subGridBeforeExpand)) { bfsc = ts.p.subGridBeforeExpand.call(ts, pID+"_"+_id,_id); } if(bfsc === false) {return false;} $(tr).after( ""+atd+"
" ); $(ts).triggerHandler("jqGridSubGridRowExpanded", [pID + "_" + _id, _id]); if( $.isFunction(ts.p.subGridRowExpanded)) { ts.p.subGridRowExpanded.call(ts, pID+"_"+ _id,_id); } else { populatesubgrid(tr); } } else { $(r).show(); } $(this).html("").removeClass("sgcollapsed").addClass("sgexpanded"); if(ts.p.subGridOptions.selectOnExpand) { $(ts).jqGrid('setSelection',_id); } } else if($(this).hasClass("sgexpanded")) { bfsc = $(ts).triggerHandler("jqGridSubGridRowColapsed", [pID + "_" + _id, _id]); bfsc = (bfsc === false || bfsc === 'stop') ? false : true; _id = tr.id; if( bfsc && $.isFunction(ts.p.subGridRowColapsed)) { bfsc = ts.p.subGridRowColapsed.call(ts, pID+"_"+_id,_id ); } if(bfsc===false) {return false;} if(ts.p.subGridOptions.reloadOnExpand === true) { $(r).remove(".ui-subgrid"); } else if($(r).hasClass('ui-subgrid')) { // incase of dynamic deleting $(r).hide(); } $(this).html("").removeClass("sgexpanded").addClass("sgcollapsed"); if(ts.p.subGridOptions.selectOnCollapse) { $(ts).jqGrid('setSelection',_id); } } return false; }); } i++; } if(ts.p.subGridOptions.expandOnLoad === true) { $(ts.rows).filter('.jqgrow').each(function(index,row){ $(row.cells[0]).click(); }); } ts.subGridXml = function(xml,sid) {subGridXml(xml,sid);}; ts.subGridJson = function(json,sid) {subGridJson(json,sid);}; }); }, expandSubGridRow : function(rowid) { return this.each(function () { var $t = this; if(!$t.grid && !rowid) {return;} if($t.p.subGrid===true) { var rc = $(this).jqGrid("getInd",rowid,true); if(rc) { var sgc = $("td.sgcollapsed",rc)[0]; if(sgc) { $(sgc).trigger("click"); } } } }); }, collapseSubGridRow : function(rowid) { return this.each(function () { var $t = this; if(!$t.grid && !rowid) {return;} if($t.p.subGrid===true) { var rc = $(this).jqGrid("getInd",rowid,true); if(rc) { var sgc = $("td.sgexpanded",rc)[0]; if(sgc) { $(sgc).trigger("click"); } } } }); }, toggleSubGridRow : function(rowid) { return this.each(function () { var $t = this; if(!$t.grid && !rowid) {return;} if($t.p.subGrid===true) { var rc = $(this).jqGrid("getInd",rowid,true); if(rc) { var sgc = $("td.sgcollapsed",rc)[0]; if(sgc) { $(sgc).trigger("click"); } else { sgc = $("td.sgexpanded",rc)[0]; if(sgc) { $(sgc).trigger("click"); } } } } }); } }); })(jQuery);