我有一个看起来像这样的表:
<table name="exercises" id="workout-table">
<tr>
<th>Name</th>
<th>Reps/Intervals</th>
<th>Sets</th>
<th>Weight/Distance/Time</th>
</tr>
[%- i=0 %]
[% WHILE i<=10 %]
<tr class="workout-rows">
<td><input type="text" name="workout[exercise][[% i %]][name]" /></td>
<td><input type="text" name="workout[exercise][[% i %]][repetitions]" size="3"/></td>
<td><input type="text" name="workout[exercise][[% i %]][sets]" size="3"/></td>
<td><input type="text" name="workout[exercise][[% i %]][weight]" size="4"/></td>
</tr>
[% i = i + 1 %]
[% END %]
</table>
该模板代码是 Template::Toolkit 代码,它基本上只生成一个索引,因此我可以跟踪从 Catalyst::Plugin::Params::Nested 变为 HoAoH 的元素。这是实际在按钮单击时将行添加到表中的 javascript:
$("#add-row").click(function(){
var size = $(".workout-rows").length;
//size += 1;
var row ='<tr class="workout-rows">' +
'<td><input type="text" name="workout[exercise][' + size + '][name]" /></td>' +
'<td><input type="text" name="workout[exercise][' + size + '][repetitions]" size="3"/></td>' +
'<td><input type="text" name="workout[exercise][' + size + '][sets]" size="3"/></td>' +
'<td><input type="text" name="workout[exercise][' + size + '][weight]" size="4"/></td>' +
'</tr>';
$("#workout-table >tbody tr:last").after(row)
});
我真的不喜欢将表格行标记复制粘贴到脚本本身的想法,因为它重复且不直观。我已经尝试过 .clone 的东西,它非常适合逐字复制行,但它不能像我需要的那样动态跟踪行数。
所以基本上我已经将其缩减为需要找出如何弄乱每个输入的名称,以便它可以适当地反映循环索引,因此 Catalyst::Plugin::Params::Nested 将构建正确的结构。
想法?