Dynamics 365 on-premises V9.1 NavigaiteTo
打开对话框
详情参考微软官方文档navigateTo (Client API reference) 此文章介绍其中一种,点击窗体按钮,打开webresource。 在command里添加js操作,执行以下js。
var pageInput = {
pageType: "webresource",
webresourceName: "tec_TaskApprove.html",
data: "typename=" + Xrm.Page.data.entity.getEntityName() + "&entityid=" + Xrm.Page.data.entity.getId().replace("{", "").replace("}","")
};
var navigationOptions = {
target: 2,
width: 400,
height: 400,
position: 1
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
function success() {
},
function error() {
}
);
参数介绍:
- pageType: String Specify “webresource”.
- webresourceName String The name of the web resource to load.
- data String (Optional) The data to pass to the web resource.
关闭层
微软官方目前未提供关闭dialog的方法,只能通过操作DOM执行click事件。
parent.$("button[data-id='dialogCloseIconButton']", parent.document).click();
获取传参
将参数名称传入此方法即可
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = decodeURIComponent(vars[i].split("=")[1]).split('&');
for (var i in pair) {
if (pair[i].indexOf(variable) != -1) {
return pair[i].split('=')[1];
}
}
console.log(pair);
}
return (false);
}
刷新父页面数据
比如在dialog中操作修改了父页面的数据,那如何自动刷新呢?直接调用Xrm.Page.data.refresh();是不行的,既然在dialog中调用不到,那可不可以操作parent的方法呢,答案是可以的。 首先在父级页面加入一个方法,方式可以自定义一个JS,在窗体库中添加这个JS。
parent.shuaxin = function () { Xrm.Page.data.refresh(); };
这时,在dialog中直接调用即可。
parent.shuaxin();
获取父页面formContext
因data只能传string类型,把上下文传到Dialog肯定是不行的。不过也是可以通过调用父级页面方法来获取。
Contoso.CustomerPlan = (function () {
return {
OnLoad: function (ExecutionContext) {
var formContext = ExecutionContext.getFormContext();
Contoso.CustomerPlan.formContext = formContext;
}
}
})();
parent.getContext = function () {
return Contoso.CustomerPlan.formContext;
}
接收:直接在html页面调用此方法就行。
var formContext = parent.getContext();
完整示例
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script type="text/javascript">
function ClickFun() {
debugger;
var entityid = getQueryVariable("entityid");
var opinion = document.getElementById('value').value;
var entity = {};
entity.tec_approveopinion = opinion;
var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/tec_tasks(" + entityid + ")", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
Xrm.Utility.alertDialog("审批成功", function refershFun() {
debugger;
parent.shuaxin();
parent.$("button[data-id='dialogCloseIconButton']", parent.document).click();
});
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = decodeURIComponent(vars[i].split("=")[1]).split('&');
for (var i in pair) {
if (pair[i].indexOf(variable) != -1) {
return pair[i].split('=')[1];
}
}
console.log(pair);
}
return (false);
}
</script>
</head>
<body style="margin-left:100px">
<h3>审批意见</h3>
<input id="value" type="text" name="name" value="" placeholder="请输入审批意见" /><br /><br />
<button onclick="ClickFun()">审批</button>
</body>
</html>
Ribbon
function approveFun()
{
var pageInput = {
pageType: "webresource",
webresourceName: "tec_TaskApprove.html",
data: "typename=" + Xrm.Page.data.entity.getEntityName() + "&entityid=" + Xrm.Page.data.entity.getId().replace("{", "").replace("}","")
};
var navigationOptions = {
target: 2,
width: 400,
height: 400,
position: 1
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
function success() {
},
function error() {
}
);
}
function approveButtonDisplay()
{
return true;
}
窗体JS
parent.shuaxin = function () { Xrm.Page.data.refresh(); };
Contoso.CustomerPlan = (function () {
return {
OnLoad: function (ExecutionContext) {
var formContext = ExecutionContext.getFormContext();
Contoso.CustomerPlan.formContext = formContext;
}
}
})();
parent.getContext = function () {
return Contoso.CustomerPlan.formContext;
}
|