简体   繁体   English

在等待选项时显示加载

[英]show loading while waiting for options

I have an dropdown list that gets it's info from a Google Sheets doc.我有一个下拉列表,可以从 Google 表格文档中获取信息。 The thing is it takes a little while to fetch the data so rather than waiting for the dropdown to change, I want to show loading in the dropdown list.问题是获取数据需要一些时间,所以我不想等待下拉列表更改,而是想在下拉列表中显示加载。

Here's my code:这是我的代码:

function showMediaDropdownMenuOptions(el, arrayOfArrays, index) {
    var serviceTypeElement = document.getElementById("service-type").value;
    var currentlyAdded = []
    el.innerHTML = '';
    var filteredArray = arrayOfArrays.filter(function(r) {return r[17].includes(serviceTypeElement)})
    var result = filteredArray.flatMap(({ 0: key, 17: values }) => values
            .split(', ')
            .map(value => [key, value])
        );
        const mappedArray = result.map(item => item[0].toString().replace(/\s*\,\s*/g, ",").split(',').sort())
        const sets = new Set([].concat(...mappedArray))
        const arrayValues = Array.from(sets)
    var option = document.createElement("option");
    arrayValues.forEach(function(r){
        var option = document.createElement("option");
        option.textContent = r;
        el.appendChild(option);
    });
  }

When I'm waiting for a whole page it'd use this at the end of a function.当我等待整个页面时,它会在 function 的末尾使用它。

document.getElementById("loading").remove();

With this html:有了这个 html:

 <div id="loading" class="loading pt-40">
      <div class="d-flex justify-content-center">
        <div>
          <div class="spinner-border text-light" style="width:4em; height:4em;" role="status">
            <span class="sr-only">Loading...</span>
          </div>
          <div>Loading...</div>
        </div>
      </div>
    </div>

And this css:而这个 css:

.loading {
  background-color:black;
  width: 100vw;
  height: 100vh;
  position:fixed;
  top:0;
  left:0;
  z-index:10000;
}

I don't know how to do it with a dropdown list especially as the contents come from an array?我不知道如何使用下拉列表来做到这一点,尤其是当内容来自数组时?

To display loading, at the start of the function, create the loading icon node in javascript.显示加载,在function的开头,在javascript创建加载图标节点。

 const loading = document.createElement("div") function inner(){ const inner = document.createElement("div") inner.classList.add("spinner-border text-light") inner.style = "width:4em; height:4em" return inner } function subInner (){ const subInner = document.createElement("div") const text = document.createElement("div") text.textContent = "Loading..." subInner.appendChild(inner()) subInner.appendChild(text) return subInner } loading.id = loading loading.classList.add = "loading pt-40" loading.appendChild( document.createElement("div").classList.add("d-flex justify-content-center").appendChild(subInner()) )

After the node are appending you can remove the loading node.附加节点后,您可以删除加载节点。

To append items to list: You can just append the elements to a parent container.将 append 个项目列出来:您可以只将 append 个元素添加到一个父容器中。 In your code you already create the option elements, so all you need to add is one line appending the options to a parent container div.在您的代码中,您已经创建了选项元素,因此您只需添加一行,将选项附加到父容器 div。

 const parent = document.getElementById("dropdown-list") arrayValues.forEach(function(r){ var option = document.createElement("option"); option.textContent = r; el.appendChild(option); //add to a parent container parent.appendChild(el) });
 <div id="dropdown-list"> </div

As a note though, if it really takes time for the items to load, you may want to read up on javascript promises and asynchronous requests, just so don't block the user interface while the elements are created.但请注意,如果加载项目确实需要时间,您可能需要阅读 javascript 承诺和异步请求,只是不要在创建元素时阻塞用户界面。

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

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