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.

224 lines
4.5 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. function submitTable(){
  70. alert("submit");
  71. var obj;
  72. var table = document.getElementById("tabbody");
  73. for (var i = 0, row; row = table.rows[i]; i++) {
  74. console.log(row.cells[1]);
  75. // var n = row.cells[1].value;
  76. // alert(n);
  77. }
  78. }
  79. $K.add('module','kaform', {
  80. init: function(app,context){
  81. this.app = app;
  82. // getting context and the module element
  83. this.context = context;
  84. this.$element = context.getElement();
  85. },
  86. hello: function(){
  87. alert ("hello function of kaform");
  88. },
  89. start: function()
  90. {
  91. /*this.$element.on('keydown.kube.editable', this._hui.bind(this));
  92. this.$element.on('paste.kube.editable', this._hui.bind(this));
  93. this.$element.on('blur.kube.editable', this._hui.bind(this));
  94. */
  95. // alert(this.$element);
  96. // this.$element.on('click', this.addRow.bind(this));
  97. this._build();
  98. },
  99. addRow: function()
  100. {
  101. var $node = $K.dom('#tabbody');
  102. // create a new table row with id
  103. var element = document.createElement('tr');
  104. element.setAttribute("id", "trow"+nextRowId);
  105. var e="";
  106. // add column for position no
  107. e += '<td></td>';
  108. // add coluemn for description
  109. e += '<td><textarea rows="2"></textarea> </td>';
  110. // add column for date
  111. e += '<td style="overflow:visible;"><input type="date"/></td>';
  112. // add column for num
  113. e += '<td style="overflow:visible;"><input type="num"/></td>';
  114. // add column for file upload
  115. e+= `
  116. <td data-kube="">
  117. <form action="">
  118. <div class="form-item">
  119. <div class="upload"
  120. data-kube="upload"
  121. data-type="file" data-multiple="true"
  122. data-target="#upload-target${nextRowId}"
  123. data-url="upload.php" data-url-remove="delete.php"
  124. data-progress="true">
  125. </div>
  126. <div id="upload-target${nextRowId}" class="upload-target"></div>
  127. </div>
  128. </form>
  129. </td>
  130. `;
  131. // add delete bnutton
  132. e +=
  133. `<td><span
  134. onclick="deleteTableRow('trow${nextTableRow}'"
  135. class="close is-large">
  136. </span></td>`;
  137. $node.append(element);
  138. element.innerHTML=(e);
  139. renumberTableRows();
  140. nextRowId++;
  141. },
  142. // private
  143. _build: function()
  144. {
  145. },
  146. onmessage: {
  147. alert: {
  148. closed: function(sender)
  149. {
  150. alert(sender);
  151. // caught
  152. }
  153. }
  154. }
  155. });
  156. // startup is here
  157. $K.init({
  158. observer: true
  159. });
  160. // on startup clear session on server
  161. $K.ajax.get({
  162. url: 'clear.php',
  163. data: '', // or key=value object
  164. before: function(xhr) {},
  165. success: function(response) {},
  166. error: function(response) {}
  167. });
  168. // add on table row
  169. addTableRow();