简体   繁体   English

Arrays 将数据打印到一个 div 中,而不是循环每个 div,

[英]Arrays printing data into one div rather than looping each one,

Hoping you guys can help me, I have been playing around with brewdogs api called punk api.希望你们能帮助我,我一直在玩 brewdogs api,称为朋克 api。
I have built a search function but whenever the user searches for a word that returns multiple beers it prints them all at once.我已经建立了一个搜索 function 但是每当用户搜索返回多个啤酒的单词时,它都会一次打印它们。
Is there anyway to print one title, one tagline, one description, one abv, one pic and then repeat til all have been printed?无论如何要打印一个标题,一个标语,一个描述,一个abv,一张图片,然后重复直到全部打印完?
It is currently just printing every title in one div then every tag line in one div and so on.它目前只是在一个 div 中打印每个标题,然后在一个 div 中打印每个标签行,依此类推。
I have also attached an image to show the results when searched.我还附上了一张图片来显示搜索时的结果。

第二期

function beerAsk(e) {
  clear();
  const beerage = document.getElementById("BeerInputSearch").value;
  const url = api + beerage;
  e.preventDefault()
  fetch(url)
    .then(reponse => {
      return reponse.json()

    })
    .then(data => {

      data.forEach((beer) => {
        const theId = beer.id;
        const theBeer = beer.name;
        const theTagline = beer.tagline;
        const theDescription = beer.description;
        const theAbv = beer.abv;
        const thePic = beer.image_url;

        const nopic = document.getElementById('imageSearch').src = "https://images.punkapi.com/v2/keg.png"

        function imgDisplay() {
          if (thePic === null) {
            return nopic;
          } else {
            return thePic;
          }
        }
        // this is the bit that needs the work im guessing
        searchBeer.innerHTML += theBeer
        searchBeerTagline.innerHTML += theTagline
        searchBeerAbv.innerHTML += "ABV: " + theAbv + "%"
        searchBeerDescription.innerHTML += theDescription
        document.getElementById('imageSearch').src = imgDisplay();

      })

    })
}

In your code, you asked to put all titles in the same div indeed:在您的代码中,您确实要求将所有标题放在同一个 div 中:

// this is the bit that needs the work im guessing
searchBeer.innerHTML += theBeer
searchBeerTagline.innerHTML += theTagline
searchBeerAbv.innerHTML += "ABV: " + theAbv + "%"
searchBeerDescription.innerHTML += theDescription

Instead, you should use as many searchBeer as beer in data response.相反,您应该在数据响应中使用尽可能多searchBeerbeer One popular technique is to use Template, like Reusable HTML components.一种流行的技术是使用模板,例如可重复使用的 HTML 组件。 There are some package for that.为此,有一些 package。 Here is a basic reusable component, let's say you have a search result div and at the bottom this component (HTML group of elements that are doing the same thing) in your HTML body:这是一个基本的可重用组件,假设您有一个搜索结果 div,并且在您的 HTML 主体的底部有这个组件(执行相同操作的 HTML 组元素):

<div id="search-result" ></div>

And later at the bottom:后来在底部:

<div class="beer-component" style="display:none;" >
  <div class="beer-title" ></div>
  <div class="beer-tagline" ></div>
  <div class="beer-abv" ></div>
  <div class="beer-description" ></div>
  <div class="beer-image" ><img alt="beer" /></div>
</div>

Now you need to clone the component multiple times, add values to them and append each component in search result container.现在您需要多次克隆组件,向它们添加值,并在搜索结果容器中为每个组件添加 append。

EDIT --------------编辑 - - - - - - -

When you submit a new search, the function codes will be interpreted again.当您提交新搜索时,将再次解释 function 代码。 So if you don't clear the search result container well, you will have your title to add up continuously because of += , titleBeer.innerHTML += theBeer .因此,如果您没有很好地清除搜索结果容器,您的标题就会因为+=titleBeer.innerHTML += theBeer而不断累加。 You need to get rid of it and put data directly titleBeer.innerHTML = theBeer .您需要摆脱它并将数据直接titleBeer.innerHTML = theBeer Now the reason that might happening (cause I don't know what your function clear() does), is that when selecting component template:现在可能发生的原因(因为我不知道您的 function clear()做了什么),是在选择组件模板时:

const beerComponent = document.getElementsByClassName("beer-component")[0];

before clearing the result container, the DOM return the first beer component that reside in search result container, instead of going at the bottom to take the template instead.在清除结果容器之前,DOM 返回位于搜索结果容器中的第一个啤酒组件,而不是从底部获取模板。 That 1st component already had data, hence, doing:第一个组件已经有数据,因此,做:

titleBeer.innerHTML += theBeer

the titles keep adding together, which is wrong, instead it should be:标题不断加在一起,这是错误的,应该是:

titleBeer.innerHTML = theBeer

So you can just clear the search results before reselecting the Template component.因此,您可以在重新选择模板组件之前清除搜索结果。 Here is the final code in this case:这是这种情况下的最终代码:

function beerAsk(e) {
  // clear(); NOT SURE WHAT IT DOES, BUT I CLEAR it below
  // new elements here
  const resultContainer = document.getElementById("search-result");
  // EDIT --- CLEAR Container first
  resultContainer.innerHTML = "";

  const beerComponent = document.getElementsByClassName("beer-component")[0];
  
  const beerage = document.getElementById("BeerInputSearch").value;
  const url = api + beerage;
  e.preventDefault()
  fetch(url)
    .then(reponse => {
      return reponse.json()

    })
    .then(data => {

      data.forEach((beer) => {
        const theId = beer.id;
        const theBeer = beer.name;
        const theTagline = beer.tagline;
        const theDescription = beer.description;
        const theAbv = beer.abv;
        const thePic = beer.image_url;

        const nopic = document.getElementById('imageSearch').src = "https://images.punkapi.com/v2/keg.png"

        function imgDisplay() {
          if (thePic === null) {
            return nopic;
          } else {
            return thePic;
          }
        }
        // this is the bit that needs the work im guessing
        // CHANGES HAPPEN HERE
        // Create a copy to be different DOM object from the original
        let component = beerComponent.cloneNode(true);

        let titleBeer = component.getElementsByClassName('beer-title')[0];
        let taglineBeer = component.getElementsByClassName('beer-tagline')[0];
        let abvBeer = component.getElementsByClassName('beer-abv')[0];
        let descriptionBeer = component.getElementsByClassName('beer-description')[0];
        let imgBeer = component.querySelector('.beer-image img'); // return one element

        component.style = "display: block;";
        // EDIT ---- change the += to =
        titleBeer.innerHTML = theBeer
        taglineBeer.innerHTML = theTagline
        abvBeer.innerHTML = "ABV: " + theAbv + "%"
        descriptionBeer.innerHTML += theDescription
        imgBeer.src = imgDisplay();
        imgBeer.alt = theBeer;
        
        // Now append the component in Search Result container
        resultContainer.appendNode(component);
      })

    })
}

And for the rest you would format the component with CSS to fit your design, or just use existing component HTML codes from a Template with CSS ready. And for the rest you would format the component with CSS to fit your design, or just use existing component HTML codes from a Template with CSS ready.

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

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