/ Published in: JavaScript
Filters a dynamics CRM 2011 subgrid...implementation example included at bottom
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
if (typeof (EASI) == "undefined") { EASI = {}; } EASI._construct = function () { //filter subgrid and refresh function this.FilterSubgridandRefresh = function (subgridname, stringArrayOfCondtionsToAdd, removeExistingConditionsBool) { try { window.setTimeout(function () { var subgrid = document.getElementById(subgridname); if (subgrid == undefined || subgrid == null) { } var oldFetch = $("#" + subgridname).find("#effectiveFetchXml").attr("value"); if (oldFetch == undefined) { //we have to refresh the grid now because it was in a tab that was collapsed so it hasn't run yet subgrid.control.refresh(); //re-run this operation setTimeout(function () { EASI.FilterSubgridandRefresh(subgridname, stringArrayOfCondtionsToAdd, removeExistingConditionsBool); }, 1000); } else { var html_fetch = $("<div>" + oldFetch + "</div>"); if (removeExistingConditionsBool)//remove the existing conditions html_fetch.find("condition[operator!='']").remove(); for (var i in stringArrayOfCondtionsToAdd) { var cond = $(stringArrayOfCondtionsToAdd[i]); html_fetch.find("filter").append(cond); } var newFetch = html_fetch.html(); subgrid.control.SetParameter("fetchXml", newFetch); subgrid.control.refresh(); } }, 1000); } catch (err) { alert("Error in subgrid refresh\r\n" + err.message); } } //get crm field value function this.GetFieldValue = function (fieldlogicalname) { if (typeof (Xrm) == "undefined") Xrm = window.parent.Xrm; var atype = Xrm.Page.getControl(fieldlogicalname).getAttribute().getAttributeType(); if (atype == this.CRM.type_optionset) { return this.GetPicklistSelectedValue(fieldlogicalname); } else if (atype == this.CRM.type_lookup) { return this.GetLookupId(fieldlogicalname); } else { return Xrm.Page.getControl(fieldlogicalname).getAttribute().getValue(); } } } EASI._construct(); /*here is an implementation example*/ function filterGrid_Load() { var oid = $("#opportunityid").attr("value"); var configno = EASI.GetFieldValue("easi_configurationno"); FilterSubGrid_OppDetailSubgrid("DoNotRemove_options_subgrid", oid, configno, 101); function FilterSubGrid_OppDetailSubgrid(subgridname, oppid, configno, lineitemtype) { try { var newcond1 = "<condition attribute=\"easi_configurationno\" operator=\"eq\" value=\"" + configno + "\" />"; var newcond2 = "<condition attribute=\"opportunityid\" operator=\"eq\" value=\"" + oppid + "\" />"; var newcond3 = "<condition attribute=\"easi_lineitemtype\" operator=\"eq\" value=\"" + lineitemtype + "\" />"; var newcond4 = "<condition attribute=\"easi_removeoption\" operator=\"ne\" value=\"" + 1 + "\" />"; var condArray = new Array(newcond1, newcond2, newcond3, newcond4); EASI.FilterSubgridandRefresh(subgridname, condArray, true); } catch (err) { alert("error filtering subgrid: " + err.message); } } }