简体   繁体   English

任务窗格中的 Office 插件切换 div(加载面板)不起作用

[英]Office Addin toggle div (loading panel) in taskpane not working

here is another issue I come accross.这是我遇到的另一个问题。 My AddIn is working fine, I only have three API call's and they all work.我的插件工作正常,我只有三个 API 调用,它们都工作。 However, two of these calls take some time to return the data.但是,其中两个调用需要一些时间才能返回数据。 To inform the users the process is still running I want to have a loading panel.为了通知用户进程仍在运行,我想要一个加载面板。 In my taskspane.html I have a div called:在我的taskspane.html我有一个名为:

<div id="loader" class="hideLoader"></div>

The div is styled using CSS. div 使用 CSS 设置样式。 I have added a class to the div which hides de DIV by default.我在默认情况下隐藏 de DIV 的 div 中添加了一个 class 。 I also have a class (showLoader) that has a style to show the DIV.我还有一个 class (showLoader),它具有显示 DIV 的样式。

.showLoader {
  visibility: visible;
  display: block;
}

.hideLoader {
  visibility: hidden;
  display: none;
}

Now when I execute a function that retrieves the data I have a function that toggles the classes.现在,当我执行检索数据的 function 时,我有一个切换类的 function。

function toggleLoader(display: string) {
  if (display == 'show') {
    $("#loader").removeClass("hideLoader");
    $("#loader").addClass("showLoader");
  }
  else {
    $("#loader").addClass("hideLoader");
    $("#loader").removeClass("showLoader");
  }    
}

When I debug the running AddIn, I can see the values for the DIV are set correctly, however, the DIV doesn't show.当我调试正在运行的插件时,我可以看到 DIV 的值设置正确,但是 DIV 没有显示。

One of the two functions that calls the toggelLoader-function:调用 toggelLoader 函数的两个函数之一:

 export async  function getMapImage() {
    return Word.run(async context => {

    toggleLoader('show');
  
    title = $("#title").val() as string;
    scale = $("#map-scale").val() as number;
    language = $("#language").val() as string;

    $("#error").text("");
    let fetchUrl: string;

    fetchUrl = mapUrl.replace("[title]",title)
    .replace("[scale]",scale.toString())
    .replace("[lng]",language)

    projectObject.Description = $("#project-description").val() as string;
    projectObject.Client.Name = $("#project-client").val() as string;

    // eslint-disable-next-line no-undef
    fetch(fetchUrl,
        { 
          method: 'POST',      
          headers: {
            "Content-type": "application/json; charset=UTF-8"
          },     
          credentials: 'include',
          body: JSON.stringify(projectObject)                      
        })
        .then(response => response.text())
        .then(data => insertMapImage(data))
        .catch((error) => {
          // eslint-disable-next-line no-undef
          console.error('Error:', error);
          $('#error').text("Error retreiving the map!");
        });
    
        await context.sync();

        toggleLoader('hide');
      });
    }

I don't know what is wrong with the code.我不知道代码有什么问题。 There are no javascript exceptions.没有 javascript 异常。

The AddIn is coded using TypeScript and generated using Yeoman插件使用 TypeScript 编码并使用Yeoman生成

UPDATE (after the comment from Rick)更新(在 Rick 发表评论之后)

The strange thing is I have got another function inside my taskpane.ts which does a similar thing, by similar I mean manipulate classes of an element (addClass and removeClass).奇怪的是,我的taskpane.ts中有另一个 function,它做了类似的事情,我的意思是操作元素的类(addClass 和 removeClass)。 This works fine.这工作正常。 When this function is executed from (also) a buttonclick , the class disabled is added and shows the element as disabled .当此 function 从(也)按钮单击执行时,将添加buttonclick disabled并将元素显示为disabled

 export async function clearProjectInformation(){
  return Word.run(async context =>{

    $("#insertMap").addClass("disabled");
    $("#insertTable").addClass("disabled");

    $("#project-nr").val("");
    $("#project-id").text("");
    $("#project-description").val("");
    $("#project-location").text("");
    $("#project-client").val("");
    $("#error").text("");

    await context.sync();
  }).catch(function (error) 
  {
    // eslint-disable-next-line no-undef
    if (error instanceof OfficeExtension.Error) {
      // eslint-disable-next-line no-undef
      console.log('Error code and message: ' + error.toString());
    }
  });
}

I finally found the solution.我终于找到了解决方案。 The behavior of the code was correct.代码的行为是正确的。 But because of the async nature of the function getMapImage() , the code at the end was executed right after the fetch wat initiated.但是由于function getMapImage()的异步特性,最后的代码在 fetch wat 启动后立即执行。 This happens so fast, that it was impossible to see if switching the loader-layer worked.这种情况发生得如此之快,以至于无法查看切换加载器层是否有效。 It did work according to my debugger information, but onscreen nothing seemed to happen.它确实根据我的调试器信息工作,但在屏幕上似乎没有发生任何事情。

The solution was to move the toggleLoader("hide") function from the end, to the promise code.解决方案是将toggleLoader("hide") function 从末尾移到 promise 代码。 My mistake was to not to think of the async way code execution works (I thought of the traditional procedure way code is executed).我的错误是没有想到异步代码执行方式(我想到了传统的过程方式代码执行)。 Pay attention to the extra .then() compared to the original function posted in the question:注意与问题中发布的原始 function 相比额外的 .then .then()

 export async  function getMapImage() {
    return Word.run(async context => {

      
    toggleLoader("show");

    title = $("#title").val() as string;
    scale = $("#map-scale").val() as number;
    language = $("#language").val() as string;

    $("#error").text("");
    let fetchUrl: string;

    fetchUrl = mapUrl.replace("[title]",title)
    .replace("[scale]",scale.toString())
    .replace("[lng]",language)

    projectObject.Description = $("#project-description").val() as string;
    projectObject.Client.Name = $("#project-client").val() as string;

    // eslint-disable-next-line no-undef
    fetch(fetchUrl,
        { 
          method: 'POST',      
          headers: {
            "Content-type": "application/json; charset=UTF-8"
          },     
          credentials: 'include',
          body: JSON.stringify(projectObject)                      
        })
        .then(response => response.text())
        .then(data => insertMapImage(data))
        .then((result) => {
          toggleLoader("hide");
        })
        .catch((error) => {
          // eslint-disable-next-line no-undef
          console.error('Error:', error);
          $('#error').text("Fout bij het ophalen van de kaart!");
          toggleLoader("hide");
        });
    
        await context.sync();   
      });
    }  

This question can be closed!这个问题可以关闭!

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

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