import {AjaxReq, fetchHeaders} from "./OMGUtils"; let CSS_EDITOR_ACTIVE = 'shadow rounded-lg bg-dark text-light'; let CSS_EDITOR_ROW_ACTIVE = 'bg-info'; let _tableSelector = null; let _controllerURL = ''; function GetTableFieldNames() { let columnNames = []; $(`${_tableSelector} thead th`).each(function (i, e) { columnNames.push(e.innerText); }); return columnNames; } function GetColumnDef(colIndex, name) { return $(`${_tableSelector} thead th:eq(${colIndex})`).attr(name); } function cancelEdit(el) { el.innerText = $(el).data('oldvalue'); $(el).parent('tr').removeClass(`dt-editing-cell ${CSS_EDITOR_ROW_ACTIVE}`); $(el).removeClass(CSS_EDITOR_ACTIVE); $('body').focus(); el.contentEditable = false; } function EditTableInit(tableRef, controllerURL) { _tableSelector = tableRef; _controllerURL = controllerURL; let tableBodyRef = $(`${_tableSelector} tbody`); $(tableRef).DataTable({ "ajax": { "url": _controllerURL, "type": 'POST', "data": { 'action': 'getData' }, 'dataSrc': function (data) { $(_tableSelector).parent('.dataTables_wrapper').parent().removeClass('d-none'); return data; } }, 'lengthChange': false, dom: 'frtBip', // for buttons order: [[0, "asc"]], buttons: [ { text: 'Completa lista', action: function (e, dt, node, config) { let postHeadersContentType = fetchHeaders; postHeadersContentType['Content-Type'] = 'application/x-www-form-urlencoded'; AjaxReq(_controllerURL, { headers: postHeadersContentType, method: 'POST', body: 'action=closelist' }).fetchPromise() .then(json => alert(`${json.code} - ${json.err}`)) .catch(err => alert(err.url, `${err.status} (${err.statusText})`)); } }, { text: 'Annulla lista', action: function (e, dt, node, config) { let postHeadersContentType = fetchHeaders; postHeadersContentType['Content-Type'] = 'application/x-www-form-urlencoded'; AjaxReq(_controllerURL, { headers: postHeadersContentType, method: 'POST', body: 'action=cancellist' }).fetchPromise() .then(json => alert(`${json.code} - ${json.err}`)) .catch(err => alert(err.url, `${err.status} (${err.statusText})`)); } } ] }); tableBodyRef.on('click', 'td', function () { let $this = $(this); // avoid to edit if aria-editable === 'false' if (GetColumnDef(this.cellIndex, 'aria-editable') === 'false') return; this.contentEditable = true; // enable html edit $this.data('oldvalue', this.innerText); // backup actual value $this.parent('tr').addClass(`dt-editing-cell ${CSS_EDITOR_ROW_ACTIVE}`); // mark the row of current cell $this.addClass(CSS_EDITOR_ACTIVE); $this.focus(); }); tableBodyRef.on('blur', 'td', function (e) { cancelEdit(this); }); tableBodyRef.on('keydown', 'td', function (e) { // Cancel Edit if (e.code === 27 || e.code === 9) { cancelEdit(this); return false; } // Save Modified Data if (e.code === 13) { $(this).data('oldvalue', this.innerText); let ajaxData = new FormData(); let fields = GetTableFieldNames(); $('.dt-editing-cell td').each(function (i, e) { ajaxData.append(fields[i], e.innerText); }); ajaxData.append('action', 'edit'); let postHeadersContentType = fetchHeaders; delete postHeadersContentType['Content-Type']; AjaxReq(_controllerURL, { headers: postHeadersContentType, method: 'POST', body: ajaxData }).fetchPromise() .then(json => { $(tableRef).DataTable().ajax.reload(); alert(`${json.code} - ${json.err}`); }) .catch(err => alert(err.url, `${err.status} (${err.statusText})`)) .finally(() => this.contentEditable = false); return false; } }); } export default EditTableInit;