我正在尝试创建一个 Web 应用程序来处理任意大小的二维矩形表。每个表将是一个文档(存储在一个集合中,并由用户彼此共享)。最终,我想将其扩展到任意数量的维度,但 2D 已经给我带来了麻烦。目前我的数据存储为
{dimensions: [2,3],
data: [2, 3, 5, 7, 11, 13]}
但我认为将 UI 代码编写为数组数组会更容易
[[2, 3, 5],
[7, 11, 13]]
我很容易在其他地方进行这种转换。
我一直在研究一个玩具实现,以在不增加集合和用户帐户的复杂性的情况下将我的头脑围绕在反应部分上。硬编码的 2x3 版本位于https://github.com/scentoni/meteor-grid,双{{#each}}
循环版本位于https://github.com/scentoni/meteor-grid/tree/loopcell,其中关键位是
<table>
<tbody>
{{#each rows}}
<tr>
{{#each cols}}
<td><input class="cell" name="{{../row}},{{col}}" type="text" value="{{stuff ../row col}}"/></td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
和
var iota = function (n) {return Array.apply(null, {length: n}).map(Number.call, Number); }
Template.page.rows = function () {
return iota(Session.get('numrows')).map( function (x) {return {row: x};} );
};
Template.page.cols = function () {
return iota(Session.get('numcols')).map( function (x) {return {col: x};} );
};
Template.page.stuff = function (i, j) {
return Session.get('data')[i][j];
};
这似乎有效,但我觉得我在滥用这些工具。谁能给我一些指导?