使用 JSON URL 数据动态填充 HTML 表格

发布于 2025-01-05 06:58:24 字数 1573 浏览 4 评论 0原文

我的页面上有这个 HTML 表,我想用以下 JSON URL 中的数据动态填充它: http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1055

我老实说不知道最好的方法来做到这一点,但从我发现 JSON 模板似乎是我应该使用的。我找到了“Pure.js”( http://beebole.com/pure/ )和“Mustache” .js” (https://github.com/janl/mustache.js),使用 Javascript 帮助将 JSON 数据转换为 HTML。我不知道如何使用这些从 URL 加载数据,示例都使用静态字符串。我可以/应该使用 AJAX、jQuery 来做到这一点吗? JSON url 中的数据每天都会变化。

下面 HTML 中带有 {...} 的位置是我想要加载 url 中的数据的位置。

如果有人能指出我如何做到这一点的正确方向,并希望为我提供一个代码示例来帮助我完成这项工作,我将不胜感激。

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2025-01-12 06:58:24

Distal 页面上的演示 http://code.google.com/p/distal 似乎实现您上面想要做的事情。

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

橘味果▽酱 2025-01-12 06:58:24

我不确定您是否仍在寻找示例。如果是这样,总有一款适合您。

pure.js 作为 jQuery 插件工作,因此您可以使用 jQuery ajax 函数从 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);
        }
    });
});

如果 JSON 文档的结构没有改变,pure.js 指令将正常工作。目前如下:

{"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"}}

我还对 HTML 模板进行了一些小的更改,以简化 pure.js 指令模型。请记住,最终的 HTML 应符合预期。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文