简体   繁体   English

Dynamics CRM - 如何在 Javascript Web 资源中使用 JQuery

[英]Dynamics CRM - How to use JQuery in a Javascript Web ressource

I have a script that is launched in ribbon command in order to perform an action but it's written in simple javascript.我有一个在功能区命令中启动的脚本以执行操作,但它是用简单的 javascript 编写的。 As I would like to improve it, I would like to use JQuery instead.因为我想改进它,所以我想改用 JQuery。 The script is located in a solution that is uploaded in solutions, should I add the jquery file inside the solution ?该脚本位于解决方案中上传的解决方案中,我应该在解决方案中添加 jquery 文件吗? How can I use it ?我怎样才能使用它?

您应该能够像这样访问 jQuery:

$ = ($ || parent.$);

All of the answers are correct so far but I wanted to add my input.到目前为止,所有答案都是正确的,但我想添加我的输入。

For starters, we may be touching some unsupported customization here but that wasn't the question.首先,我们可能会在这里接触一些不受支持的自定义,但这不是问题。

If you're running the script in context of a view, you cannot add jquery directly.如果您在视图上下文中运行脚本,则不能直接添加 jquery。 The only way of accomplishing this is to load the file as an object from the resource url via your original script and append it to the head of the page.完成此操作的唯一方法是通过原始脚本从资源 url 中将文件作为对象加载并将其附加到页面的头部。 Then have an on-load that waits to execute the remaining script.然后有一个等待执行剩余脚本的加载。 This obviously is beyond what it should be and I advise against it.这显然超出了它应有的范围,我建议不要这样做。 You're better off dropping jquery altogether in this scenario.在这种情况下,您最好完全放弃 jquery。

Lastly, if you're running it in context of the form (on the form), you can add the jquery to the entity's form as a normal script.最后,如果您在表单的上下文中(在表单上)运行它,您可以将 jquery 作为普通脚本添加到实体的表单中。 Depending upon which form rendering engine you're using, you may need to do what Alex suggested and set $ = $ || parent.$;根据您使用的表单渲染引擎,您可能需要按照亚历克斯的建议进行操作并设置$ = $ || parent.$; $ = $ || parent.$; . . If using the new turbo forms introduced in the newer versions, you will have to do this.如果使用较新版本中引入的新涡轮形式,则必须执行此操作。 Aside from that, you shouldn't have any problems using it from your ribbon.除此之外,从功能区使用它应该没有任何问题。

Adding JQuery to your solution will add it as a resource.将 JQuery 添加到您的解决方案会将其添加为资源。 That alone will not allow you to execute it wherever you like.仅凭这一点,您就无法在任何您喜欢的地方执行它。

When you want to use jQuery in your ribbon command, to make 100% sure jQuery is available, you should add an additional Custom Action in your RibbonCommand which takes place before your actual Custom Action in which your javascript is defined.当您想在您的功能区命令中使用 jQuery 时,为了 100% 确定 jQuery 可用,您应该在 RibbonCommand 中添加一个额外的自定义操作,该操作发生定义您的 javascript 的实际自定义操作之前

In this Custom Action you call the function isNaN on the jQuery webresource (which you also need to include).在此自定义操作中,您在 jQuery 网络资源(您还需要包含)上调用函数isNaN

Assuming you're using the Ribbon Workbench (which you probably should if you don't :-)), it would look like this:假设您正在使用功能区工作台(如果您不使用,您可能应该使用 :-)),它看起来像这样:

在此处输入图片说明

Using this method you are sure that jQuery will be available, no matter what context you use (Forms, Grids, Subgrids).使用此方法,您可以确定 jQuery 将可用,无论您使用什么上下文(表单、网格、子网格)。

If you do not actually add it like this, you need to rely on $ ||如果你真的没有像这样添加它,你需要依靠 $ || parent.$, which works in 99% of the cases, but is not guaranteed to work. parent.$,在 99% 的情况下都有效,但不能保证有效。 The solution mentioned by @Domenico will work, but only when the button is shown on Forms and the jQuery library is loaded before your custom code. @Domenico 提到的解决方案将起作用,但仅当按钮显示在 Forms 上并且jQuery 库在您的自定义代码之前加载时。

Add the JQuery Library to CRM将 JQuery 库添加到 CRM

  1. You can download the latest verison of JQuery from http://jquery.com/ .您可以从http://jquery.com/下载最新版本的 JQuery。

    1. After you have downloaded the JQuery you will want to add it to CRM as a JScript Web resource.下载 JQuery 后,您需要将它作为 JScript Web 资源添加到 CRM。
    2. Open your Solution or open the Default Solution by going to Settings > Custmoizations > Customize the System.打开您的解决方案或通过转到设置 > 自定义 > 自定义系统打开默认解决方案。
    3. Enter a name and Select Type: Script (JScript).输入名称并选择类型:脚本 (JScript)。 I recomend also entering the JQuery file name in the descript so you know what version is installed.我建议还在描述中输入 JQuery 文件名,以便您知道安装了哪个版本。

    4. Click Browse and Select the JQuery file you just downloaded.单击浏览并选择您刚刚下载的 JQuery 文件。

    5. Save and Publish the new Web Resource.保存并发布新的 Web 资源。

    6. You can now add the JQuery Library to your Forms so you can use it from your other Web Resources.您现在可以将 JQuery 库添加到您的表单中,以便您可以从其他 Web 资源中使用它。

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

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