Skip to main content

jQuery JSON response into table column



The excerpt below is part of a .ajax() function I'm using to pull data from a database. The database is queried using PHP and the output sent back in JSON format. Only 1 row of data is returned by the function.







success: function(data) {

for(var key in data) {

$("#formTable tr").find("td:eq(1)").text(data[key]);

}

}







I have an HTML table on the page which is split into two columns. The left column has field labels, the right column is empty.





I would like to cycle through my JSON reply for each key/value pair. I would like to insert the value into the right hand column table cell. The code should cycle through until all key/value pairs have been output onto the next table row, into the next right hand table cell.





The code above selects the second column table cell but inserts the last JSON value into all cells instead of each value going into its' own table cell in the column.





I think if I can get the selector right this will work, I'm just not sure what that should be..





Thanks.


Comments

  1. You are basically selecting all the rows of the table and second td from the whole set of table rows in each iteration so it isn't working as expected.

    Assuming that the json response has one to one mapping of keys and the field labels in the table you can try this.

    success: function(data) {
    var fieldCount = 0,
    $tableRows = $("#formTable tr");
    for(var key in data) {
    $tableRows.eq(fieldCount++).find("td:eq(1)").text(data[key]);
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.