Javascript自动完成功能不适用于REST API数组

时间:2020-01-22 11:39:09

标签: javascript

我在页面上运行了一个自动完成脚本,可以更轻松地搜索400多个名称,但是名称列表每隔几周就会添加或删除一些项。

为了使此操作更容易跟上,我想使用REST API调用来调用列表,以返回在文档加载时运行的这些名称的数组。

我有一个for循环,当返回此列表时将运行该循环,并且仅选择不带任何元数据的名称,并将其添加到第二个数组中,当提供完成建议时,自动完成功能应查看​​该数组。

我已经成功完成了REST API调用并将所有内容添加到功能数组中,但是在文本框中键入内容时,自动完成不会返回任何建议。

请注意,自动完成代码已从w3的Countries of the World autocomplete example中提取出来,并且此代码适用于已定义的数组,而不是调用返回的JSON数组。

   <form autocomplete="off" action="/action_page.php">
   <div class="autocomplete" style="width:200px;">
    <input id="myInput" type="text" name="mySystem" placeholder="Please Enter System Name...">
   </div>
var countries = [ ];
document.onload = getApplications();
function getApplications(){
  $.ajax({
    url: "URL/_api/web/lists/getbytitle('Application List')/items?$select=Title&$top=1000",
    type: "GET",
    headers: {"accept": "application/json;odata=verbose"},
    success: function (data) {
    var appresults = data.d.results;
    for(var obj in appresults){
      var app = appresults[obj].Title;
      countries = countries.concat(app);
    }
    console.log(countries);
    }
  });
}

function autocomplete(inp, arr) {
  var currentFocus;
  inp.addEventListener("input", function(e) {
      var a, b, i, val = this.value;
      closeAllLists();
      if (!val) { return false;}
      currentFocus = -1;
      a = document.createElement("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items");
      this.parentNode.appendChild(a);
      for (i = 0; i < arr.length; i++) {
        if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
          b = document.createElement("DIV");
          b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
          b.innerHTML += arr[i].substr(val.length);
          b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
          b.addEventListener("click", function(e) {
              inp.value = this.getElementsByTagName("input")[0].value;
              closeAllLists();
          });
          a.appendChild(b);
        }
      }
  });
  inp.addEventListener("keydown", function(e) {
      var x = document.getElementById(this.id + "autocomplete-list");
      if (x) x = x.getElementsByTagName("div");
      if (e.keyCode == 40) {
        currentFocus++;
        addActive(x);
      } else if (e.keyCode == 38) { //up
        currentFocus--;
        addActive(x);
      } else if (e.keyCode == 13) {
        e.preventDefault();
        if (currentFocus > -1) {
          if (x) x[currentFocus].click();
        }
      }
  });
  function addActive(x) {
    if (!x) return false;
    removeActive(x);
    if (currentFocus >= x.length) currentFocus = 0;
    if (currentFocus < 0) currentFocus = (x.length - 1);
    x[currentFocus].classList.add("autocomplete-active");
  }
  function removeActive(x) {
    for (var i = 0; i < x.length; i++) {
      x[i].classList.remove("autocomplete-active");
    }
  }
  function closeAllLists(elmnt) {
    var x = document.getElementsByClassName("autocomplete-items");
    for (var i = 0; i < x.length; i++) {
      if (elmnt != x[i] && elmnt != inp) {
        x[i].parentNode.removeChild(x[i]);
      }
    }
  }
  document.addEventListener("click", function (e) {
      closeAllLists(e.target);
  });
}

autocomplete(document.getElementById("myInput"), countries);

0 个答案:

没有答案