【问题标题】:json with jquery template带有 jquery 模板的 json
【发布时间】:2011-03-20 22:21:30
【问题描述】:

我有这个多维 json,它应该生成一个 html 表。但截至目前,唯一要写入的内容是 tr,而不是 td。

我知道已经有一个类似的问题title,但用法完全不同。

与我所知道的不同之处在于,我正在使用模板方法编写模板,而不是将每个模板都声明为自己的脚本...

这是解析后的(JSON)数组


[
    [
        {
        "name": "Morning meeting.",
        "description": "The morning business meeting hosted at Apple.",
        "image": "302632084.png",
        "rating": "1.5",
        "category": "4"
        },
        {
        "name": "Winning",
        "description": "",
        "image": "321752348.png",
        "rating": "5.0",
        "category": "3"
        },
        {
        "name": "1234566890abcdefghijklmnopqrstuvwxyz",
        "description": "",
        "image": "316892896.png",
        "rating": "3.0",
        "category": "16"
        }
    ],
    [
        {
        "name": "Kristian Lunch",
        "description": "Having a lunch break.",
        "image": "320196094.png",
        "rating": "3.0",
        "category": "8"
        },
        {
        "name": "Dropping the kids off at the pool.",
        "description": "A daly lesson on taking the kids to the pool.",
        "image": "302658031.png",
        "rating": "5.0",
        "category": "4"
        },
        {
        "name": "Dropping the kids off at the pool.",
        "description": "A daly lesson on taking the kids to the pool.",
        "image": "302658031.png",
        "rating": "5.0",
        "category": "4"
        }
    ]
]

这是我应该写入数据的方法


EventsView.prototype.writeGrid = function(events)
{
    var gridRows = "<td class='gridRow'>${name}</td>";    
    $.template("gridRows", gridRows);
    var markup = "<tr class='gridCol'>{{events, gridRows}}</tr>";
    $.template( "gridCols", markup );
    $.tmpl( "gridCols", events )
        .appendTo("#gridBody");
}

截至目前,这是生成的 html


<tbody id="gridBody">
    <tr class="gridCol">
    </tr>
    <tr class="gridCol">
    </tr>
</tbody>

这是我想要的 HTML...


<tbody id="gridBody">
    <tr class="gridCol">
        <td class="gridRow">Morning meeting.</td>
        <td class="gridRow">Winning</td>
        <td class="gridRow">1234566890abcdefghijklmnopqrstuvwxyz</td>
    </tr>
    <tr class="gridCol">
        <td class="gridRow">Kristian Lunch</td>
        <td class="gridRow">Dropping the kids off at the pool.</td>
        <td class="gridRow">Dropping the kids off at the pool.</td>
    </tr>
</tbody>

【问题讨论】:

    标签: jquery multidimensional-array jquery-templates


    【解决方案1】:

    JSON 是你的朋友,比多维数组更简洁...

    http://www.json.org/js.html

    https://github.com/douglascrockford/JSON-js

    【讨论】:

    • 你使用的是 json2.js 吗?如果是这样,你能把jsFiddle放上来,我帮你看看吗?很高兴能帮上忙,不久前我做了一个类似的模板系统...... :-)
    • 我不太确定我使用的是什么 json。我把它贴在 jsfiddle 上。谢谢你的帮助! jsfiddle.net/YHQAa
    • 我找到了一个不使用模板系统的解决方法,但我真的不想让网站的这一部分不使用它。
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多