简体   繁体   English

无法从 autocomplete.js 搜索结果中实际选择

[英]Unable to actually select from autocomplete.js search results

I am using autoComplete.js and have valid search results listing.我正在使用 autoComplete.js 并且有有效的搜索结果列表。 However, as the title states, I am unable to actually select any of the search results.但是,正如标题所述,我无法实际选择任何搜索结果。 The current code that I have is listed below.我拥有的当前代码如下所示。 I did not alter the autoComplete.js file.我没有更改 autoComplete.js 文件。 If anyone could help with this, I would sincerely appreciate it^^如果有人可以帮助解决这个问题,我将不胜感激^^

( if interested, full repo is at: https://github.com/shonb6570/Tech-Degree-Project-7 ) (如果有兴趣,完整的 repo 位于: https : //github.com/shonb6570/Tech-Degree-Project-7

Code from the index.js file: index.js 文件中的代码:

document.querySelector("#autoComplete").addEventListener("autoComplete", function (event) {
  console.log(event.detail);
});

const membersText = document.querySelectorAll('.members .members-container .members-text p');

let members = [];

membersText.forEach(memberText => {
  members.push(memberText.textContent.toLowerCase());
});

const autoCompletejs = new autoComplete({
  data: { 
    src: members,
  },
  resultsList: {                       
    render: true,
    container: source => {
        source.setAttribute("id", "user-names");
    },
    destination: document.querySelector("#autoComplete"),
    position: "afterend",
    element: "ul"
  },
  maxResults: 5,   
  highlight: true, 
  resultItem: {    
      content: (data, source) => {
          source.innerHTML = data.match;
      },
      element: "li"
  },
});

I found the answer myself (with help from a more advanced programmer).我自己找到了答案(在更高级的程序员的帮助下)。 I hope that this can be of help to someone else.我希望这可以对其他人有所帮助。 The "feedback" section needs to be set as follows to allow results to be selected and set as the value of the search input( making them appear in the search when clicked ). “反馈”部分需要如下设置,以允许选择结果并将其设置为搜索输入的值(使它们在单击时出现在搜索中)。

See appended code below:请参阅下面的附加代码:

document.querySelector("#autoComplete").addEventListener("autoComplete", function (event) {
  console.log(event.detail);
});

const membersText = document.querySelectorAll('.members .members-container .members-text p');

let members = [];

membersText.forEach(memberText => {
  members.push(memberText.textContent.toLowerCase());
});

const autoCompletejs = new autoComplete({
  data: { 
    src: members,
  },
  resultsList: {                      
    render: true,
    container: source => {
        source.setAttribute("id", "user-names");
    },
    destination: document.querySelector("#autoComplete"),
    position: "afterend",
    element: "ul"
  },
  maxResults: 5,   
  highlight: true, 
  resultItem: {    
      content: (data, source) => {
          source.innerHTML = data.match;
      },
      element: "li"
  },
  //added code
  onSelection: feedback => { 
    feedback.event.preventDefault();
    document.querySelector("#autoComplete").value = feedback.selection.value;
  }
});

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

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