I’m trying to add rows to html tables in a CakePHP view and increment the id’s/names where necessary. The code used to generate the table is:
<tbody>
<tr>
<td><?php echo $this->Form->input('OrderDetail.0.product_code'); ?></td>
<td><?php echo $this->Form->input('OrderDetail.0.product_name'); ?></td>
<td><?php echo $this->Form->input('OrderDetail.0.product_qty'); ?></td>
<td><?php echo $this->Form->input('OrderDetail.0.product_price'); ?></td>
</tr>
</tbody>
CakePHP outputs the following html:
<tbody>
<tr>
<td><div class="input text"><input name="data[OrderDetail][0][product_code]" maxlength="100" type="text"/></div></td>
<td><div class="input text"><input name="data[OrderDetail][0][product_name]" maxlength="255" type="text"/></div></td>
<td><div class="input number"><input name="data[OrderDetail][0][product_qty]" step="any" type="number"/></div></td>
<td><div class="input number"><input name="data[OrderDetail][0][product_price]" step="any" maxlength="24" type="number"/></div></td>
</tr>
</tbody>
On clicking a link/button I would like a row added to the table in the same format but with the array incremented by 1 as such:
<tbody>
<tr>
<td><div class="input text"><input name="data[OrderDetail][0][product_code]" maxlength="100" type="text"/></div></td>
<td><div class="input text"><input name="data[OrderDetail][0][product_name]" maxlength="255" type="text"/></div></td>
<td><div class="input number"><input name="data[OrderDetail][0][product_qty]" step="any" type="number"/></div></td>
<td><div class="input number"><input name="data[OrderDetail][0][product_price]" step="any" maxlength="24" type="number"/></div></td>
</tr>
<tr>
<td><div class="input text"><input name="data[OrderDetail][1][product_code]" maxlength="100" type="text"/></div></td>
<td><div class="input text"><input name="data[OrderDetail][1][product_name]" maxlength="255" type="text"/></div></td>
<td><div class="input number"><input name="data[OrderDetail][1][product_qty]" step="any" type="number"/></div></td>
<td><div class="input number"><input name="data[OrderDetail][1][product_price]" step="any" maxlength="24" type="number"/></div></td>
</tr>
</tbody>
I can get the desired result using plain old Javascript, but I’m looking for a more elegant solution. I have jQuery and the JSHelper loaded.
my solution would be this
Demo: http://jsfiddle.net/CKxLh/