You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
4.3 KiB

4 years ago
  1. var nextRowId=1;
  2. function renumberTableRows(){
  3. var row_no=1;
  4. var table = document.getElementById("tabbody");
  5. for (var i = 0, row; row = table.rows[i]; i++) {
  6. row.cells[0].innerHTML=row_no++;
  7. }
  8. }
  9. function deleteTableRow(rowId){
  10. var target = document.getElementById("upload-target"+rowId);
  11. var children = target.children;
  12. // delete all uploaded elements
  13. for (var i = 0; i < children.length; i++) {
  14. var tmp = children[i].getAttribute("data-id");
  15. $K.ajax.post({
  16. url: 'delete.php',
  17. data: 'id='+tmp,
  18. before: function(xhr) {},
  19. success: function(response) {},
  20. error: function(response) {}
  21. });
  22. }
  23. // delete the row from table
  24. var element = document.getElementById("trow"+rowId);
  25. element.parentNode.removeChild(element);
  26. renumberTableRows();
  27. // alert(rowId);
  28. }
  29. function addTableRow(){
  30. var $node = $K.dom('#tabbody');
  31. // create a new table row with id
  32. var element = document.createElement('tr');
  33. element.setAttribute("id", "trow"+nextRowId);
  34. var e="";
  35. // add column for position no
  36. e += '<td></td>';
  37. // add coluemn for description
  38. e += `<td><textarea name="desc[${nextRowId}]" rows="2"></textarea> </td>`;
  39. // add column for date
  40. e += `<td><input name="date[${nextRowId}]" type="date"/></td>`;
  41. // add column for num
  42. e += `<td><input name="amount[${nextRowId}]" type="num"/></td>`;
  43. // add column for file upload
  44. e+= `
  45. <td data-kube="">
  46. <div class="form-item">
  47. <div class="upload"
  48. data-kube="upload"
  49. data-type="file" data-multiple="true"
  50. data-target="#upload-target${nextRowId}"
  51. data-url="upload.php?row=${nextRowId}" data-url-remove="delete.php"
  52. data-progress="true">
  53. </div>
  54. <div id="upload-target${nextRowId}" class="upload-target"></div>
  55. </div>
  56. </td>
  57. `;
  58. // add delete bnutton
  59. e +=
  60. `<td><span
  61. onclick="deleteTableRow(${nextRowId})"
  62. class="close is-large">
  63. </span></td>`;
  64. $node.append(element);
  65. element.innerHTML=(e);
  66. renumberTableRows();
  67. nextRowId++;
  68. }
  69. $K.add('module','kaform', {
  70. init: function(app,context){
  71. this.app = app;
  72. // getting context and the module element
  73. this.context = context;
  74. this.$element = context.getElement();
  75. },
  76. hello: function(){
  77. alert ("hello function of kaform");
  78. },
  79. start: function()
  80. {
  81. /*this.$element.on('keydown.kube.editable', this._hui.bind(this));
  82. this.$element.on('paste.kube.editable', this._hui.bind(this));
  83. this.$element.on('blur.kube.editable', this._hui.bind(this));
  84. */
  85. // alert(this.$element);
  86. // this.$element.on('click', this.addRow.bind(this));
  87. this._build();
  88. },
  89. addRow: function()
  90. {
  91. var $node = $K.dom('#tabbody');
  92. // create a new table row with id
  93. var element = document.createElement('tr');
  94. element.setAttribute("id", "trow"+nextRowId);
  95. var e="";
  96. // add column for position no
  97. e += '<td></td>';
  98. // add coluemn for description
  99. e += '<td><textarea rows="2"></textarea> </td>';
  100. // add column for date
  101. e += '<td style="overflow:visible;"><input type="date"/></td>';
  102. // add column for num
  103. e += '<td style="overflow:visible;"><input type="num"/></td>';
  104. // add column for file upload
  105. e+= `
  106. <td data-kube="">
  107. <form action="">
  108. <div class="form-item">
  109. <div class="upload"
  110. data-kube="upload"
  111. data-type="file" data-multiple="true"
  112. data-target="#upload-target${nextRowId}"
  113. data-url="upload.php" data-url-remove="delete.php"
  114. data-progress="true">
  115. </div>
  116. <div id="upload-target${nextRowId}" class="upload-target"></div>
  117. </div>
  118. </form>
  119. </td>
  120. `;
  121. // add delete bnutton
  122. e +=
  123. `<td><span
  124. onclick="deleteTableRow('trow${nextTableRow}'"
  125. class="close is-large">
  126. </span></td>`;
  127. $node.append(element);
  128. element.innerHTML=(e);
  129. renumberTableRows();
  130. nextRowId++;
  131. },
  132. // private
  133. _build: function()
  134. {
  135. },
  136. onmessage: {
  137. alert: {
  138. closed: function(sender)
  139. {
  140. alert(sender);
  141. // caught
  142. }
  143. }
  144. }
  145. });
  146. // startup is here
  147. $K.init({
  148. observer: true
  149. });
  150. // on startup clear session on server
  151. $K.ajax.get({
  152. url: 'clear.php',
  153. data: '', // or key=value object
  154. before: function(xhr) {},
  155. success: function(response) {},
  156. error: function(response) {}
  157. });
  158. // add on table row
  159. addTableRow();