edittable.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {AjaxReq, fetchHeaders} from "./OMGUtils";
  2. let CSS_EDITOR_ACTIVE = 'shadow rounded-lg bg-dark text-light';
  3. let CSS_EDITOR_ROW_ACTIVE = 'bg-info';
  4. let _tableSelector = null;
  5. let _controllerURL = '';
  6. function GetTableFieldNames() {
  7. let columnNames = [];
  8. $(`${_tableSelector} thead th`).each(function (i, e) {
  9. columnNames.push(e.innerText);
  10. });
  11. return columnNames;
  12. }
  13. function GetColumnDef(colIndex, name) {
  14. return $(`${_tableSelector} thead th:eq(${colIndex})`).attr(name);
  15. }
  16. function cancelEdit(el) {
  17. el.innerText = $(el).data('oldvalue');
  18. $(el).parent('tr').removeClass(`dt-editing-cell ${CSS_EDITOR_ROW_ACTIVE}`);
  19. $(el).removeClass(CSS_EDITOR_ACTIVE);
  20. $('body').focus();
  21. el.contentEditable = false;
  22. }
  23. function EditTableInit(tableRef, controllerURL) {
  24. _tableSelector = tableRef;
  25. _controllerURL = controllerURL;
  26. let tableBodyRef = $(`${_tableSelector} tbody`);
  27. $(tableRef).DataTable({
  28. "ajax": {
  29. "url": _controllerURL,
  30. "type": 'POST',
  31. "data": {
  32. 'action': 'getData'
  33. },
  34. 'dataSrc': function (data) {
  35. $(_tableSelector).parent('.dataTables_wrapper').parent().removeClass('d-none');
  36. return data;
  37. }
  38. },
  39. 'lengthChange': false,
  40. dom: 'frtBip', // for buttons
  41. order: [[0, "asc"]],
  42. buttons: [
  43. {
  44. text: 'Completa lista',
  45. action: function (e, dt, node, config) {
  46. let postHeadersContentType = fetchHeaders;
  47. postHeadersContentType['Content-Type'] = 'application/x-www-form-urlencoded';
  48. AjaxReq(_controllerURL, {
  49. headers: postHeadersContentType,
  50. method: 'POST',
  51. body: 'action=closelist'
  52. }).fetchPromise()
  53. .then(json => alert(`${json.code} - ${json.err}`))
  54. .catch(err => alert(err.url, `${err.status} (${err.statusText})`));
  55. }
  56. },
  57. {
  58. text: 'Annulla lista',
  59. action: function (e, dt, node, config) {
  60. let postHeadersContentType = fetchHeaders;
  61. postHeadersContentType['Content-Type'] = 'application/x-www-form-urlencoded';
  62. AjaxReq(_controllerURL, {
  63. headers: postHeadersContentType,
  64. method: 'POST',
  65. body: 'action=cancellist'
  66. }).fetchPromise()
  67. .then(json => alert(`${json.code} - ${json.err}`))
  68. .catch(err => alert(err.url, `${err.status} (${err.statusText})`));
  69. }
  70. }
  71. ]
  72. });
  73. tableBodyRef.on('click', 'td', function () {
  74. let $this = $(this);
  75. // avoid to edit if aria-editable === 'false'
  76. if (GetColumnDef(this.cellIndex, 'aria-editable') === 'false')
  77. return;
  78. this.contentEditable = true; // enable html edit
  79. $this.data('oldvalue', this.innerText); // backup actual value
  80. $this.parent('tr').addClass(`dt-editing-cell ${CSS_EDITOR_ROW_ACTIVE}`); // mark the row of current cell
  81. $this.addClass(CSS_EDITOR_ACTIVE);
  82. $this.focus();
  83. });
  84. tableBodyRef.on('blur', 'td', function (e) {
  85. cancelEdit(this);
  86. });
  87. tableBodyRef.on('keydown', 'td', function (e) {
  88. // Cancel Edit
  89. if (e.keyCode === 27 || e.keyCode === 9) {
  90. cancelEdit(this);
  91. return false;
  92. }
  93. // Save Modified Data
  94. if (e.keyCode === 13) {
  95. $(this).data('oldvalue', this.innerText);
  96. let ajaxData = new FormData();
  97. let fields = GetTableFieldNames();
  98. $('.dt-editing-cell td').each(function (i, e) {
  99. ajaxData.append(fields[i], e.innerText);
  100. });
  101. ajaxData.append('action', 'edit');
  102. let postHeadersContentType = fetchHeaders;
  103. delete postHeadersContentType['Content-Type'];
  104. AjaxReq(_controllerURL, {
  105. headers: postHeadersContentType,
  106. method: 'POST',
  107. body: ajaxData
  108. }).fetchPromise()
  109. .then(json => {
  110. $(tableRef).DataTable().ajax.reload();
  111. alert(`${json.code} - ${json.err}`);
  112. })
  113. .catch(err => alert(err.url, `${err.status} (${err.statusText})`))
  114. .finally(() => this.contentEditable = false);
  115. return false;
  116. }
  117. });
  118. }
  119. export default EditTableInit;