1

i have some json response which contains data in array format.i have to display that data one by one in html page.in my html page i added some div element for merchantname,productname and remove button.and json data contanins array of same product name,merchant name,remove button for specific merchant.now when i am displaying data in page its showing me all the name of merchant after that list of product.means it is like merchant name:abc merchantname:xyz

productname:a1 productname:a2 remove button for merchant1 remove button for merchant2 but i want to display this data like merchantname:abc productname:a1 remove button for merchnant1 merchantname:xyz productname:a2 remove button for merchnant2 this is the code

$("#merchantname").append("<font color='green'>"+(responseObj.merchants.merchantname[i])+"</font>"+"\n");
    $("#productname").append(responseObj.merchants.productname[i]+"\n");

    $("#remove").append("<input type='button' value='remove' onClick='remove(needID)'>"+"</input>"+"\n");
1
  • Use jsrender or simply iterate the json array to append in your page.. Commented May 21, 2014 at 6:37

1 Answer 1

0

Based on my understanding of your problem I have written a small piece of code which may help answer the question.

I'll try to answer any question of yours.

//Create the dom from the json response
$.each(responseObj.merchants,function(index){
    record=$('<div/>',{'class':'record'});
    merchantname=$('<span/>',{'class':'merchant'});
    merchantname.text(responseObj.merchants[index].merchantname);

    productname=$('<span/>',{'class':'product'});
    productname.text(responseObj.merchants[index].productname);

    remove=$('<input/>',{'class':'remove','val':'Remove','type':'button'});

    record.append(merchantname).append(productname).append(remove);

    $('#view').append(record);
});

// Handler for the remove button
$(document).on('click','.remove',function(){
// Statement to remove record
    $(this).closest('.record').remove();
});

FIDDLE

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

12 Comments

see this is json "needs":{"customerNeeds":[{"needTitle":"Need a Mobile","needDescription":"Android","fromCustomer":"xyz ","needDate":"2013-12-20T07:39:39Z","needID":4,"needStatus":"pending"} ]} now i want to print needtitle ,i wrote like $.each(response.needs, function (i, needs) { alert(needs.customerNeeds.needTitle); });but its not showing anything.correct me what i m doing wrong here.
thanks for this .but pls see this i tried but i want one by one data means need a bike ,after that its need id...not all need after all need id..jsfiddle.net/ardeezstyle/9c2Tn
@user3415421 Can you please be a bit more clear on your last comment? I'm afraid to say I'm not able to understand.
yes sure..from ur link i tried ,i m getting right data but for designing purpose i want data should be displayed like all the info of first need then all the info of second need one by one..means suppose i m havving needtitle and need date.then i want data should be displayed like first needid,its need date then second need title ,second need date,but what i m getting is first needtitle,second needtitle,first need id,second need id which i dont want.i know the error also as i m appending it to div thats why its showing like this.but if u have any other idea.then pls tell me.
Oh I understand now. I have re-written the code for you. Actually, all the record divs should be appended to the same div with id view. Since you were appending to two different divs, it was not coming as you wanted. Please find the fiddle here jsfiddle.net/ardeezstyle/9c2Tn/4.
|

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.