使用 Handlebars.js 创建网格的简单方法?
我试图根据该数组中的对象生成一个由 5 个元素宽的 div 组成的网格:
[{n:'a'},{n:'b'},{n:'c'},{n:'d'}...{n:'y'}];
该数组可能包含 1 到 50 个对象,数据格式是来自 Spine.js 模型的一维数组。为了分离数据和表示,我希望将数据保留在一维数组中,并使用视图(车把模板)代码在每第 5 个项目上开始一个新行,如下所示:
<div class="grid">
<div class="row">
<div class="cell"> a </div>
<div class="cell"> b </div>
<div class="cell"> c </div>
<div class="cell"> d </div>
<div class="cell"> e </div>
</div>
<div class="row">
<div class="cell"> f </div>
etc...
</div>
我有一个解决方案,通过返回辅助函数中的整个字符串。只有我的模板看起来像:
<script id="grid-template" type="text/x-handlebars-template">
{{#grid}}
{{/grid}}
</script>
这似乎违背了使用模板的意义。有没有一种简单的方法来创建像上面这样的网格,其中代码主要驻留在模板中?
[编辑]解决方案
根据下面@Sime的回答修改控制器中的数据。
模板代码:
<script id="grid-template" type="text/x-handlebars-template">
{{#rows}}
<div class="row">
{{#cells}}
<div class="cell">
{{n}}
</div>
{{/cells}}
</div>
{{/rows}}
</script>
控制器渲染代码():
this.data=[{n:'a'},{n:'b'},{n:'c'},{n:'d'}...{n:'y'}]; // previously set
this.rows=[];
var step=5,
i=0,
L=this.data.length;
for(; i<L ; i+=step){
this.rows.push({cells:this.data.slice(i,i+step)});
};
this.el.html(this.template(this));
I'm attempting to generate a grid of divs five elements wide from the objects in this array:
[{n:'a'},{n:'b'},{n:'c'},{n:'d'}...{n:'y'}];
The array may contain between 1 and 50 objects, and the data format is a 1d array coming from a Spine.js model. In order to separate data and presentation, I'm hoping to keep the data in a 1d array, and use the view (handlebars template) code to start a new row on every 5th item, like so:
<div class="grid">
<div class="row">
<div class="cell"> a </div>
<div class="cell"> b </div>
<div class="cell"> c </div>
<div class="cell"> d </div>
<div class="cell"> e </div>
</div>
<div class="row">
<div class="cell"> f </div>
etc...
</div>
I have a solution working by returning the whole string in a helper function. Only my template looks like:
<script id="grid-template" type="text/x-handlebars-template">
{{#grid}}
{{/grid}}
</script>
That seems like it defeats the point of using templates. Is there a simple way to create a grid like the above, where the code resides mostly in the template?
[Edit] Solution
Modify the data in the controller, based on @Sime's answer below.
Template code:
<script id="grid-template" type="text/x-handlebars-template">
{{#rows}}
<div class="row">
{{#cells}}
<div class="cell">
{{n}}
</div>
{{/cells}}
</div>
{{/rows}}
</script>
Controller rendering code ():
this.data=[{n:'a'},{n:'b'},{n:'c'},{n:'d'}...{n:'y'}]; // previously set
this.rows=[];
var step=5,
i=0,
L=this.data.length;
for(; i<L ; i+=step){
this.rows.push({cells:this.data.slice(i,i+step)});
};
this.el.html(this.template(this));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,模板将是:
但是,此模板需要一个二维数组,因此您必须首先转换数据对象。
现场演示: http://jsfiddle.net/emfKH/3/
So, the template would be:
However, this template expects a two-dimensional array, so you would have to transform your data-object first.
Live demo: http://jsfiddle.net/emfKH/3/
尝试使用表标签示例:
您还可以提供 id 和类作为其属性,以便于操作策略。
try using table tag example:
and you could also provide ids and classes as their attributes for easy manipulation strategies.