【问题标题】:Error with jtemplates with tables带有表格的 jtemplates 错误
【发布时间】:2010-09-09 02:29:59
【问题描述】:

我正在将 jtemplates 与 jquery 一起使用,当我尝试在模板中使用表时出现错误。

以下工作正常

<ul>
 {#foreach $T as record}
 <li>{$T.record.FirstName}</li>
 {#/for}
</ul>

但以下不起作用并给出错误 $T.record is undefined in firebug

<table border="1">
{#foreach $T as record}
<tr>
 <td>{$T.record.FirstName}</td>
</tr>
{#/for}
</table>

以下是我如何使用一些数据调用模板

$(document).ready(function() {
    var data = [
                  { ID: 1, FirstName: 'Anne', Email: 'anne@domain.com' },
                  { ID: 2, FirstName: 'Amelie', Email: 'amelie@domain.com' },
                  { ID: 3, FirstName: 'Polly', Email: 'polly@domain.com' },
                  { ID: 4, FirstName: 'Alice', Email: 'alice@domain.com' },
                  { ID: 5, FirstName: 'Martha', Email: 'martha@domain.com' }
               ];

    $("#jTemplateDemo").setTemplate($("#templateHolder").html());
    $("#jTemplateDemo").processTemplate(data);
});

非常感谢您提供解决此问题的任何帮助。

【问题讨论】:

    标签: jquery jtemplate


    【解决方案1】:

    我会尝试一下,试试这个:

    {#template MAIN}
    <table border="1">
        <tr>
            <th>First Name</th>
            <th>Email</th>
        </tr>
        {#foreach $T as record}
            {#include ROW root=$T.record}
        {#/for} 
    </table>
    {#/template MAIN}
    
    {#template ROW}
        <tr>
            <td>{$T.FirstName}</td>
            <td>{$T.Email}</td>
        </tr>   
    {#/template ROW}
    

    如果您仍然遇到问题,我建议您将模板放在外部 html 文件中并像这样使用它:

    $("#jTemplateDemo").setTemplateURL('JTemplates/yourTemplateHere.html');
    $("#jTemplateDemo").processTemplate(data);
    

    【讨论】:

    • 谢谢兰德尔。我在 IE 8 中正确地看到了结果,但在 Firefox 和 chrome 中却没有。任何想法为什么我会这样做。
    • 检查我更新的答案。 Firefox 和 Chrome 也有错误吗?
    【解决方案2】:

    错误是由于我放置了模板本身。当我将模板放在这样的块中时

    <script id="templateHolder" type="text/html">
        <!-- Template itself -->
    </script>
    

    模板正确呈现。

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 2022-01-24
      • 1970-01-01
      • 2012-10-06
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      相关资源
      最近更新 更多