2010-11-16 233 views
2

嗨,我试图从Dave Ward blogjQuery的模板插件

关于jQuery的模板和东西我做错了这个例子。 任何帮助将不胜感激。 这是他的代码: 数据:

var invoice = { 
    invoiceItems: [ 
    { type: 'item', 
     part: '99Designs', description: '99 Designs Logo', 
     price: 450.00, qty: 1 }, 
    { type: 'service', 
     service: 'Web development and testing', 
     price: 25000.00 }, 
    { type: 'item', 
     part: 'LinodeMonthly', description: 'Monthly site hosting', 
     price: 40.00, qty: 12 } 
    ] 
}; 

客户:

<script id="invoiceTemplate" type="x-jquery-tmpl"> 
     <table class="invoice"> 
     {{each lineItems}} 
     {{tmpl($value) get_invoiceRowTemplateName(type)}} 
     {{/each}} 
     </table> 
    </script> 

JS:

$(function() 
{ 
    $('#invoiceTemplate').tmpl(invoice).appendTo('body'); 
}); 

function get_invoiceRowTemplateName(type) { 
    // Return a template selector that matches our 
    // convention of <type>RowTemplate. 
    return '#' + type + 'RowTemplate'; 
} 
+1

你可能要阐述什么“错”的意思......你得到一个输出,是输出错的,什么是错的结果,等等等等 – Ben 2010-11-16 01:37:36

+0

@Ben 它没有做任何事情,但戴夫已经回答我的问题,我错过了“行”模板。 – pyccki 2010-11-16 15:58:32

回答

3

不要忘记行模板:

<script id="serviceRowTemplate" type="x-jquery-tmpl"> 
    <tr class="service"> 
    <td colspan="2">${service}</td> 
    <td colspan="2">${price}</td> 
    </tr> 
</script> 

<script id="itemRowTemplate" type="x-jquery-tmpl"> 
    <tr class="item"> 
    <td>${item}</td> 
    <td>${description}</td> 
    <td>${price}</td> 
    <td>${qty}</td> 
    </tr> 
</script> 

当get_invoiceRowTemplateName()将每个项目的type解析为相应的* type * RowTemplate时,这些单独的行模板就是用于呈现每个项目的内容。

+0

明白了戴夫。 非常感谢! – pyccki 2010-11-16 15:57:57