-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java script Add remove Table Row
146 lines (138 loc) · 8.23 KB
/
Java script Add remove Table Row
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Html Code
<div class="row xdisplay_inputx form-group ro">
<label>
<h4>Medicine</h4>
</label>
<table>
<tr>
<td>
<select class='form-control' name="medicine[]" style='width: 170px;'>
<option selected hidden disabled>--Select Medicine--</option>
@foreach ($Medicines as $Medicine)
<option value='{{ $Medicine->id }}'>{{ $Medicine->name }}</option>
@endforeach
</select>
</td>
<input type="hidden" name="row_no" id="row_no" value="1">
<td>
<input type='text' name='dosage[]' class='form-control'
style='margin-left: 9px; width: 170px;' placeholder="100mg">
</td>
<td>
<input type='text' name='frequency[]' class='form-control'
style='margin-left: 9px; width: 170px;' placeholder="1+0+1">
</td>
<td>
<input type='text' name='days[]' class='form-control'
style='margin-left: 9px; width: 170px;' placeholder="7days">
</td>
<td>
<input type='text' name='instruction[]' class='form-control'
style='margin-left: 9px; width: 170px;' placeholder="After Food">
</td>
<td>
<button class='btn btn-success btn-sm add_field' type="button"
style='margin-left: 8px;'><i class="fa fa-plus-square"></i></button>
</td>
</tr>
</table>
<div class="input_field"></div>
</div>
// JS Code
{{-- Java script Add remove Table Row --}}
<script type="text/javascript">
$(document).ready(function() {
var max_field = 10;
var wrapper = $(".input_field");
var add_field = $(".add_field");
var i = 1;
$(add_field).click(function(e) {
e.preventDefault();
if (i < max_field) {
i++;
$("#row_no").val(i);
$(wrapper).append("<div>\
<table>\
<tr>\
<td>\
<select class='form-control' name='medicine[]' style='width: 170px;'>\
<option selected hidden disabled>Select</option>\
@foreach ($Medicines as $Medicine)\
<option value='{{ $Medicine->id }}'>{{ $Medicine->name }}</option>\
@endforeach\
</select>\
</td>\
<td>\
<input type='text' name='dosage[]' class='form-control' style='margin-left: 9px; width:170px;' placeholder='100mg'>\
</td>\
<td>\
<input type='text' name='frequency[]' class='form-control' style='margin-left: 9px; width:170px;' placeholder='1+0+1'>\
</td>\
<td>\
<input type='text' name='days[]' class='form-control' style='margin-left: 9px; width: 170px;' placeholder='7days'>\
</td>\
<td>\
<input type='text' name='instruction[]' class='form-control' style='margin-left: 9px; width: 170px;' placeholder='After Food'>\
</td>\
<td>\
<button class='btn btn-danger btn-sm remove_field' style='margin-left: 8px;'><i class='fa fa-trash'></i></button>\
</td>\
</tr>\
</table>\</div>");
}
});
$(wrapper).on("click", ".remove_field", function(e) {
e.preventDefault();
$(this).closest('div').remove();
i--;
$("#row_no").val(i);
});
});
</script>
// 2nd way only tr create
<table class="table input_field">
<tr>
<input type="hidden" name="row_no" id="row_no" value="1">
<td style='width:50%'>
<input type='text' name='items[]' class='form-control' placeholder="Items">
</td>
<td>
<input type='number' name='price[]' class='form-control number_field' placeholder="Price">
</td>
<td>
<button class='btn btn-success btn-sm add_field' type="button"><i class="fa fa-plus-square"></i></button>
</td>
</tr>
</table>
{{-- Java script Add remove Table Row --}}
<script type="text/javascript">
$(document).ready(function() {
var max_field = 10;
var wrapper = $(".input_field");
var add_field = $(".add_field");
var i = 1;
$(add_field).click(function(e) {
e.preventDefault();
if (i < max_field) {
i++;
$("#row_no").val(i);
$(wrapper).append("<tr>\
<td style='width:50%'>\
<input type='text' name='items[]' class='form-control' placeholder='Items'>\
</td>\
<td>\
<input type='number' name='price[]' class='form-control number_field' placeholder='Price'>\
</td>\
<td>\
<button class='btn btn-danger btn-sm remove_field'><i class='fa fa-trash'></i></button>\
</td>\
</tr>");
}
});
$(wrapper).on("click", ".remove_field", function(e) {
e.preventDefault();
$(this).closest('tr').remove();
i--;
});
});
</script>