1

I have this HTML table on my page and I want to fill it dynamically with data from this JSON URL: http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1055

I honestly don't know the best way to do this but from what I've found JSON templating seems to be what I should use. I've found "Pure.js" ( http://beebole.com/pure/ ) and "Mustache.js" (https://github.com/janl/mustache.js) that use Javascript to help convert JSON data to HTML. I can't figure out how to load the data from the URL using these, the example are all using a static string. Can I/Should I do this with AJAX, jQuery? The data from the JSON url changes daily.

The places I have in the HTML below with {...} are where I want the data from the url to load into.

I'd appreciate if someone could point me in the right direction on how to do this and hopefully provide me with a code sample to help me get this working.

<div class="item-details">
<div>
<h5>
{item.name}
</h5>
<p>Aaaarrrghhh ... I'm a monster.</p></div>
<h6>Pricing Information:</h6>
<table>
<tbody>
<tr>
<th>Current guide price:</th>
<td>{item.current.price}</td>
</tr>
<tr>
<th>Today's Change:</th>
<td class="{item.today.trend}">{item.today.price}</td>
</tr>
</tbody>
<tr>
<th>30 Day Change:</th>
<td class="{item.day30.trend}">{item.day30.change}</td>
</tr>
<tr>
<th>90 Day Change:</th>
<td class="{item.day30.trend}">{item.day90.change}</td>
</tr>
<tr>
<th>180 Day Change:</th>
<td  class="{item.day30.trend}">{item.day180.change}</td>
</tr>
</table>
</div>
</div>
Tom
  • 26,212
  • 21
  • 100
  • 111
Pixel Life
  • 13
  • 1
  • 4

2 Answers2

0

I am not sure if you are still looking for examples. If so there is one for you.

pure.js works as a jQuery plugin so you can use jQuery ajax function to load data from http://services.runescape.com/....

$(function(){
    var directives = {
        'h5' : 'item.name',
        'td.currentPrice' : 'item.current.price',
        'td.currentPrice@class' : function() {return "";},
        'td.todayTrend' : 'item.today.price',
        'td.todayTrend@class' : 'item.today.trend',
        'td.day30Trend' : 'item.day30.change',
        'td.day30Trend@class' : 'item.day30.trend',
        'td.day90Trend' : 'item.day90.change',
        'td.day90Trend@class' : 'item.day90.trend',
        'td.day180Trend' : 'item.day180.change',
        'td.day180Trend@class' : 'item.day180.trend'
    };

    $.ajax({
        url: 'http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1055',
        dataType: 'jsonp',
        success: function(data) {
            $('div.item-details').render(jsonData, directives);
        }
    });
});

pure.js directives will work fine for if structure of your JSON document does not change. Currently it is as follows:

{"item":{"icon":"...","icon_large":"...","id":1055,"type":"...","typeIcon":"...","name":"...","description":"...","current":{"trend":"neutral","price":"131.2m"},"today":{"trend":"positive","price":"+1.1m"},"day30":{"trend":"positive","change":"+11.0%"},"day90":{"trend":"positive","change":"+14.0%"},"day180":{"trend":"positive","change":"+32.0%"},"members":"false"}}

I also added small changes to your HTML template in order to simplify pure.js directives model. Please bear in mind that the final HTML should look as expected.

<div class="item-details">
  <div>
    <h5>{item.name}</h5>
    <p>Aaaarrrghhh ... I'm a monster.</p>
  </div>
  <h6>Pricing Information:</h6>
  <table>
    <tbody>
      <tr>
        <th>Current guide price:</th>
        <td class="currentPrice">{item.current.price}</td>
      </tr>
      <tr>
        <th>Today's Change:</th>
        <td class="todayTrend">{item.today.price}</td>
      </tr>
    </tbody>
    <tr>
      <th>30 Day Change:</th>
      <td class="day30Trend">{item.day30.change}</td>
    </tr>
    <tr>
      <th>90 Day Change:</th>
      <td class="day90Trend">{item.day90.change}</td>
    </tr>
    <tr>
      <th>180 Day Change:</th>
      <td class="day180Trend">{item.day180.change}</td>
    </tr>
  </table>
</div>
Tom
  • 26,212
  • 21
  • 100
  • 111
0

The demo on the Distal page http://code.google.com/p/distal seems to achieve what you're trying to do above.

Kernel James
  • 3,752
  • 25
  • 32