-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLab5.html
173 lines (173 loc) · 8.1 KB
/
Lab5.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
162
163
164
165
166
167
168
169
170
171
172
173
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<link rel="stylesheet" href="https://cdn.bootcss.com/materialize/0.97.6/css/materialize.css"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<style type="text/css">
.main {
margin: 5px auto;
max-width: 1080px;
}
.tran {
margin: 20px auto;
max-width: 1080px;
}
.rspd {
height: 500px;
}
</style>
</head>
<body>
<div id="app">
<!-- Header -->
<nav style="padding-left: 20px; padding-right: 20px;">
<div class="nav-wrapper">
<a class="brand-logo">School Database</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li v-for="table in tables"><a onclick="vm.navgateTable('{{table}}')">{{table}}</a></li>
<li><a onclick="gotoTran()">Transaction</a></li>
</ul>
</div>
</nav>
<!-- Table -->
<div class="main">
<table>
<thead>
<tr>
<template v-for="ctitle in ctitles">
<th data-field="{{ctitle.name}}">{{ctitle.name}}</th>
</template>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<template v-for="row in rows">
<tr>
<template v-for="ctitle in ctitles">
<td>{{row[ctitle.name]}}</td>
</template>
<td><a class="btn btn-small waves-effect waves-light" v-on:click="dropValue($index)"><i class="material-icons">delete</i></a></td>
</tr>
</template>
<tr>
<template v-for="ctitle in ctitles">
<td><input type="text" name="{{ctitle.name}}" placeholder="{{ctitle.def}}"></input></td>
</template>
<td><a class="btn btn-small waves-effect waves-light" v-on:click="updateValue"><i class="material-icons">send</i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="https://cdn.bootcss.com/vue/1.0.27/vue.common.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.12.2/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/materialize/0.97.6/js/materialize.min.js"></script>
<script>
var Data = {
currentTableName: 'No table selected.',
tables: [],
ctitles: [],
rows: []
}
var vm = new Vue({
el: '#app',
data: Data,
created: function () {
$.ajax({
type: 'GET',
url: 'http://localhost:2333/?query=show%20tables',
success: function(response) {
var data = JSON.parse(response);
Data.tables = data.map((a, b, c) => {
return a.Tables_in_school;
})
vm.navgateTable(Data.tables[Data.tables.length - 1]);
}
});
},
methods: {
navgateTable: function(table) {
$('.tran').css('display', 'none');
$('.main').css('display', '');
if (table)
this.currentTableName = table;
else
table = this.currentTableName;
$.ajax({
type: 'GET',
url: 'http://localhost:2333/?query=desc%20' + table,
success: function(response) {
var data = JSON.parse(response);
Data.ctitles = data.map((a, b, c) => {
return {name: a.Field, def: a.Default};
});
vm.flushContent(table);
}
});
},
flushContent: function(table) {
$.ajax({
type: 'GET',
url: 'http://localhost:2333/?query=select%20*%20from%20' + table,
success: function(response) {
Data.rows = JSON.parse(response);
}
});
},
dropValue: function(index) {
var sql = 'DELETE FROM ' + this.currentTableName + ' WHERE ' + this.currentTableName + '.' + this.ctitles[0].name + ' = \'' + this.rows[index][this.ctitles[0].name] + '\'';
$.ajax({
type: 'GET',
url: 'http://localhost:2333/?query=' + sql,
success: function(response) {
if (response.indexOf('errno') > 0) {
Materialize.toast('Server report a error.', 10000);
return;
}
vm.navgateTable(this.currentTableName);
Materialize.toast('Great success!', 4000);
}
});
},
updateValue: function() {
var content = '';
var tb = '';
// check if all the non-default blank has its value
for (var i = 0; i != this.ctitles.length; ++i) {
var val = document.getElementsByName(this.ctitles[i].name)[0].value;
if (val == '' && this.ctitles[i].def == null) {
Materialize.toast('Please fill all the blank without placeholder!', 4000);
return;
}
content += '\'';
if (val == '')
content += this.ctitles[i].def;
else
content += val;
content += '\'';
tb += '`' + this.ctitles[i].name + '`';
if (i != this.ctitles.length - 1) {
content += ', ';
tb += ', ';
}
}
var sql = 'INSERT%20INTO%20' + this.currentTableName + '(' + tb + ')' + '%20VALUES(' + content + ')';
$.ajax({
type: 'GET',
url: 'http://localhost:2333/?query=' + sql,
success: function(response) {
if (response.indexOf('errno') > 0) {
Materialize.toast('Server report a error.', 10000);
return;
}
vm.navgateTable(this.currentTableName);
Materialize.toast('Great success!', 4000);
}
});
}
}
});
</script>
</body>
</html>