edittable.js 3.1 KB

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