Click here to Skip to main content
14,639,515 members
Rate this:
Please Sign up or sign in to vote.
See more:
function GetProductDetails(barcodeId,coords) 
{
    $.getJSON("api/products/?barcodeId=" + barcodeId  + "&latitude=" + coords.latitude + "&longitude=" + coords.longitude)
      .done(function (data) {
         
          console.log(data);
          if (data.results == null) {

              $('#result').append(data.message)
          }

          else
          {
              var ul = $("<ul></ul>");
              ul.append(data.results.retailername);
              ul.append(data.results.productname);
              ul.append(data.results.price);
              ul.append(data.results.distance)
          }        
      });
}
Posted
v3
Comments
   
Do you see your data on Console?
klein_jnr 12-Dec-13 2:07am
   
Thank you Tadit no i dont see my data on my console and i would also like to know how i would iterate or go through each object while using the code that you showed me because my data result object is an array of 2 using something like $.each(data, function (key, item) and i only see undefined appearing on my html page as my list,Thank you for your help
   
Do something like below...

$.each( data.items, function( i, item ) {
// Here try to get the data from "item". Check by putting debugger.
});
klein_jnr 12-Dec-13 3:51am
   
Thank you so much Talip, i managed to display everything the way you told me to go about like this. Product Details
Thank you for shopping with us details will be sent to you shortly
ShopName :Shoprite
Name : DogFood
Rand :150
Distance in Km :8309559.355265908
ShopName :Spar
Name : DogFood
Rand :100
Distance in Km :16471269.62219214 but now i need to to separate the list in two parts for readabilty purposes, so that if there are many shops that have the searched product the shopname with all the other items will be added but they should be grouped together i tried the </br> tags but just makes spaces between each item and dont make spaces between two groups, your help is appriciated i am very new to all this
   
Ok post the updated which you are using now. I will try to modify.
klein_jnr 12-Dec-13 6:37am
   
Okay thank you so much here there working code
function GetProductDetails(barcodeId,coords)
{
$.getJSON("api/products/?barcodeId=" + barcodeId + "&latitude=" + coords.latitude + "&longitude=" + coords.longitude)
.done(function (data) {
$('#result').append(data.message)
console.log(data)
var list = $("#result").append('<ul></ul>').find('ul');
$.each(data.results, function (i, item)
{


if (data.results == null) {

$('#result').append(data.message)
}

else {


list.append('<li>ShopName :' + item.retailerName + '</li>');
list.append('<li>Name : ' + item.productName + '</li>');
list.append('<li>Rand :' + item.price + '</li>');
list.append('<li>Distance in Km :' + item.Distance + '</li>');


}

});

$("#result").append(ul);

});
}
   
If you want to group them, then I would suggest you to make them one one "ul" instead of adding them under one "ul". Adding "br" tags in "ul" after "li" would not work.
So, try like below...

function GetProductDetails(barcodeId,coords)
{
$.getJSON("api/products/?barcodeId=" + barcodeId + "&latitude=" + coords.latitude + "&longitude=" + coords.longitude)
.done(function (data) {
$('#result').append(data.message)
console.log(data)

$.each(data.results, function (i, item)
{
if (data.results == null) {
$('#result').append(data.message)
}
else {
var list = $("#result").append('<ul></ul>').find('ul');
list.append('<li>ShopName :' + item.retailerName + '</li>');
list.append('<li>Name : ' + item.productName + '</li>');
list.append('<li>Rand :' + item.price + '</li>');
list.append('<li>Distance in Km :' + item.Distance + '</li>');
$("#result").append(ul);
}
});
});
}
klein_jnr 12-Dec-13 7:06am
   
This only shows one results in other words it only returns one array this is what it returns ShopName :Shoprite
Name : DogFood
Rand :150
Distance in Km :8309559.355265908 and does not return the secound one like this(reults that are not displayed ) ShopName :Spar
Name : DogFood
Rand :100
Distance in Km :16471269.62219214 i need to return both these results but they shouldnt be clustered first result must alone and visa versa
   
I don't get you. Do you need space between each "li" like below...

ShopName :Shoprite

Name : DogFood

Rand :150

Distance in Km :8309559.355265908

Name : DogFood

Rand :100

Distance in Km :16471269.62219214
klein_jnr 12-Dec-13 8:16am
   
I need attributes like Dog Food,Rand,Distance to belong to one Shopname:Shoprite and all the other details to another SHopName :spar it like Shopname:Shoprite has to be a parent container to Name,Rand,Distance and SHopname:Spar has to be a parent container to Name,Rand,Distance too

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to append that ul to another div or something then append all the li to that ul like below...
else
{
    var list = $("#result").append('<ul></ul>').find('ul');

    list.append('<li>' + data.results.retailername + '</li>');
    list.append('<li>' + data.results.productname + '</li>');
    list.append('<li>' + data.results.price + '</li>');
    list.append('<li>' + data.results.distance + '</li>');
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100