3

I am trying to call an API and display the data which is in the form of list, the problem I am facing is I am able to split the list and display it in HTML page, but I need to display appropriate label against the data. Any help in this regard is much appreciated. Thank you.

The data I got from API call is:

[[236,1,"1537890525704.jpg","","Residential","Rent",1200], 
[235,1,null,"","Residential","Rent",10000]]

The way I've displayed data on my HTML page is:

enter image description here

The way I want to display is appropriate label against the data, Ex:

Property Id : 236
No. of Bedrooms: 1 
Property Type : Residential  

and so on..

client.html

<html>
    <head>
        <title>Test client application</title>
             <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
              <script src="cleint.js"></script> 
             <style>
                .card {
                    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
                    transition: 0.3s;
                    width: 40%;
                }

                .card:hover {
                    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
                }

                .container {
                    padding: 2px 16px;
                }
                </style>

    </head>

    <body>


        <p id="msgs" class="card container"></p>

client.js

$(document).ready(function() {   
    $.ajax({
       url: "http://192.168.1.5:8000/fetch",
       success: function(data){        
          var c=data;
         }
    }).then(function(data) {
       var text = "";
       var res = "";
       var i;
       for(var i=0; i<data.length; i++){
        var prop = data[i];
           for(var j=0;j<prop.length;j++){
            $('#msgs').append($('<div>').text(prop[j]));

           }

       }



    });

    function display(str) {
        $('#msgs').append($('<div>').text(str));
     }

    });

2 Answers 2

2

You need to build the HTML output within the loop. As each property is contained within an array you'll need to access it directly by index, and not through another inner loop. Try this:

var data = [
  [236, 1, "1537890525704.jpg", "", "Residential", "Rent", 1200],
  [235, 1, null, "", "Residential", "Rent", 10000]
]

data.forEach(function(prop) {
  var html = `<p>Property Id: ${prop[0]}</p><p>No. of Bedrooms: ${prop[1]}</p><p>Property Type: ${prop[4]}</p>`;
  $('#msgs').append(`<div>${html}</div>`);
});
#msgs div {
  border: 1px solid #C00;
  padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="msgs"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

@ Rory McCrossan Thanks a ton. It worked like a charm.
0

If you have fixed set of data in single array then you change it as..

then(function(data) {
   var text = "";
   var res = "";
   var i;
   for(var i=0; i<data.length; i++){
    var prop = data[i];
        $('#msgs').append($('<div>').text("Property Id:"+ prop[0]));
        $('#msgs').append($('<div>').text("No. of Bedrooms:"+ prop[j]));
});

this ,might help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.