0

Personally, I have a servlet that returns a Map via Json and I would like to use the values of this map (key and value) to populate a table via Ajax.

My code in Ajax are as follows below:

success:function(responseJson){
$(".results").show();
$(".content").show();

var headTable = $(".tablesorter > thead > tr");
var bodyTable = $(".tablesorter > tbody > tr");

$.each(responseJson, function(key, value){
    $("<th>").html(key).appendTo(headTable);

    for (var i = 0; i < value.length; i++){
        $("<td>").val(key).html(value[i]).appendTo(bodyTable);
    }
});

The key of my Map I would like it to be the value of the head of the table and the Map's values were the body of the table.

The table I want to populate the following below:

<table cellspacing="1" class="tablesorter">
<thead>
    <tr>

    </tr>
</thead>
<tbody>
    <tr class="even">

    </tr>
</tbody>

However, the way my code to be rather than to create rows with the values from the Map, it be creating multiple columns.

What should I do to my code behave correctly?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jeovane Reges
  • 19
  • 1
  • 5
  • Show how the JSON looks like, and show how the table should look like given this JSON. – JB Nizet Feb 02 '13 at 16:37
  • Suppose in my Map has the following values below.
    Map<"A", "a1,a2,a3">
    Map<"B", "b1,b2,b3">
    What I wish I was using the values of the Key as the head of the table and with each Key values to create the body of the table. However, the way that my code is not to be working.
    – Jeovane Reges Feb 02 '13 at 17:08
  • Why don't you simply do what you're being asked? Edit your question (by clicking the *edit* link), then show us how the JSON looks like (what the map looks like is almost irrelevant: you don't use a Map but a JSON structure in your JS code), then show us what the HTML table should look like given this JSON. – JB Nizet Feb 02 '13 at 17:11
  • This is the result of my table after it is populated by Json http://bit.ly/WLZ01a .. As you can see my code be creating multiple columns. The values in this column were to be the rows corresponding to the values of the table header. – Jeovane Reges Feb 02 '13 at 17:16
  • Why don't you simply do what you're being asked? Edit your question (by clicking the edit link), then show us how the JSON looks like (what the map looks like is almost irrelevant: you don't use a Map but a JSON structure in your JS code), then show us what the HTML table should look like given this JSON. – JB Nizet Feb 02 '13 at 17:52

0 Answers0