// 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 = ''; action = 'deprecate'; title = 'κατάργηση'; type = 'warning'; } else { icon = ''; action = 'activate'; title = 'προσθήκη'; type = 'primary'; } return [ '' ].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) ? (' / deprecated') : (' / ' + ((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(); }); }) });