-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableData.js
More file actions
206 lines (181 loc) · 5.78 KB
/
Copy pathtableData.js
File metadata and controls
206 lines (181 loc) · 5.78 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
204
205
206
function calculate() {
var game = document.getElementById("dropdownInput")
var itemName = document.getElementById('itemName').value
var cost = document.getElementById("cost").value
var finalCost
if(game.value === "general"){
finalCost = cost*1.05
}else{
finalCost = cost*1.15
}
addRow(itemName, finalCost)
}
function updateData(id, itemName, sellp){
$.post("./controllers/itemController.php",
{
update:"upd",
id: id,
itemName: itemName,
sellp:sellp
})
}
function deleteData(id){
$.post("./controllers/itemController.php",
{
delete:"del",
item_id: id
})
}
function addRow(name, finalCost) {
finalCost = +finalCost.toFixed(2);
$('#resultTable').find('tbody').append('<tr>\
<td>'+name+'</td>\
<td>'+finalCost+'</td>\
<td>\
<button type="submit" class="Edit-button"><ion-icon class="editIcon" name="create-outline"></ion-icon></button>\
<button class="Delete-button"><ion-icon name="trash-outline"></ion-icon></button>\
</td></tr>');
}
function totalResult(){
$(document).ready(function(){
var currentTR = $('#resultTable').find('tbody').find('tr');
var totalValue = 0;
$.each(currentTR, function(){
totalValue += parseFloat($(this).find('td:nth-child(2)').text())
})
$(this).find('#result').text("Rp." + totalValue);
})
}
function checkLoginStatus(callback){
$.post("./controllers/AuthController.php",
{
loginStatus:"?",
},function(data){
if(data === "isLoggedIn"){
callback(true);
}else{
callback(false);
}
}
)
}
$(document).on("click", ".Edit-button", function () {
var currentTD = $(this).parents('tr').find('td');
var icon = currentTD.find('.editIcon');
if (icon.attr("name") == "create-outline") {
icon.attr("name", "checkmark-outline");
currentTD = $(this).parents('tr').find('td');
$.each(currentTD, function () {
$(this).prop('contenteditable', true)
});
} else {
icon.attr("name", "create-outline");
$.each(currentTD, function () {
$(this).prop('contenteditable', false)
});
var upTD = this;
checkLoginStatus(function (param) {
if(param == true){
var name = $(upTD).parents('tr').find('td:first-child').text();
var sellp = $(upTD).parents('tr').find('td:nth-child(2)').text();
var id = $(upTD).val();
updateData(id, name, sellp);
}
})
}
});
$(document).on("click", ".Delete-button", function () {
var currentTR = $(this).parents('tr');
var id = $(this).val();
currentTR.remove();
checkLoginStatus(function(param){
if(param==true){
deleteData(id);
}
})
});
function exportTable(){
let rows = document.getElementsByTagName('tr');
let cells;
let csv = "";
let csvSeparator = ","; // Sets the separator between fields
let quoteField = false; // Adds quotes around fields
// let regex = /.*<img.*src="(.*?)"/i
for (let row = 0; row < rows.length; row++) {
cells = rows[row].getElementsByTagName('td');
if (cells.length === 0) {
cells = rows[row].getElementsByTagName('th');
}
for (let cell = 0; cell < (cells.length)-1; cell++) {
if (quoteField) { csv += '"'; }
csv += cells[cell].innerText;
if (quoteField) { csv += '"'; }
if (cell === cells.length - 2) {
csv += "\n";
} else {
csv += csvSeparator;
}
}
}
downloadToFile(csv, 'data.csv', 'text/plain')
}
function downloadToFile(content, filename, contentType) {
const a = document.createElement('a');
const file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = filename;
// // To generate a link, use this:
// a.innerHTML = "Download CSV";
// document.body.appendChild(a);
// If you want to automatically download then use this instead:
a.click();
URL.revokeObjectURL(a.href);
}
// $(document).on("keyup", "#searchItem", function(){
// alert("WOARHHH")
// })
// var $rows = $('#resultTable').find('tr');
// $(document).on("keyup", "#searchItem", debounce(function() {
// var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
// reg = RegExp(val, 'i'),
// text;
// $rows.show().filter(function() {
// text = $(this).text().replace(/\s+/g, ' ');
// alert("WOI")
// return !reg.test(text);
// }).hide();
// }, 300));
$(document).on("keyup", "#searchItem", debounce(function() {
var input, filter, table, tr, td, i;
input = $("#searchItem");
filter = input.val().toUpperCase();
table = $("#resultTable");
tr = table.find("tbody").find("tr");
tr.each(function () {
var linha = $(this);
$(this).find('td:first-child').each(function () {
td = $(this);
if (td.html().toUpperCase().indexOf(filter) > -1) {
linha.show();
return false;
} else {
linha.hide();
}
})
})
},300))
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};