Kawiesh

Live Table Editor JS

Sep 5th, 2021 (edited)
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="theme-color" content="black"/>
  6. <meta name="msapplication-TileColor" content="#da532c"/>
  7. <meta name="msapplication-navbutton-color" content="black"/>
  8. <meta name="apple-mobile-web-app-capable" content="yes"/>
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  11.  
  12. <title></title>
  13.  
  14. <style>            
  15. *{
  16. box-sizing: border-box;
  17. margin:0; padding:0;
  18. outline: none;
  19. }  
  20.  
  21. body{
  22. background: rgb(251,244,234) url("");
  23. min-width: 100vw; min-height: 100vh;
  24. position: relative;
  25. display: flex;
  26. flex-direction: column;
  27. align-items: center;
  28. justify-content: flex-start;
  29. }
  30.  
  31. #container{
  32. border: 1px solid red;
  33. margin-top: 20px;
  34. position: relative;
  35. display: flex;
  36. flex-direction: column;
  37. align-items: center;
  38. justify-content: flex-start;
  39. }
  40.  
  41.  
  42. table{
  43. border:1.5px solid black;      
  44. border-collapse: collapse;
  45. max-width: 100%;
  46. table-layout: fixed;
  47. caption-side: top;
  48. }
  49.  
  50. th,td{
  51. padding: 2px;
  52. border:1px solid black;
  53. height: calc(1em + 5px);
  54. text-align: center;
  55. vertical-align: center;
  56. overflow: auto;
  57. width: 10ch;
  58. }
  59.  
  60. thead,tfoot,th{
  61. background:rgb(204, 255, 204);
  62. }
  63.  
  64.  
  65.  
  66. textarea{
  67. height:20em;
  68. width: 100%;                               
  69. }
  70.  
  71.  
  72. .block{
  73. width: 100%;
  74. height: 2em;
  75. display: flex;
  76. justify-content: space-between;                            
  77. }
  78.  
  79. button{
  80. width: 5ch;
  81. height: 2em;       
  82. text-align: center;        
  83. }
  84.  
  85. input{
  86. height: 2em;
  87. width: 5ch;
  88. text-align: center;    
  89. }
  90.  
  91.  
  92.  
  93. @media (orientation:portrait){
  94. #container{
  95. width: 95%; height: auto;
  96. }
  97. body{background-size: 35%}     
  98. }  
  99.  
  100. @media (orientation:landscape){
  101. #container{
  102. width: 75%; height: auto;
  103. }
  104. body{background-size: 50%}         
  105. }  
  106.  
  107. </style>
  108. </head>
  109. <body>
  110. <div id="container">
  111. <div>
  112. <input id="row" type="number" min="1">
  113. <span>x</span>
  114. <input id="col" type="number" min="1">
  115. <button id="go">Go</button>
  116. </div>
  117. <div class="block">
  118. <button id="remrow">-Row</button>
  119. <button id="addrow">+Row</button>
  120. </div>
  121. <div class="block">
  122. <button id="remcol">-Col</button>
  123. <button id="addcol">+Col</button>
  124. </div>
  125.  
  126. <textarea id="output" readonly="true"></textarea>
  127. <table id="input"></table>
  128. </div>
  129. <script>               
  130. let create= (x)=> document.createElement(x),
  131. select= (x,y=document)=> y.querySelector(x),
  132. selectAll= (x,y=document)=> y.querySelectorAll(x);
  133.  
  134. let a= selectAll("input"),
  135. b= selectAll("button"),
  136. c= select("table"),
  137. d= select("textarea");
  138.  
  139.  
  140. let row= select("#row"),
  141. col= select("#col"),
  142. go= select("#go"),
  143. addCol= select("#addcol"),
  144. remCol= select("#remcol"),
  145. addRow= select("#addrow"),
  146. remRow= select("#remrow"),
  147. output= select("#output");
  148. input= select("#input");
  149.  
  150.  
  151.  
  152.  
  153. col.value= "2";
  154. row.value= "3";
  155.  
  156.  
  157. go.onclick=()=>{
  158. input.innerHTML="";
  159. let newRowCells=[];
  160.            
  161. for(let i=0; i<col.value; i++){            
  162. newRowCells.push("<td></td>");         
  163. }  
  164.    
  165.    
  166. for(let i=0; i<row.value; i++){            
  167. let newRow= create("tr");
  168. newRow.innerHTML= newRowCells.join("");
  169. input.append(newRow);      
  170. }  
  171.  
  172.  
  173.  
  174. update();
  175.    
  176. };
  177.  
  178.  
  179. function update(){
  180. let outputvalue= "<table>"  + eskape(input.innerHTML,"unesc") + "</table>";
  181. output.value=
  182. outputvalue
  183. .replace(/<td contenteditable="true">/g,"<td>")
  184. .replace(/></g,">\n<")
  185. .replace(/<td>\n<\/td>/g,"<td></td>");
  186.  
  187.  
  188. selectAll("td").forEach(i=>{i.contentEditable= true;});
  189. }
  190.  
  191.    
  192. input.oninput= update;
  193.  
  194.  
  195. function eskape(a,b="esc") {
  196. if(b=="esc"){
  197. return a
  198. .replace(/&/g, "&amp;")
  199. .replace(/</g, "&lt;")
  200. .replace(/>/g, "&gt;")
  201. .replace(/"/g, "&quot;")
  202. .replace(/'/g, "&#039;");
  203. }
  204.  
  205. if(b=="unesc"){
  206. return a
  207. .replace(/&amp;/g,"&")
  208. .replace(/&lt;/g,"<")
  209. .replace(/&gt;/g,">")
  210. .replace(/&quot;/g,'"')
  211. .replace(/&#039;/g,"'")
  212. .replace(/&nbsp;/g," ")
  213. .replace(/<br>/g,"\n");
  214. }
  215.  
  216. }
  217.  
  218.  
  219. //add rows
  220. addRow.onclick=()=>{
  221. let firstRow= select("tr");
  222. let firstRowCells= selectAll("td",firstRow);
  223. let newRowCells= [];           
  224. for(let i=0; i<firstRowCells.length; i++){             
  225. newRowCells.push("<td></td>");         
  226. }  
  227. let newRow= create ("tr");
  228. newRow.innerHTML= newRowCells.join("");
  229. input.append(newRow);
  230. update();  
  231. };
  232.  
  233. //rem rows
  234. remRow.onclick=()=>{
  235. let allRows= selectAll("tr");
  236. if(allRows.length>1){
  237. allRows[allRows.length-1].remove();
  238. }  
  239. update();
  240. };
  241.  
  242. //rem cols
  243. remCol.onclick=()=>{
  244. let allRows= selectAll("tr");
  245. allRows.forEach(i=>{
  246. let allRowCells= selectAll("td",i);
  247. if(allRowCells.length>1){
  248. allRowCells[allRowCells.length-1].remove();
  249. }          
  250. });
  251. update();
  252. };
  253.  
  254.  
  255. //add cols
  256. addCol.onclick=()=>{
  257. let allRows= selectAll("tr");
  258. allRows.forEach(i=>{
  259. let cell= create("td");
  260. i.append(cell);            
  261. });
  262. update();                          
  263. };
  264.  
  265.  
  266.  
  267. </script>  
  268. </body>
  269. </html>
  270.  
Advertisement
Add Comment
Please, Sign In to add comment