简体   繁体   English

javascript遍历数组显示详细信息-遍历第二个URL显示第二个数组的详细信息

[英]javascript loop through array display detail - loop through a second array display details of 2nd url

I'm looking to build an app that searches for a person by name - I have achieved this part - and displays the person name. 我正在寻找一个按名称搜索人的应用程序-我已经完成了这一部分-并显示人的名字。 The second part of the app should also present more details for each person on click (this information should be retrieved from a second URL. Lastly the app should also inject an image when I click on that person. I haven't been able to make this work. Can you guy help fix this or shine a light? 该应用程序的第二部分还应为每个单击的人提供更多详细信息(应从第二个URL检索此信息。最后,当我单击该人时,该应用程序还应注入一个图像。这项工作,您可以帮忙解决此问题或发光吗?

I have commented out what I have been trying to do. 我已注释掉我一直在尝试做的事情。

https://codepen.io/edmonteiro/pen/qKeMLj https://codepen.io/edmonteiro/pen/qKeMLj

document.getElementById("subBtn").onclick = function(event){
//prevent default submission
event.preventDefault();

//grab the typed value in the text box
var name = document.getElementsByClassName("name")[0].value;

//Alternatively give an "id" attribute to the text box in the HTML
//for example, <input type="text" name="textboxname" class="name">, use:
//var name = document.getElementById("textboxname").value();

let ourRequest = new XMLHttpRequest();

//pass this search value as a parameter in URL to get results from database
ourRequest.open('GET', 'https://izrite.com:5555/leads?page=1&name=' + name, true);

//function to call when the AJAX request is completed
ourRequest.onload = function(){
//if request completed successfully
if(ourRequest.status === 200){
  let ourData = JSON.parse(ourRequest.responseText);

  let list = document.getElementById("list");

  //process only if request returned a valid JSON data
  if(typeof ourData === 'object'){
    //if there are 1 or more results found
    if(ourData.length > 0){
      list.innerHTML = "<p><b>Matched names from database:</b></p>";

      //for each `lead` in the array, print its name
      for(lead of ourData){
        list.innerHTML += "<p>Name: " + lead.name + "</p>";
      /*-----------------------------------------------------------------*/
      /* Second and third part of the test - to be continued. Not finished before the 5hours time frame mentioned on the challenge specs*/
        // if((lead.id).value()==="0009bd06-a9ce-470e-b13a-acd2aaaa42d4"){
        //   let ourRequest2 = new XMLHttpRequest();
        //   ourRequest2.open('GET', 'https://izrite.com:5555/lead/0009bd06-a9ce-470e-b13a-acd2aaaa42d4', true);

        //   let moreDetails = document.getElementById('moreDetails');
        //   let ourData2 = JSON.parse(ourRequest2.responseText);
        //   if(typeof ourData2 === 'object'){
        //     //if there are 1 or more results found
        //     if(ourData2.length > 0){
        //       moreDetails.innerHTML = "<p><b>More details</b></p>";

        //       //for each `lead` in the array, print its name
        //       for(detl of ourData2){
        //         moreDetails.innerHtml += "<p>Name: " + detl.name + "</p><br><p>Make: " + detl.make + "</p><br><p>Model: " + detl.model + "</p><br><p>Derivative: " + detl.derivative + "</p>";

        //         // var myImage = new Image(100, 200);
        //         // myImage.src = 'https://izrite.com:5555/image/71890';
        //         // document.body.appendChild(myImage);
        //       }
        //     }
        //   }
        // }
        // ourRequest2.send();
      /*-----------------------------------------------------------------*/

      };
    }else{
      //no results found for this search query
      list.innerHTML = "<p><b>No match found in database!</b></p>";
    }
  }else{
    //response not in JSON format
    list.innerHTML = "<p><b>Response is not in valid JSON format!</b></p>";
  }
 }
}

 //send the AJAX request
 ourRequest.send();
}

The forEach callback takes the current value as the first parameter and index as the second. forEach回调将当前值作为第一个参数,将索引作为第二个参数。 Try: 尝试:

 ourData.forEach((element, index) => { document.getElementById("app").innerHTML = `<p>${element.name}</p>`; }); 

As far as I can understand your question, you want to type in the search box, hit "Search" and get the matched results from your database via AJAX. 据我了解您的问题,您想在搜索框中键入内容,点击“搜索”,然后通过AJAX从数据库中获取匹配的结果。

Here is the JavaScript that works: (Remove your javascript in your CodePen and paste this to see it working.) 这是可以使用的JavaScript :(将您的JavaScript删除到CodePen中,然后将其粘贴以查看其工作原理。)

document.getElementById("subBtn").onclick = function(event)
{
    //prevent default submission
    event.preventDefault();

    //grab the typed value in the text box
    //getElementsByClassName is used because in your HTML, the text box has no "id" attribute
    var name = document.getElementsByClassName("name")[0].value;

    //if you give an "id" attribute to the text box in your HTML
    //for example, <input type="text" name="textboxname" class="name">, use:
    //var name = document.getElementById("textboxname").value();

    let ourRequest = new XMLHttpRequest();

    //pass this search value as a parameter in URL to get results from database
    ourRequest.open('GET', 'https://izrite.com:5555/leads?page=1&name=' + name, true);

    //function to call when the AJAX request is completed
    ourRequest.onload = function()
    {
        //if request completed successfully
        if(ourRequest.status === 200)
        {
            let ourData = JSON.parse(ourRequest.responseText);

            var list = document.getElementById("list");

            //process only if request returned a valid JSON data
            if(typeof ourData === 'object')
            {
                //if there are 1 or more results found
                if(ourData.length > 0)
                {
                    list.innerHTML = "<p><b>Matched names from database:</b></p>";

                    //for each `lead` in the array, print its name and id
                    for(lead of ourData)
                    {
                        list.innerHTML += "<p>Name: " + lead.name + "<br />Id: " + lead.id + "</p>";
                    }
                }
                else
                {
                    //no results found for this search query
                    list.innerHTML = "<p><b>No match found in database!</b></p>";
                }
            }
            else
            {
                //response not in JSON format
                list.innerHTML = "<p><b>Response is not in valid JSON format!</b></p>";
            }
        }
    }

    //send the AJAX request
    ourRequest.send();
}

Try this ourData.forEach((element, index) => { document.getElementById("app").innerHTML += <p>${element.name}</p> ; }); 试试这个ourData.forEach((element,index)=> {document.getElementById(“ app”)。innerHTML + = <p>${element.name}</p> ;}); innerHTML += innerHTML + =

Add to the HTML on each cycle. 在每个周期添加到HTML。

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

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