Reputation: 57
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.
Upvotes: 2
Views: 1882
Reputation: 40066
my solution would be this
$(":button").click(function() {
$("tr:last-child").clone().appendTo('tbody'); //clone the last row
$("tr:last-child input").each(function() { //for each input in the last row, do the following
var nameAttr = $(this).attr('name'); //get the whole value of attribute 'name'
var newIndex = parseInt(nameAttr.replace(/[^\d]/g, ''))+1; //get the digit and add one
$(this).attr('name',nameAttr.replace(/\d/,newIndex)); //replace the old digit with new
});
});
Demo: http://jsfiddle.net/CKxLh/
Upvotes: 1
Reputation: 5303
Check this jQuery code:
$('#add_element').click(function(){
var next_id = $('table tr').length;
var clone_tr = $('table tr:first').clone();
clone_tr.find('input').val('');
clone_tr.find('input:eq(0)').attr('name', 'data[OrderDetail][' + next_id + '][product_code]');
clone_tr.find('input:eq(1)').attr('name', 'data[OrderDetail][' + next_id + '][product_name]');
clone_tr.find('input:eq(2)').attr('name', 'data[OrderDetail][' + next_id + '][product_qty]');
clone_tr.find('input:eq(3)').attr('name', 'data[OrderDetail][' + next_id + '][product_price]');
$('table tbody').append(clone_tr);
return false;
});
I think you will need do this for id attribute too.
Check this jsFiddle. I hope it helps.
Upvotes: 0