-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabledata.html
More file actions
203 lines (176 loc) · 7.05 KB
/
tabledata.html
File metadata and controls
203 lines (176 loc) · 7.05 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!doctype html>
<html lang="en">
<head>
<title>Table Data</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
.active {
background-color: darkgreen;
color: #fff;
}
.error-valid{
color: red;
padding-top: 8px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container mt-4">
<h1 class="mb-4">Table data for AJAX</h1>
<div class="row">
<div class="col-lg-4">
<form name="adddata_form" method="post" action="adddata.php">
<div class="form-group">
<div class="col">
<input type="text" id="name" name="name" class="form-control" placeholder="Product name">
</div>
</div>
<div class="form-group">
<div class="col">
<input type="text" id="price" name="price" class="form-control" placeholder="Price">
</div>
</div>
<div class="form-group">
<div class="col">
<input type="number" id="qty" name="qty" class="form-control" placeholder="Qty">
</div>
</div>
<div class="form-group">
<div class="col">
<input type="submit" class="btn btn-success btn-block" value="Submit">
</div>
</div>
</form>
</div>
<div class="col-lg-8">
<table class="table table-bordered">
<thead>
<tr class="bg-info">
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Qty</th>
<th>Manage</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<!--Initial jquery function-->
<script>
$(function () {
// Selector
$('h1')
.css('color', 'white')
.css('background-color', 'brown');
// Mouse Over
$('table tbody tr').mouseover(function () {
$(this).addClass('active');
});
// Mouse Out
$('table tbody tr').mouseout(function () {
$(this).removeClass('active');
});
// รับค่าจากฟอร์ม
// validate form
$("form[name=adddata_form]").validate({
rules:{
name:{
required:true
},
price:{
required:true,
number: true
},
qty:{
required:true
}
},
messages:{
name:{
required:"กรุณากรอกชื่อสินค้าก่อน"
},
price:{
required:"กรุณากรอกราคาสินค้าก่อน",
number: "ราคาต้องเป็นตัวเลขเท่านั้น"
},
qty:{
required:"ป้อนจำนวนสินค้าก่อน"
}
},
errorClass:"error-valid",
errorPlacement:function(error,element){
error.appendTo(element.parent());
},
submitHandler:function(form){
// AJAX
var product_name = $('#name').val();
var product_price = $('#price').val();
var product_qty = $('#qty').val();
// นับจำนวนแถว
var row = $('table tbody tr').length;
var trstring = "<tr>"+
"<td>"+(row+1)+"</td>"+
"<td>"+product_name+"</td>"+
"<td>"+product_price+"</td>"+
"<td>"+product_qty+"</td>"+
"<td>"+
" <a href='#' id='btnedit' class='btn btn-warning'>Edit</a>"+
" <a href='#' id='btndel' class='btn btn-danger'>Delete</a>"+
"</td>"+
"</tr>";
$('table tbody').append(trstring);
// clear form
$('form').trigger('reset');
// focus input name
$('#name').focus();
} //submitHandler
}); // validate form
// event delete data from table
$('body').on('click','a#btndel',function(){
// deleate row
// $(this).parent().parent().remove();
$(this).closest('tr').remove();
});
// event edit data from table
$('body').on('click','a#btnedit',function(){
// call modal
$('#modal-edit').modal('show');
});
});
</script>
<!-- Modal -->
<div class="modal fade" id="modal-edit" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Edit Product</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h1>Edit Product</h1>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>
</html>