返回模板表中行和列的索引

埃尔汗·亚萨尔

我有一个通过Handlebars.js模板创建的动态表,问题是我无法将 id 分配给每个单元格,将它们的位置表示为行和列。我有下面的模板,

<script id="td-template" type="text/x-handlebars-template">
    {{#each ships}}
    <tr>
        <td class='left-aligned'>{{this.name}}</td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
        <td class="default" id="id{{@index}}"></td>
    </tr>
    {{/each}}
</script>

第一个循环返回 0,第二个循环返回 1,反之亦然。我需要的是添加一个公式而不是{{@index}} ,但它不会让我在我打算破坏 html 或表格本身时添加。

我打开任何建议,例如JQueryHandlebars.js

提多

您可以定义一个辅助函数,如下所示:

var columnsCount = 3; // change this to the total number of columns per row in your table.
Handlebars.registerHelper("multiply", function(index, count){
    return (index * columnsCount) + count;
});

然后使用以下内容设置 ID:

<script id="td-template" type="text/x-handlebars-template">
    {{#each ships}}
    <tr>
        <td class='left-aligned'>{{this.name}}</td>
        <td class="default" id="id{{multiply @index 1}}"></td>
        <td class="default" id="id{{multiply @index 2}}"></td>
        <td class="default" id="id{{multiply @index 3}}"></td>
        .....
    </tr>
    {{/each}}
</script>

这是一个显示这种方法在行动中的片段:

var columnsCount = 3; // change this to the total number of columns per row in your table.
Handlebars.registerHelper("multiply", function(index, count){
    return (index * columnsCount) + count;
});

var context = {
  ships: [{
    name: 'ship one'
  }, {
    name: 'ship two'
  }]
};

$('#rendered').append(Handlebars.compile($("#td-template").html())(context));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.js"></script>

<script id="td-template" type="text/x-handlebars-template">
    <table>
      <tbody>
        {{#each ships}}
        <tr>
            <td class='left-aligned'>{{this.name}}</td>
            <td class="default" id="id{{multiply @index 1}}">column {{multiply @index 1}}</td>
            <td class="default" id="id{{multiply @index 2}}">column {{multiply @index 2}}</td>
            <td class="default" id="id{{multiply @index 3}}">column {{multiply @index 3}}</td>
            <!-- and so on.... -->
        </tr>
        {{/each}}
      </tbody>
    </table>
</script>

<div id="rendered"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

查找值是表中行和列的串联

来自分类Dev

返回csv模块python中行的索引

来自分类Dev

SQL在表上创建多列索引和单列索引

来自分类Dev

使用Requests和lxml,获取表中行的href值

来自分类Dev

索引列和行返回不正确的值

来自分类Dev

结合索引模板和动态模板

来自分类Dev

结合索引模板和动态模板

来自分类Dev

数据透视表按名称(索引)和标题(列)的等级

来自分类Dev

Pandas数据透视表索引和列的小计

来自分类Dev

索引和表空间

来自分类Dev

如何获取二维数组中行和列的长度?

来自分类Dev

如何使用R获取绘图中行和列的总和

来自分类Dev

python数据框中行和列之间的公共元素

来自分类Dev

计算R中数据框中行和列的中位数

来自分类Dev

扩展Google表格中行和列的多个标量产品

来自分类Dev

如何获取二维数组中行和列的长度?

来自分类Dev

php表中行的颜色

来自分类Dev

Django:返回模板和模板变量

来自分类Dev

使用数据透视表只返回索引列,省略数据透视列

来自分类Dev

获取选中行的行索引

来自分类Dev

获取选中行的行索引

来自分类Dev

如何在表布局中查找行索引和列索引?

来自分类Dev

Cassandra中行键,主键和索引之间有什么区别?

来自分类Dev

在工作表中搜索值并返回行号和列号

来自分类Dev

索引扫描,索引查找和表扫描

来自分类Dev

列中行的连接值

来自分类Dev

R中行的列值

来自分类Dev

从具有表名,行和列索引的表中获取特定值

来自分类Dev

将 Vector 匹配到多列,返回其他列中的索引和对应值