/
index.html
161 lines (153 loc) · 5.97 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Whisky Collection</title>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" role="main">
<div class="jumbotron">
<h1>My Whisky Collection</h1>
<p>Just an example of simple CRUD application developed using Vert.x and Vertx Web.</p>
</div>
<div class="page-header">
<div class="row">
<div class="col-md-12">
<h1>My Whiskies</h1>
<button class="pull-right btn btn-primary product-add" data-action="add" data-toggle="modal"
data-target="#productModal">
<span class="glyphicon glyphicon-plus"></span> Add a new bottle
</button>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Origin</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="content">
<!-- filled using Ajax -->
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
$(function () {
load();
initModal();
});
function create(name, origin) {
$.post("/api/whiskies", JSON.stringify({name: name, origin: origin}), function () {
load();
}, "json");
}
function remove(id) {
$.ajax({
method: "DELETE",
url: "/api/whiskies/" + id
}).done(function () {
load();
});
}
function update(id, name, origin) {
$.ajax({
method: "PUT",
url: "/api/whiskies/" + id,
data: JSON.stringify({name: name, origin: origin})
}).done(function () {
load();
});
}
function load() {
$("#content").children().remove();
$.getJSON("/api/whiskies", function (data) {
$.each(data, function (key, val) {
$("<tr><td>" + val.id + "</td><td>" + val.name + "</td><td>" + val.origin + "</td>" +
"<td>" +
"<button data-action='edit' class='btn btn-primary btn-sm product-edit' " +
"data-toggle='modal' " +
"data-target='#productModal' " +
"data-name='" + val.name + "' " +
"data-origin='" + val.origin + "' " +
"data-id='" + val.id + "'>" +
"<span class='glyphicon glyphicon-pencil'></span>" +
"</button>" +
" " +
"<button class='btn btn-danger btn-sm product-delete' data-id='" + val.id + "'>" +
" <span class='glyphicon glyphicon-minus'></span>" +
"</button>" +
"</td>" +
"</tr>").appendTo("#content");
});
initCallbacks();
});
}
function initCallbacks() {
$(".product-delete").unbind().click(function() {
var id = $(this).data("id");
remove(id);
});
}
function initModal() {
$("#productModal").on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var action = button.data('action');
var id = button.data('id');
var productAction = $("#productAction");
productAction.unbind();
var modal = $(this);
if (action === "add") {
modal.find('.modal-title').text("Add a bottle");
modal.find('#product-name').val("");
modal.find('#product-origin').val("");
productAction.click(function () {
create($("#product-name").val(), $("#product-origin").val());
$('#productModal').modal('toggle');
});
} else {
modal.find('.modal-title').text("Edit a bottle");
modal.find('#product-name').val(button.data("name"));
modal.find('#product-origin').val(button.data("origin"));
productAction.click(function () {
update(id, $("#product-name").val(), $("#product-origin").val());
$('#productModal').modal('toggle');
});
}
})
}
</script>
<div class="modal fade" id="productModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
<h4 class="modal-title" id="productModalTitle">Add a bottle</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="product-name" class="control-label">Name:</label>
<input type="text" class="form-control" id="product-name">
</div>
<div class="form-group">
<label for="product-origin" class="control-label">Origin:</label>
<input type="text" class="form-control" id="product-origin">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="productAction" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</body>
</html>