javascript 使用 JScript 检索 crm2011 子网格中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4347083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:07:57  来源:igfitidea点击:

Retrieve rows in crm2011 subgrid with JScript

dynamics-crmjavascriptdynamics-crm-2011

提问by Robby Iven

As an JScript newbie, I have a problem with a subgrid in MS CRM 2011.

作为 JScript 新手,我遇到了 MS CRM 2011 中的子网格问题。

I have a form with a subgrid and in OnSave of that form, I want to loop over all the rows in the subgrid.

我有一个带有子网格的表单,在该表单的 OnSave 中,我想遍历子网格中的所有行。

How can I do this with JScript ? Or is it possible another way, ex plugin ?

我怎样才能用 JScript 做到这一点?或者是否有可能以另一种方式,例如插件?

Thx

谢谢

回答by Asim Sajjad

Here is the sample code which you can do on save of the form

这是您可以在保存表单时执行的示例代码

var gridControl = document.getElementById('grdrelatedcontacts').control;
for (var intRowNumber = 0; intRowNumber < gridControl.getRecordsFromInnerGrid().length; intRowNumber++)
    for (var intCellNumber = 0; intCellNumber < gridControl.getRecordsFromInnerGrid()[intRowNumber][3].cells.length; intCellNumber++)
        alert(gridControl.getRecordsFromInnerGrid()[intRowNumber][3].cells[intCellNumber].outerText);

回答by Mauro De Biasio

Use a Rest call and retrieve the corresponding records :S

使用 Rest 调用并检索相应的记录:S

回答by Moran Barzilay

You can do something like this:

你可以这样做:

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/pws_streedandhousenodatas?$filter=_pws_streetandhousenumberid_value eq " + Xrm.Page.data.entity.getId(), true);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var pws_streedandhousenodataid = results.value[i]["pws_streedandhousenodataid"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

In this case the Xrm.Page.data.entity.getId() get you your current record id and you are looking all the lookups (that are in the sub-grid), you can also add some fields to select more info from them.

在这种情况下, Xrm.Page.data.entity.getId() 获取您当前的记录 ID 并且您正在查找所有查找(在子网格中),您还可以添加一些字段以从中选择更多信息.

回答by Dan Rigby

You can inspect the subgrid values on save by doing the following:

您可以通过执行以下操作在保存时检查子网格值:

var gridControl = document.getElementById('subgrid_id').control;
var ids = gridControl.get_allRecordIds();
for(i = 0; i < ids.length; i++) {
    var cellValue = gridControl.getCellValue('column_name', ids[i]);
    // logic
}

Doing this on load is a bit more challenging since subgrids are loaded asynchronously and aren't likely to be done loading when the form onload event fires. You can check the grid periodically though to see when it's done loading by calling a function like the following in your form onload:

在加载时执行此操作更具挑战性,因为子网格是异步加载的,并且在表单 onload 事件触发时不太可能完成加载。您可以定期检查网格,但通过在 onload 表单中调用如下函数来查看它何时完成加载:

function subGridOnload() {
    var grid = document.getElementById('subgrid_id');
    if (grid.readyState!="complete") {
        // delay one second and try again.  
        setTimeout(subGridOnload, 1000);
        return;
    }

    // logic
}