English 中文(简体)
如何使用 vkTemplate 用 JSON 对象来弹出 html 。
原标题:how to use vkTemplate to populate html with JSON object

我希望能够将一个 php- supplied json 对象放入 html 。 我的老方法只是制作一个令人难以置信的长不可理解的 html 字符串, 然后做$. append(hltStr) 上次我把它贴在SO上时没有这么好过。 我想知道是否有人能向我解释如何用 < a href=' 将一个 json 对象放入 html 。 http://www.eslinstructor. net/vktemplate/" rel="nofollow" < a > 这个模板 < a > 。 这显然是对 < a href= > "http://ejohn.org/blog/javacrent-micro-templating/ " rel=" nofolpolt > 的改进。

鉴于对象:

{
"order": {
    "name": "TRADEMARK WHEEL COMPANY",
    "sales_order_id": "18278",
    "order_date": "05 u2044 15 u2044 2012",
    "due_date": "05 u2044 21 u2044 2012",
    "order_number": "1213140",
    "reference": "21192D/35546",
    "order_description": "BICICLETTE",
    "ship_name": "ADAMS",
    "ship_address1": "1919 W RANDOLPH ST.",
    "ship_address2": "",
    "ship_city": "CHICAGO",
    "ship_state": "IL",
    "ship_postal_code": "60606",
    "ship_country": null,
    "ship_via": "FEDEX GROUND",
    "tracking_number": null,
    "package_contents": null,
    "freight": "0.00",
    "taxable": "0.00",
    "nontaxable": "748.88",
    "sales_tax": "0.00"
时 时,
"line_item": [{
    "description": "RED ONE",
    "quantity": "2.00",
    "sell_price": "349.44"
时 时,
{
    "description": "FRONT GEAR",
    "quantity": "2.00",
    "sell_price": "15.00"
时 时,
{
    "description": "5th GEAR",
    "quantity": "2.00",
    "sell_price": "10.00"
时 时]

时 时

是这个 ajax 请求正在收集的 data 数据 , 我如何能够在这个 ajax 函数的成功部分中用它来弹出一个 html 表格 。

$.ajax({
    type: "POST",
    url: "getJSON.php",
    data: submitStr,
    success: function (data) {

    //populate order details

    //loop through variable number of line items

时 时

<html>

<table id="contentTable">
</table>

</html>

还有,

最佳回答

< 强 > HTML

a. 包括jQuery脚本,类似:

<script src=".../libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

b. 包括 vkTemplate 插件, 类似 :

<script src="vktemplate.js" type="text/javascript"></script>

c. 在您的 html 页面中添加一个要输入此表格的元素,例如:

 <div id="container"></div>

<强>TEMPLATE

(注意, 直线项目是数组, 所以您要按顺序排列, 直线项目则不同)

<table id="contentTable">
    <tr>
        <td>
            label-one:
        </td>
        <td>
            <%= o.order.name %> 
        </td>
    </tr>
    <tr>
        <td>
            label-two:
        </td>
        <td>
            <%= o.line_item[0].description %> 
        </td>
    </tr>
</table>

在您的 Web 服务器上保存此模板( 例如, 以 < strong> order. tmpl ) 。

< 强 > JAVASCRIPT

初始化 vkTemplate 插件, 并提供模板 URL 作为第一个参数, json_ data URL 作为第二个参数

$( #container ).vkTemplate(  order.tmpl ,  getJSON.php  );

到了这个时候,你完成了。

希望有帮助

-瓦迪姆

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.