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
|
// Globals
// -----------------------------------------------------------------------------
var petitions = [];
var table;
// Supplamentary functions
// -----------------------------------------------------------------------------
// array.indexOf polyfill
// --- -- -- - - -
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
// return petitions user by user-id
// --- -- -- - - -
function user_record(id) {
var record = false;
petitions.forEach(el => { if (el.id == id) record = el; });
return record;
}
// create actions html (edit and delete buttons)
// --- -- -- - - -
function create_actions_html(el) {
if (el.deprecated == null) {
icon = '<i class="fa-solid fa-minus"></i>';
action = 'deprecate';
title = 'κατάργηση';
type = 'warning';
} else {
icon = '<i class="fa-solid fa-plus"></i>';
action = 'activate';
title = 'προσθήκη';
type = 'primary';
}
return [
'<button type="button" class="btn btn-xs btn-'+ type +'" title="'+ title +'"',
'data-id="'+ el.id +'" data-action="'+ action +'" >',
icon,
'</button>'
].join('\n');
}
$(document).ready(function() {
// When document is ready
// -------------------------------------------------------------------------
/** init_table
* --- -- -- - - -
* get categories
* constuct categories array
* render dataTable
*/
function init_table() {
// get all categories from server (ajax)
$.getJSON( "/admin/get/users")
.done(function( json ) {
// then ...
console.log(json);
// reset categories
petitions.length = 0;
// construct petitions data
json.forEach( el => {
rec = {
id: parseInt(el.id),
first_name: el.first_name,
last_name: el.last_name,
email: el.email,
sector: el.sector,
status: (el.deprecated == 1)
? ('<i class="fa-regular fa-eye-slash"></i> / deprecated')
: ('<i class="fa-regular fa-eye"></i> / ' + ((el.active == 1)
? 'active'
: 'pending')
),
deprecated: el.deprecated
}
rec.actions = create_actions_html(rec);
petitions.push(rec);
});
// destroy previous datatable table instances
$('#dt-petitions').dataTable().fnClearTable();
$('#dt-petitions').dataTable().fnDestroy();
// (re-)create categories datatable
table = $('#dt-petitions').DataTable({
language: { url: '/assets/js/DataTables/localization/Greek.json' },
data: petitions,
// ordering: false,
columns: [
{ data: 'last_name' },
{ data: 'first_name'},
{ data: 'email' },
{ data: 'sector' },
{ data: 'status' },
{ data: 'actions', class: 'dt-actions' }
]
});
})
.fail(function( jqxhr, textStatus, error ) {
console.log( "Request Failed (" + error +")" );
});
}
init_table(); // init table on first run
/** When click to action button
* -------------------------------------------------------------------------
*/
$('body').on('click', '.dt-actions button', event => {
event.preventDefault();
var button;
if ($(event.target).data('id')) { // click on button
button = $(event.target)
} else { // click on button's content
button = $(event.target).parent('button');
}
var id = button.data('id'); // user-id from data-id
var action = button.data('action'); // action to take
console.log(action, id);
var request = '/admin/user/'+ action +'/'+ id;
$.get(request)
.done(function(data) {
init_table();
});
})
});
|