Salve,
ho un form dove con due tabelle correlate, prodotto e offerte.
Ogni prodotto ha più offerte e nel form devi premere sul + per aggiungere al volo una nuova riga.
Come posso fare per far si che al posto {n} mi compaia un numero uguale per tutti i campi della stessa riga e se premi aggiungi record, mi incrementi il numero progressivo per tutti i campi della nuova righa?
Codice PHP:
<table class="table table-hover small-text" id="tb">
<tr class="tr-header">
<th><?= __('Offer') ?></th>
<th><?= __('Old price') ?></th>
<th><?= __('New price') ?></th>
<th><?= __('Discount') ?></th>
<th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Person"><span class="glyphicon glyphicon-plus"></span></a></th>
</tr>
<tr>
<td><?php // echo $this->Form->input('offers.{n}.id'); ?>
<?php echo $this->Form->input('offers.{n}.name', ['class' => 'form-control', 'label' => false]); ?></td>
<td><?php echo $this->Form->input('offers.{n}.start_price', ['class' => 'form-control', 'label' => false]); ?></td>
<td><?php echo $this->Form->input('offers.{n}.end_price', ['class' => 'form-control', 'label' => false]); ?></td>
<td><?php echo $this->Form->input('offers.{n}.discount', ['class' => 'form-control', 'label' => false]); ?></td>
<td><a href='javascript:void(0);' class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
</tr>
</table>
<script>
$(function () {
$('#addMore').on('click', function () {
var data = $("#tb tr:eq(1)").clone(true).appendTo("#tb");
data.find("input").val('');
});
$(document).on('click', '.remove', function () {
var trIndex = $(this).closest("tr").index();
if (trIndex > 1) {
$(this).closest("tr").remove();
} else {
alert("Sorry!! Can't remove first row!");
}
});
});
</script>