JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2012, 09:51 AM
dcparham dcparham is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 17 dcparham User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 7 m 29 sec
Reputation Power: 0
jQuery - Trying to add new row to display json data

I created a JSON variable with 2 records that have 2 fields each. Inside my js function I loop through the JSON array and add each JSON field pair inside a table cell. After the 4 fields are written, the .each method keeps adding the fields into the same table row. I have tried appending a </tr>, etc, to no avail. It is like the .each cycles all the way through the array and ignores attempts to create a new row. Please see code below and offer any suggestions - thank you! I saw some similar questions but nothing quite like this I found helped.

Code:
    function createHTMLTable(tableBody, rows, cols) {
    if (tableBody == null || tableBody.length < 1) return;
    var jsonp = '[{"Flooring":"Spruce","Price":"$7.68","#SF":"780","Total$":"xyz"},{"Flooring":"Bamboo","Price":"$6.47","#SF":"$6.50","Total$":"xyz"}]';
    var flooring = '';               
    var trow 		= $("<tr>");
    var obj = $.parseJSON(jsonp);
        $.each(obj, function() {
        $("<td>")
            .addClass("tableCell")
            .text(this['Flooring'])
            .data("col", 1)
            .appendTo(trow);
                          
        $("<td>")
            .addClass("tableCell")
            .text(this['Price'])
            .data("col", 2)
            .appendTo(trow);
                          
        $("<td>")
            .addClass("tableCell")
            .text(this['#SF'])
            .data("col", 3)
            .appendTo(trow);
                          
        $("<td>")
            .addClass("tableCell")
            .text(this['Total$'])
            .data("col", 4)
            .appendTo(trow);

    //$('#tableBody').append( $("</td></tr>") );//not working
    //$("</tr>") .appendTo(tableBody);		//not working
    //$(this).parents('<tr>').append();		//not working
            });
    trow.appendTo(tableBody);
    } //function

Thank you in advance for your help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > jQuery - Trying to add new row to display json data

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap