使用 Handlebars.js 创建网格的简单方法?

发布于 2025-01-06 01:29:01 字数 1522 浏览 2 评论 0原文

我试图根据该数组中的对象生成一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2025-01-13 01:29:01

因此,模板将是:

<script id="template" type="x-handlebars-template">
    <div class="grid">
        {{#each this}}
        <div class="row">
            {{#each this}}
            <div class="cell">{{n}}</div>
            {{/each}}
        </div>
        {{/each}}
    </div>
</script>

但是,此模板需要一个二维数组,因此您必须首先转换数据对象。

function transform ( arr ) {
    var result = [], temp = [];
    arr.forEach( function ( elem, i ) {
        if ( i > 0 && i % 5 === 0 ) {
            result.push( temp );
            temp = [];
        }
        temp.push( elem );
    });
    if ( temp.length > 0 ) {
        result.push( temp );
    }
    return result;
}

现场演示: http://jsfiddle.net/emfKH/3/

So, the template would be:

<script id="template" type="x-handlebars-template">
    <div class="grid">
        {{#each this}}
        <div class="row">
            {{#each this}}
            <div class="cell">{{n}}</div>
            {{/each}}
        </div>
        {{/each}}
    </div>
</script>

However, this template expects a two-dimensional array, so you would have to transform your data-object first.

function transform ( arr ) {
    var result = [], temp = [];
    arr.forEach( function ( elem, i ) {
        if ( i > 0 && i % 5 === 0 ) {
            result.push( temp );
            temp = [];
        }
        temp.push( elem );
    });
    if ( temp.length > 0 ) {
        result.push( temp );
    }
    return result;
}

Live demo: http://jsfiddle.net/emfKH/3/

吐个泡泡 2025-01-13 01:29:01

尝试使用表标签示例:

<table>
<thead>
<th>head 1</th>
<th>head 2</th>
<th>head 3</th>
</thead>
<tbody>
<tr>
<td>column 1</td>
<td>column 2</td>
<td>column 3</td>
</tr>
</tbody>
</table>

您还可以提供 id 和类作为其属性,以便于操作策略。

try using table tag example:

<table>
<thead>
<th>head 1</th>
<th>head 2</th>
<th>head 3</th>
</thead>
<tbody>
<tr>
<td>column 1</td>
<td>column 2</td>
<td>column 3</td>
</tr>
</tbody>
</table>

and you could also provide ids and classes as their attributes for easy manipulation strategies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文