When I fetch data in from datatabse to dynamic form it shows like this. How to force the second, third… and next rows to show DELETE button. I want the first row only to show Add and the rest is delete.
My blade code
JavaScript
x
12
12
1
@foreach ($form->requirements as $reqs)
2
<tr>
3
<td><input type="text" name="addMoreInputFields[0][requirement]"
4
placeholder="Enter requirements"
5
value="{{ $reqs['requirement'] }}"class="form-control" />
6
</td>
7
<td><button type="button" name="add" id="dynamic-ar"
8
class="btn btn-outline-primary">Add</button>
9
</td>
10
</tr>
11
@endforeach
12
the script
JavaScript
1
10
10
1
var i = 0;
2
$("#dynamic-ar").click(function() {
3
++i;
4
$("#dynamicAddRemove").append('<tr><td><input type="text" name="addMoreInputFields[' + i +
5
'][requirement]" placeholder="Enter requirements" class="form-control" /></td><td><button type="button" class="btn btn-outline-danger remove-input-field">Delete</button></td></tr>'
6
);
7
});
8
$(document).on('click', '.remove-input-field', function() {
9
$(this).parents('tr').remove();
10
});
Advertisement
Answer
You can use the $loop
variable made available to you by Laravel:
JavaScript
1
15
15
1
@foreach ($form->requirements as $reqs)
2
<tr>
3
<td><input type="text" name="addMoreInputFields[0][requirement]"
4
placeholder="Enter requirements"
5
value="{{ $reqs['requirement'] }}"class="form-control" />
6
</td>
7
<td><button type="button" name="add" id="dynamic-ar"
8
class="btn btn-outline-primary">Add</button>
9
</td>
10
@if (!$loop->first)
11
// your delete button code here
12
@endif
13
</tr>
14
@endforeach
15