0

I am trying to render a table in html using Ajax data payload. The ajax data structure is as follows:-

{"id":733,
 "lastUpdatedBy":"4",
 "lastUpdatedTime":"2013-11-24 03:00:03PM",
 "jobName":"jobnameA",
 "accountName":"accountname1A"
} 

The Html for the table to be rendered is as follows:-

    <table id="main_table">
       <thead>
        <tr class="firstline">
        <th>Column1</th>
        <th>Column2</th>
        <th>Column3</th>
     </tr>
    </thead>
     <tbody>
     <tr style="background-color:green; color:white">
        <td  colspan="4" class="flip" id="fisrtsec"> Test Account 19 </td>
     </tr>
    </tbody>
    <tbody class="section">
    <tr>

        <td>item id</td>
        <td>item jobName</td>
        <td>item accountName</td>

    </tr>
    <tr>
    <td></td>
        <td>item 121</td>
        <td>item 122</td>
        <td>item 123</td>
        <td>item 124</td>
     </tr>
     <tr>
      <td></td>
        <td>item 131</td>
        <td>item 132</td>
        <td>item 133</td>
        <td>item 134</td>
      </tr>
     </tbody>
     <tbody>
     <tr style="background-color:green; color:white">
        <td  colspan="4" class="flip"> Section 2 </td>
     </tr>
   </tbody>
   <tbody class="section">
    <tr>
     <td></td>
        <td>item 211</td>
        <td>item 212</td>
        <td>item 213</td>
        <td>item 214</td>
     </tr>
     <tr>
     <td></td>
        <td>item 221</td>
        <td>item 222</td>
        <td>item 223</td>
        <td>item 224</td>
     </tr>
      <tr>
     <td></td>
        <td>item 231</td>
        <td>item 232</td>
        <td>item 233</td>
        <td>item 234</td>
       </tr>
    </tbody>

    </table>

Am using Jquery library. My jquery is

 $.each(data, function(key, val) {
    $('<tr><td>ID: '+key+'</td><td id="'+key+'">'+val+'</td>   
  <tr>').appendTo('#main_table');
     });

Any help is appreciated. Thanks for looking.

Vivek
  • 61
  • 1
  • 8
  • 1
    It looks like you are missing some markup at the top of your HTML section. And do you have any JavaScript code to post? What have you tried? What frameworks/libraries are you using, if any? – Drew Gaynor Nov 26 '13 at 00:47

1 Answers1

0

I don't think having multiple tbody's is valid markup. Having said that, try.

('<tr><td>ID: '+key+'</td><td id="'+key+'">'+val+'</td><tr>')
  .appendTo('#main_table tbody:last');

Using :last will append at the end of the table.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
  • Thanks for the solution. I tried the above code. It is working fine. However, am looking to toggle the section with class 'flip' (in html table), so that I can show/hide the block - , hence the multiple tags. – Vivek Nov 26 '13 at 19:00
  • I think using multiple tbody elements should be fine. As per the question here - [link](http://stackoverflow.com/questions/3076708/can-we-have-multiple-tbody-in-same-table?rq=1) – Vivek Nov 26 '13 at 21:24