Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Spreadsheet</title>
  5. <style type="text/css">
  6. td {
  7. padding:3px;
  8. }
  9. tr:hover {
  10. background-color: yellow;
  11. }
  12. }
  13. </style>
  14. </head>
  15.  
  16. <body>
  17. <table border="0" cellpadding="0" cellspacing="0" id = "table">
  18. <thead>
  19. <tr>
  20. <th>Name</th>
  21. <th>Student ID</th>
  22. <th>Faculty</th>
  23. <th>Major</th>
  24. <th>Minor</th>
  25. <th>Year</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tr id = "row1">
  30. <td><input type="text" /></td>
  31. <td><input type="text" /></td>
  32. <td><input type="text" /></td>
  33. <td><input type="text" /></td>
  34. <td><input type="text" /></td>
  35. <td><input type="text" /></td>
  36. <td>
  37. <button onclick="swapFunction(this, 'Up')">Up</button>
  38. <button onclick="swapFunction(this, 'Down')">Dn</button>
  39. <button onclick = "delFunction(this)">Del</button>
  40. </td>
  41. </tr>
  42. <tr id = "row2">
  43. <td><input type="text" /></td>
  44. <td><input type="text" /></td>
  45. <td><input type="text" /></td>
  46. <td><input type="text" /></td>
  47. <td><input type="text" /></td>
  48. <td><input type="text" /></td>
  49. <td>
  50. <button onclick="swapFunction(this, 'Up')">Up</button>
  51. <button onclick="swapFunction(this, 'Down')">Dn</button>
  52. <button onclick = "delFunction(this)">Del</button>
  53. </td>
  54. </tr>
  55. <tr id = "row3">
  56. <td><input type="text" /></td>
  57. <td><input type="text" /></td>
  58. <td><input type="text" /></td>
  59. <td><input type="text" /></td>
  60. <td><input type="text" /></td>
  61. <td><input type="text" /></td>
  62. <td>
  63. <button onclick="swapFunction(this, 'Up')">Up</button>
  64. <button onclick="swapFunction(this, 'Down')">Dn</button>
  65. <button onclick = "delFunction(this)">Del</button>
  66. </td>
  67. </tr>
  68. </table>
  69. <button onclick = "newRowBtn()">New Row</button>
  70. <script>
  71. var counter = 4;
  72.  
  73. function delFunction(oButton) {
  74. var table = document.getElementById("table");
  75. var index = oButton.parentElement.parentElement.rowIndex;
  76. table.deleteRow(index);
  77. counter--;
  78. }
  79.  
  80. function swapFunction(oButton, direction){
  81. var table = document.getElementById("table");
  82. if (direction === "Down"){
  83. if(oButton.parentElement.parentElement.rowIndex === counter){
  84. alert("error");
  85. }
  86. table.insertBefore(oButton.parentElement.parentElement, oButton.parentElement.parentElement.rowIndex + 1);
  87. }
  88. if (direction === "Up"){
  89. if(oButton.parentElement.parentElement.rowIndex === 1){
  90. alert("error");
  91. }
  92. table.insertBefore(oButton.parentElement.parentElement, oButton.parentElement.parentElement.rowIndex - 1);
  93. }
  94. }
  95.  
  96.  
  97.  
  98. function newRowBtn() {
  99. var table = document.getElementById("table");
  100. var node = document.createElement("tr");
  101. for (var i=0;i<6;i++){
  102.  
  103. var td = document.createElement("td");
  104. var inputTag = document.createElement("input");
  105. inputTag.setAttribute("type","text");
  106.  
  107. td.appendChild(inputTag);
  108.  
  109. node.appendChild(td);
  110.  
  111.  
  112. var tdBtn1 = document.createElement("td");
  113. var btn1 = document.createElement("button");
  114. btn1.innerHTML = "Up";
  115. btn1.setAttribute("onclick", "swapFunction(this,'Up')");
  116. tdBtn1.appendChild(btn1);
  117.  
  118. var tdBtn2 = document.createElement("td");
  119. var btn2 = document.createElement("button");
  120. btn2.innerHTML = "Dn";
  121. tdBtn2.appendChild(btn2);
  122.  
  123. var tdBtn3 = document.createElement("td");
  124. var btn3 = document.createElement("button");
  125. btn3.innerHTML = "Del";
  126. tdBtn3.appendChild(btn3);
  127.  
  128. node.appendChild(tdBtn1);
  129. node.appendChild(tdBtn2);
  130. node.appendChild(tdBtn3);
  131.  
  132. node.setAttribute("id", "row"+counter);
  133. counter++;
  134. table.appendChild(node);
  135. }
  136. }
  137.  
  138. </script>
  139. </body>
  140.  
  141. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement