使用jquery模板明智地显示网格列

时间:2012-02-20 04:54:11

标签: jquery jquery-ui jquery-plugins jquery-templates jsrender

我有json行的数据明智的计划.. 像

[{"CostPerSearch":2.39,"PackageId":9,"PackageName":"PAYG","PackPlanId":1,"PackTypeId":2,"SearchPerMonth":0,"SetupFee":349.00,"SubscriptionFee":0.00,"ValidityMonth":null},{"CostPerSearch":1.99,"PackageId":10,"PackageName":"Standard","PackPlanId":1,"PackTypeId":2,"SearchPerMonth":275,"SetupFee":349.00,"SubscriptionFee":499.00,"ValidityMonth":null},{"CostPerSearch":1.79,"PackageId":11,"PackageName":"Premium","PackPlanId":1,"PackTypeId":2,"SearchPerMonth":600,"SetupFee":349.00,"SubscriptionFee":999.00,"ValidityMonth":null},{"CostPerSearch":1.59,"PackageId":12,"PackageName":"Silver","PackPlanId":1,"PackTypeId":2,"SearchPerMonth":1500,"SetupFee":349.00,"SubscriptionFee":1999.00,"ValidityMonth":null},{"CostPerSearch":1.39,"PackageId":13,"PackageName":"Gold","PackPlanId":1,"PackTypeId":2,"SearchPerMonth":3800,"SetupFee":349.00,"SubscriptionFee":4999.00,"ValidityMonth":null},{"CostPerSearch":0.00,"PackageId":14,"PackageName":"Platinum","PackPlanId":1,"PackTypeId":2,"SearchPerMonth":0,"SetupFee":349.00,"SubscriptionFee":9999.00,"ValidityMonth":null}]

我希望按以下格式列显示以上数据

Packages        PayG       Standard .......

Setupfee         349         349
Monthlyfee       0           499
......

我有json但是对jquery模板的使用感到困惑,以便在上面看起来很容易显示列式数据。

1 个答案:

答案 0 :(得分:0)

一种选择是在放入模板之前转换javascript对象。您似乎知道该表将包含多少行。在您的示例中,它有8行,因为每个条目都有9个属性,但您要为顶部列名称提取一个。

这种变化虽然有点乏味,但并非火箭科学。

您还可以查看“星星”示例。它为每个评级附加一个字符串。它在这里应用的方式是你可以在这个例子中有一组字符串 - 9。一个用于标题1,然后用于每个数据行8个。在每个循环中,将{{:PackageName}}附加到标题字符串,并将类似(例如){{:SetupFee}}附加到setup_fee字符串。完成字符串后,用字符串包装tr字符串。用“tr”包装每个td字符串,然后用“tbody”包装所有那些。最后把两个包在一张桌子里。