-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava script calculation dynamic add row value
More file actions
83 lines (70 loc) · 2.85 KB
/
Java script calculation dynamic add row value
File metadata and controls
83 lines (70 loc) · 2.85 KB
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
// value Calculation ( dynamic add row value and delete row remove value from total)
// table
<table class="table input_field" id="calculation">
<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 item_price' 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>
// Table dynamic add row or delete
$(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 item_price' 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--;
$("#row_no").val(i);
});
});
</script>
// Create Calculation in J query
$('#calculation').on("keyup", ".item_price", function () {
var sum = 0;
$('.item_price').each(function () {
sum += Number($(this).val());
});
$('#Item_total_price').html(sum);
console.log(sum);
});
// delete row remove value from total
add following code in delete row j query code
$(wrapper).on("click", ".remove_field", function(e) {
e.preventDefault();
$(this).closest('tr').remove();
i--;
var sum = 0;
$('.item_price').each(function () {
sum += Number($(this).val());
});
$('#Item_total_price').html(sum);
console.log(sum);
});