I have here a laravel project with a dynamic table. The dynamic table is working fine. However, after submitting the form with an invalid input, the dynamically added table rows resets and I’m unable to retain old values.
Here’s the code for the table:
<div class="modal fade" id="addOrderModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title" id="exampleModalLabel">Deliver Items</h3>
<a class="btn" data-dismiss="modal" aria-label="Close">
<i class="bi bi-x-circle"></i>
</a>
</div>
<form method="POST" id="forms">
@csrf
<div class="modal-body">
<div class="mb-3">
<div class="form-row">
<div class="col">
<label for="location" class="block font-medium text-sm text-gray-700">Location</label>
<select name="location" id="location" class="form-control" required>
<option value="" selected disabled>Select Location</option>
<option value="ORTIGAS" {{ old('location') === 'ORTIGAS' ? 'selected' : '' }}>
ORTIGAS</option>
<option value="A-JUAN" {{ old('location') === 'A-JUAN' ? 'selected' : '' }}>A-JUAN
</option>
<option value="MARIKINA" {{ old('location') === 'MARIKINA' ? 'selected' : '' }}>
MARIKINA</option>
</select>
@error('location')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="col">
<label for="checkoutdate">Checkout date</label>
<input type="date" required name="checkoutdate" id="checkoutdate"
class="form-control" value="{{ old('checkoutdate', '') }}" />
@error('checkoutdate')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
</div>
<div class="mb-3">
<div class="form-row">
<div class="col">
<label for="client">Client</label>
<input type="text" required name="client" id="client" class="form-control"
value="{{ old('client', '') }}" placeholder="Enter client name" />
@error('client')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="col">
<label for="contact">Contact Person</label>
<input type="text" required name="contact" id="contact" class="form-control"
value="{{ old('contact', '') }}" placeholder="Enter contact person" />
@error('contact')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
</div>
<div class="mb-3">
<div class="form-row">
<div class="col">
<label for="site">Site/Event</label>
<input type="text" required name="site" id="site" class="form-control"
value="{{ old('site', '') }}" placeholder="Enter site/event" />
@error('site')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="col">
<label for="address">Address</label>
<input type="text" required name="address" id="address" class="form-control"
value="{{ old('address', '') }}" placeholder="Enter address" />
@error('address')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
</div>
<div class="mb-3">
<div class="form-row">
<div class="col">
<label for="srnumber">SR No.</label>
<input type="text" required name="srnumber" id="srnumber" class="form-control"
value="{{ old('srnumber', '') }}" placeholder="Enter SR no." />
@error('srnumber')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="col">
<label for="ponumber">PO No.</label>
<input type="text" required name="ponumber" id="ponumber" class="form-control"
value="{{ old('ponumber', '') }}" placeholder="Enter PO no." />
@error('ponumber')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
</div>
<div class="mb-3">
<div class="table-responsive text-nowrap">
@if ($errors->any())
<div class="alert alert-danger">
Check fields for invalid input.
</div>
@endif
<table class="table table-bordered" id="dynamicTable">
<thead class="table-dark">
<tr>
<th>SKU</th>
<th>Product Code</th>
<th>Model</th>
<th style="width:13%">Quantity</th>
<th>UOM</th>
<th>Item Description</th>
<th>Serial No.</th>
</tr>
</thead>
<tr>
<td>
<input type="text" name="addmore[0][sku]" placeholder="SKU"
value="{{ old('addmore.0.sku') }}" class="form-control" />
</td>
<td><input type="text" name="addmore[0][productcode]"
value="{{ old('addmore.0.productcode') }}" placeholder="Product Code"
class="form-control" /></td>
<td><input type="text" name="addmore[0][model]" placeholder="Model"
value="{{ old('addmore.0.model') }}" class="form-control" /></td>
<td><input type="number" name="addmore[0][quantity]" placeholder="Quantity"
value="{{ old('addmore.0.quantity') }}" class="form-control" /></td>
<td><select name="addmore[0][uom]" class="form-control">
<option value="UNIT/S"
{{ old('addmore.0.uom') === 'UNIT/S' ? 'selected' : '' }}>UNIT/S
</option>
<option value="PANEL/S"
{{ old('addmore.0.uom') === 'PANEL/S' ? 'selected' : '' }}>PANEL/S
</option>
<option value="PC/S"
{{ old('addmore.0.uom') === 'PC/S' ? 'selected' : '' }}>PC/S</option>
</select></td>
<td><input type="text" name="addmore[0][itemdescription]"
value="{{ old('addmore.0.itemdescription') }}"
placeholder="Item Description" class="form-control" /></td>
<td>
<textarea name="addmore[0][serialnumber]" class="form-control" cols="15" rows="1"
placeholder="Serial no.">{{ old('addmore.0.serialnumber') }}</textarea>
</td>
<td class="border border-white"><button type="button" name="add"
id="add" class="btn btn-success btn-sm"
style="background-color: #4D83FF; color: white; border-color: #4D83FF;"><i
class="bi bi-plus-circle"></i></button></td>
</tr>
</table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="submitButt" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
And here’s the script for the dynamic table:
<script type="text/javascript">
var i = 0;
$("#add").click(function() {
++i;
$("#dynamicTable").append('<tr><td><input type="text" name="addmore[' + i +
'][sku]" placeholder="SKU" class="form-control"/></td><td><input type="text" name="addmore[' +
i +
'][productcode]" placeholder="Product Code" class="form-control"/></td><td><input type="text" name="addmore[' +
i +
'][model]" placeholder="Model" class="form-control"/></td><td><input type="number" name="addmore[' +
i +
'][quantity]" placeholder="Quantity" class="form-control"/></td><td><select name="addmore[' +
i +
'][uom]" class="form-control"><option value="Units">Unit/s</option><option value="Panels">Panel/s</option><option value="Pcs">Pc/s</option></select></td><td><input type="text" name="addmore[' +
i +
'][itemdescription]" placeholder="Description" class="form-control"/></td><td><textarea name="addmore[' +
i +
'][serialnumber]" placeholder="Serial No." class="form-control" rows="1"></textarea></td><td class="border border-white"><button type="button" class="btn btn-danger btn-sm remove-tr" title="Remove item"><i class="bi bi-x-circle"></i></button></td></tr>'
);
event.preventDefault();
});
$(document).on('click', '.remove-tr', function() {
$(this).parents('tr').remove();
});
</script>
I tried many fixes but I still can’t make it work.