简体   繁体   English

Dynamics CRM表单的客户端大小操作

[英]Client-size manipulation of a Dynamics CRM Form

ORIGINAL QUESTION: 原始问题:

I am trying to create some javascript to add to a form in Microsoft Dynamics CRM. 我正在尝试创建一些JavaScript,以添加到Microsoft Dynamics CRM中的表单。

I have the following script which I have assigned to the forms onLoad event: 我有以下已分配给表单onLoad事件的脚本:

$(document).ready(function () {

    if ($('#CheckBox1').is(':checked')) {

        $('<div id="div2">Some Data Here</div>').insertAfter("#Div1");

        $('#divHeader').height('+=25px');

        var newtop = $('#divMain').position().top + 25;
        $('#divMain').css('top', newtop + 'px');
    }

});

The following is a stripped down version of the forms HTML 以下是表格HTML的精简版

<div id="divHeader">
    <div id="Div1"></div>
</div>

<div id="divMain"></div>

When the form loads, what should happen is this: 加载表单时,将发生以下情况:

<div id="divHeader">
    <div id="Div1"></div>
    <div id="Div2">Some Data Here</div>
</div>

<div id="divMain"></div>

That does happen. 确实发生了。 However, the problem is that the divHeader and divMain do not resize, so the newly added Div2 can't be seen unless the user scrolls down within the divHeader . 然而,问题是, divHeaderdivMain不调整,那么新加入的Div2都看不出来,除非用户滚动内上下divHeader

If I add an alert: 如果我添加警报:

$(document).ready(function () {

    if ($('#CheckBox1').is(':checked')) {

        alert("Random alert");

        $('<div id="div2">Some Data Here</div>').insertAfter("#Div1");

        $('#divHeader').height('+=25px');

        var newtop = $('#divMain').position().top + 25;
        $('#divMain').css('top', newtop + 'px');
    }

});

The whole thing works perfectly. 整个过程运行完美。 How do I get this to work without the alert? 如何在没有警报的情况下使它正常工作?


UPDATE 1: 更新1:

setTimeout also works instead of using an alert . setTimeout也可以代替alert使用。

$(document).ready(function () {

    setTimeout(function () {
        if ($('#CheckBox1').is(':checked')) {

            $('<div id="div2">Some Data Here</div>').insertAfter("#Div1");

            $('#divHeader').height('+=25px');

            var newtop = $('#divMain').position().top + 25;
            $('#divMain').css('top', newtop + 'px');

        }
    }, 5000);

});

So it seems $(document).ready doesn't seem to do it's job properly. 因此,似乎$(document).ready似乎无法正常工作。 In both cases alert or setTimeout , the form gets extra time to finish loading before the div tags are resized. 在这两种情况下, alertsetTimeout都将在调整div标签大小之前获得额外的时间来完成加载。

It is entirely unsupported to do DOM manipulations in Dynamics CRM. 在Dynamics CRM中完全不支持执行DOM操作。 See Do Not Access the DOM . 请参阅不访问DOM

You will instead need to use supported methods of manipulating the form (ie Xrm.Page ). 相反,您将需要使用支持的方式来处理表单(即Xrm.Page )。 While it is not possible to dynamically add/remove content on a CRM-form, you can show and hide elements. 虽然无法在CRM表单上动态添加/删除内容,但是您可以显示和隐藏元素。 As I understand it, you are trying to show some content if a boolean field (presented as a checkbox) on the form is true. 据我了解,如果表单上的布尔字段(表示为复选框)为true,则您尝试显示一些内容。

This can be done by adding the control that you want to show/hide to your form (that control can either be one of the standard CRM-fields or a custom web resource, depending on your requirements). 这可以通过将要显示/隐藏的控件添加到表单中来完成(该控件可以是标准CRM字段之一,也可以是自定义Web资源,具体取决于您的要求)。 You would then write a bit of JavaScript to show/hide the control in question when your boolean attribute changes its value: 然后,当您的布尔属性更改其值时,您将编写一些JavaScript来显示/隐藏有问题的控件:

function onload() {
    Xrm.Page.getAttribute("booleanField").addOnChange(showMoreStuffInHeader);
    showMoreStuffInHeader();
}

function showMoreStuffInHeader() {
    var visible = Xrm.Page.getAttribute("booleanField").getValue();
    Xrm.Page.getControl("someDataHereControl").setVisible(visible);
}

Use Jquery Plugin Wait until element exists Pluging will wait for element to appear in DOM and then fire given handler function. 使用Jquery插件等待直到元素存在插件将等待元素出现在DOM中,然后触发给定的处理函数。

$(document).ready(function () {

    $('#CheckBox1').waitUntilExists(function () { //  OR $('#Div1').waitUntilExists(function ()
        if ($('#CheckBox1').is(':checked')) {

            $('<div id="div2">Some Data Here</div>').insertAfter("#Div1");

            $('#divHeader').height('+=25px');

            var newtop = $('#divMain').position().top + 25;
            $('#divMain').css('top', newtop + 'px');

        }
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM