使用 JSON URL 数据动态填充 HTML 表格
我的页面上有这个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
我不确定您是否仍在寻找示例。如果是这样,总有一款适合您。
pure.js
作为jQuery
插件工作,因此您可以使用jQuery
ajax
函数从http://services.runescape.com/...
加载数据。如果 JSON 文档的结构没有改变,
pure.js
指令将正常工作。目前如下:我还对 HTML 模板进行了一些小的更改,以简化 pure.js 指令模型。请记住,最终的 HTML 应符合预期。
I am not sure if you are still looking for examples. If so there is one for you.
pure.js
works as ajQuery
plugin so you can usejQuery
ajax
function to load data fromhttp://services.runescape.com/...
.pure.js
directives will work fine for if structure of your JSON document does not change. Currently it is as follows: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.