0

Possible Duplicate:
Convert JSON array to an HTML table in jQuery

I'm trying desperately to figure out out to make this table...

I have 3 fields I need to be in the same <td> and another field by itself in a <td>

I am successfully getting the data with JSON to my page with this code:

$.ajax({
  url: 'code.php',
  async: false,
  dataType: 'json',
  success: function (json) {
    window.upc = json.upc;
    window.img_name = json.img_name;
    window.quantity = json.quantity;
    window.description = json.description;
  }


});

How can I get this data to be formatted like I want?

|TEXT|image|
|TEXT|goes|
|TEXT|here|

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Devin Prejean
  • 105
  • 1
  • 2
  • 16
  • i don't think it is a duplicate, his data structure is different, and would probably take a bit of fanagling to use jqGrid as the answer to that question suggests. – Patricia Nov 17 '11 at 21:26
  • @Devin: Check out jquery.templ it's a solid plugin (yes, only made it to beta, but it's good). http://api.jquery.com/tmpl/. – Patricia Nov 17 '11 at 21:29

1 Answers1

0

Check out: http://icanhazjs.com/

STEP 1. - DEFINE YOUR TEMPLATE

<script id="template" type="text/html">
  <table>
    <tr>
         <td>{{quantity}}{{description}}</td><td>{{img_name}}</td>
    </tr>
  </table>
</script>

STEP 2. - RETRIEVE YOUR POPULATED TEMPLATE:

$.ajax({
  url: 'code.php',
  async: false,
  dataType: 'json',
  success: function (json) {
     var yourTable = ich.template(json)
  }
});

You can append the variable yourTable to any element on the page.

Jose Vega
  • 10,128
  • 7
  • 40
  • 57
  • Personally I wouldn't recommend ich at the moment as it seems to be using a version of mustache with a dangerously broken implementation of HTML-escaping. (The escaping in the current version of mustache is also broken, just not in a way that allows exploits.) – bobince Nov 17 '11 at 22:10
  • If the problem is the version of mustache, that is easily fixable...I've been using ich for some time now and have not run into the escaping issue. – Jose Vega Nov 18 '11 at 00:21
  • What file do i have to include to use ich.template? – Devin Prejean Nov 18 '11 at 14:41
  • I also need it to be in a "while" kind of loop so i can display a random amount of entries – Devin Prejean Nov 18 '11 at 14:49
  • https://github.com/andyet/ICanHaz.js/raw/master/ICanHaz.js and yes you can do it in a while loop, just append the template to an object. – Jose Vega Nov 18 '11 at 15:05