简体   繁体   English

仅使用 javascript 和 html 为 json API 创建搜索栏?

[英]Create search bar for json API using just javascript and html?

I am trying to create a search bar that will show the names of schools that I've gotten from an Open Data API when their name is typed.我正在尝试创建一个搜索栏,当输入学校名称时,该搜索栏将显示我从开放数据 API 获得的学校名称。

API:应用程序接口:

[ {
  "address" : "123 Street",
  "name" : "First School ....",
  "website" : {
    "url" : 
       "http://......."
  }
}
, {
  "address" : "123 Bay",
  "name" : "Second School ....",
  "website" : {
    "url" : 
       "http://......."
  }
}   ....etc 

What I want is if I input the word "first" in my search bar just First School will appear.我想要的是,如果我在搜索栏中输入“第一”这个词,就会出现第一所学校。

So far in my html when I click the Search button it changes my URL but every school name is always showing.到目前为止,在我的 html 中,当我单击“搜索”按钮时,它会更改我的 URL,但始终显示每个学校名称。 Im not sure what my next step is ... ?我不确定我的下一步是什么......?

Thanks for your time.谢谢你的时间。

My .js file:我的 .js 文件:

let name = 'name'; 
const api = 'https...api website... .json' +
                `$where=name LIKE '%${name}%'` +
                '&$order=name';
const url = encodeURI(api);

document.addEventListener("DOMContentLoaded",load);

function load(){

    fetch('https:....api website... .json')
        .then(function(result) {
            return result.json(); 
        })
        .then(function(data) {  
            listSchools(data);
        });
}

function listSchools(schoolData){

    let body = document.getElementsByTagName("body")[0];
    let footer = document.getElementsByTagName("footer")[0];
    let input = document.getElementById('input');   //id from HTML

    for(let i = 0; i<schoolData.length; i++){
        let keys = Object.keys(schoolData[i]);
        let values = Object.values(schoolData[i]);

        let section = document.createElement("section");
        let h1 = document.createElement("h1");
        let ul = document.createElement("ul");

        h1.innerHTML = `${schoolData[i].name}`;
        section.appendChild(h1);
        section.appendChild(ul);
        body.insertBefore(section, footer); 
    }
  }

You can do something like this: You can take the filter term on click of search button and search for it in the html h1 tags.您可以执行以下操作:您可以通过单击搜索按钮获取过滤器术语并在 html h1 标签中搜索它。 If you do find it then return with the first match and append it to the document.如果您确实找到了它,则返回第一个匹配项并将其附加到文档中。

 let body = document.getElementsByTagName("body")[0];
    let footer = document.getElementsByTagName("footer")[0];
    let input = document.getElementById('input');   //id from HTML
    let button = document.getElementById('btn'); //search button
        
    button.addEventListener('click',() => filterList(input.value));
 
    function listSchools(schoolData){
    for(let i = 0; i<schoolData.length; i++){
        let keys = Object.keys(schoolData[i]);
        let values = Object.values(schoolData[i]);

        let section = document.createElement("section");
        let h1 = document.createElement("h1");
        let ul = document.createElement("ul");

        h1.innerHTML = `${schoolData[i].name}`;
        section.appendChild(h1);
        section.appendChild(ul);
        body.insertBefore(section, footer); 
    }
   }
    
      function filterList(filterTerm){
        const h1Tags = document.querySelectorAll("h1");
           let filteredh1;
           for(const h1 of h1Tags){
           if (h1.innerText.toLowerCase().includes(filterTerm.toLowerCase())){
               document.body.innerHTML = '';
               filteredh1=h1
               break;
           }
        }
           if(filteredh1 !== undefined){
             let section = document.createElement("section");
             let newh1= document.createElement("h1");
             let newul= document.createElement("ul");
             newh1.innerHTML = filteredh1.textContent
             section.appendChild(newh1);
             section.appendChild(newul);
             body.appendChild(input);
             body.appendChild(button);
             body.insertBefore(section, footer); 
           }
           else {
             let errorMessage = document.createElement("h1")
             errorMessage.innerHTML = "Not found"
             body.appendChild(errorMessage)
           }
       
       }

  

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

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