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.

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