javascript 动态过滤表单上的子网格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 动态过滤表单上的子网格相关的知识,希望对你有一定的参考价值。

function dynamicallyFilterSubGrid() {

    //RelatedStatement : is name of subgrid given on Form. 
    var objSubGrid =  window.parent.document.getElementById("statements");
    //var objSubGrid = Xrm.Page.ui.controls.get("statements");
    
    //CRM loads subgrid after form is loaded.. so when we are adding script on form load.. need to wait unitil subgrid is loaded. 
    // thats why adding delay..
    
    if (objSubGrid == null) {	
        setTimeout(dynamicallyFilterSubGrid, 2000);
        return;
    }

    try {
		var contact = Xrm.Page.getAttribute("parentcontactid").getValue();
		var account = Xrm.Page.getAttribute("parentaccountid").getValue(); 

		if (contact != null && account == null)
		{

		Xrm.Page.ui.controls.get("statements").setVisible(true);
		var contactid = contact[0].id;

		//Create FetchXML for sub grid to filter records based on category
		var FetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
						"<entity name='avt_pristopnaizjava'>" +
							"<attribute name='avt_st_pi'/>" +
							"<attribute name='avt_skladid'/>" +
							"<attribute name='avt_datumpristopa'/>" +
							"<attribute name='avt_svetovalecid'/>" +
							"<order descending='false' attribute='avt_st_pi'/>" +
							"<filter type='and'>" +
								"<condition attribute='statecode' value='0' operator='eq'/>" +
								"<condition attribute='avt_vlagateljfo' operator='eq' value='" + contactid + "' />" +
							 "</filter>" +
						"</entity>" +
					"</fetch>";

		// Layout of subgrid.
		var LayoutXml = "<grid name='resultset' object='8' jump='avt_st_pi' select='1' preview='1' icon='1'>" +
						" <row name='result' id='avt_pristopnaizjava'>" +
								"<cell name='avt_st_pi' width='100' />" +
								"<cell name='avt_skladid' width='200' />" +
								"<cell name='avt_datumpristopa' width='100' />" +
								"<cell name='avt_svetovalecid' width='100' />" +
							"</row>" +
					 "</grid>";

		//apply layout and filtered fetchXML
		objSubGrid.control.SetParameter("layoutXml", LayoutXml);
		objSubGrid.control.SetParameter("fetchXml", FetchXml);
		//document.getElementById("relatedactivities").control.SetParameter("fetchXML", fetchXml);
		//Xrm.Page.ui.controls.get("relatedactivities").refresh();

		//Refresh grid to show filtered records only. 
		objSubGrid.control.Refresh();

		}


		if (account != null && contact == null)
		{
		Xrm.Page.ui.controls.get("statements").setVisible(true);
		var accountid = account[0].id;

		//Create FetchXML for sub grid to filter records based on account
		var FetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
						"<entity name='avt_pristopnaizjava'>" +
							"<attribute name='avt_st_pi'/>" +
							"<attribute name='avt_skladid'/>" +
							"<attribute name='createdon'/>" +
							"<order descending='false' attribute='avt_st_pi'/>" +
							"<filter type='and'>" +
								"<condition attribute='statecode' value='0' operator='eq'/>" +
								"<condition attribute='avt_vlagatelj' operator='eq' value='" + accountid + "' />" +
							 "</filter>" +
						"</entity>" +
					"</fetch>";

		// Layout of subgrid.
		var LayoutXml = "<grid name='resultset' object='8' jump='avt_st_pi' select='1' preview='1' icon='1'>" +
						" <row name='result' id='avt_pristopnaizjava'>" +
								"<cell name='avt_st_pi' width='100' />" +
								"<cell name='avt_skladid' width='200' />" +
								"<cell name='createdon' width='100' />" +
							"</row>" +
					 "</grid>";

		//apply layout and filtered fetchXML
		objSubGrid.control.SetParameter("layoutXml", LayoutXml);
		objSubGrid.control.SetParameter("fetchXml", FetchXml);

		//Refresh grid to show filtered records only. 
		objSubGrid.control.Refresh();

		}


		if (account == null && contact == null)
		{
		Xrm.Page.ui.controls.get("statements").setVisible(false);
		}
    }
    catch(e)
    {
    }
}

以上是关于javascript 动态过滤表单上的子网格的主要内容,如果未能解决你的问题,请参考以下文章

如何通过文本框过滤数据表视图中的子表单? #likeoperator #where 子句

按日期字段过滤子表单

穿梭列上的 Oracle APEX 交互式网格过滤器 APEX 19.2

多值关联过滤

通过javascript检索网格中的fetchxml

对子表单应用过滤器并根据过滤器打开报表