Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7.  
  8. <style id="jsbin-css">
  9. table, th, td
  10. {
  11. margin:10px 0;
  12. border:solid 1px #333;
  13. padding:2px 4px;
  14. font:15px Verdana;
  15. }
  16. th {
  17. font-weight:bold;
  18. }
  19. div, input {
  20. display: inline-block;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <input type="text" id="myInput" onkeyup="searchByNames()" placeholder="Search for names..">
  26. <input type="button" onclick="createTableFromJSON()" value="Create Table From JSON">
  27. <input type="button" onclick="sortTable()" value="Sort By Name">
  28. <div id="table-area">
  29. </div>
  30. <script id="jsbin-javascript">
  31. function createTableFromJSON() {
  32. var userList = [
  33. {id: 1,
  34. firstName: "Jonathan",
  35. lastName: "Mendoza",
  36. email: "vydryhans@gmail.com",
  37. gender: "male"},
  38. {id: 2,
  39. firstName: "Vladislav",
  40. lastName: "Vydryhan",
  41. email: "vydryhans2@gmail.com",
  42. gender: "male"},
  43. {id: 3,
  44. firstName: "Marina",
  45. lastName: "Kozyk",
  46. email: "vydryhan3@gmail.com",
  47. gender: "female"},
  48. {id: 4,
  49. firstName: "Natasha",
  50. lastName: "Ivanova",
  51. email: "vydryhan4@gmail.com",
  52. gender: "male"},
  53. {id: 5,
  54. firstName: "Jessie",
  55. lastName: "Lingard",
  56. email: "vydryhans5@gmail.com",
  57. gender: "male"},
  58. {id: 6,
  59. firstName: "Kylian",
  60. lastName: "Mbappe",
  61. email: "vydryhans7@gmail.com",
  62. gender: "male"},
  63. {id: 7,
  64. firstName: "Morgana",
  65. lastName: "Devil",
  66. email: "vydryhans7@gmail.com",
  67. gender: "female"},
  68. {id: 8,
  69. firstName: "Cruela",
  70. lastName: "Mascherano",
  71. email: "vydryhans8@gmail.com",
  72. gender: "female"},
  73. {id: 9,
  74. firstName: "Lolik",
  75. lastName: "Bolik",
  76. email: "vydryhans9@gmail.com",
  77. gender: "male"},
  78. {id: 10,
  79. firstName: "Andrew",
  80. lastName: "Pardew",
  81. email: "vydryhans10@gmail.com",
  82. gender: "male"},
  83. {id: 11,
  84. firstName: "Marte",
  85. lastName: "Olsbu",
  86. email: "vydryhan11s@gmail.com",
  87. gender: "female"},
  88. {id: 12,
  89. firstName: "Magdalena",
  90. lastName: "Neuner",
  91. email: "vydryhans13@gmail.com",
  92. gender: "female"},
  93. {id: 13,
  94. firstName: "Gabriela",
  95. lastName: "Koukalova",
  96. email: "vydryhans13@gmail.com",
  97. gender: "female"},
  98. {id: 14,
  99. firstName: "Arsen",
  100. lastName: "Wenger",
  101. email: "vydryhans14@gmail.com",
  102. gender: "male"},
  103. {id: 15,
  104. firstName: "Thiery",
  105. lastName: "Henry",
  106. email: "vydryhans15@gmail.com",
  107. gender: "male"},
  108. ]
  109.  
  110. var tableHeaders = ["fullName", "email", "gender"]
  111.  
  112. var fullName, tableCell, th;
  113. var table = document.createElement("table");
  114. table.id = 'myTable';
  115.  
  116. var tr = table.insertRow(-1);
  117. tableHeaders.map( function(header) {
  118. th = document.createElement("th"); // TABLE HEADER.
  119. th.innerHTML = header;
  120. tr.appendChild(th);
  121. });
  122.  
  123. userList.map( function(user) {
  124. var tr = table.insertRow(-1);
  125.  
  126. tableCell = tr.insertCell(-1);
  127. fullName = user.firstName + ' ' + user.lastName;
  128. tableCell.innerHTML = fullName;
  129.  
  130. tableCell = tr.insertCell(-1);
  131. tableCell.innerHTML = user.email;
  132.  
  133. tableCell = tr.insertCell(-1);
  134. tableCell.innerHTML = user.gender;
  135. });
  136.  
  137. var divContainer = document.getElementById("table-area");
  138. divContainer.innerHTML = "";
  139. divContainer.appendChild(table);
  140.  
  141. }
  142.  
  143. function searchByNames() {
  144. var input, filter, table, tr, td, i;
  145. input = document.getElementById("myInput");
  146. filter = input.value.toUpperCase();
  147. table = document.getElementById("myTable");
  148. tr = table.getElementsByTagName("tr");
  149.  
  150. for (i = 0; i < tr.length; i++) {
  151. td = tr[i].getElementsByTagName("td")[0];
  152. if (td) {
  153. if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
  154. tr[i].style.display = "";
  155. } else {
  156. tr[i].style.display = "none";
  157. }
  158. }
  159. }
  160. }
  161.  
  162. function sortTable() {
  163. var table, rows, switching, i, x, y, shouldSwitch;
  164. table = document.getElementById("myTable");
  165. switching = true;
  166.  
  167. while (switching) {
  168.  
  169. switching = false;
  170. rows = table.getElementsByTagName("tr");
  171.  
  172. for (i = 1; i < (rows.length - 1); i++) {
  173. shouldSwitch = false;
  174.  
  175. x = rows[i].getElementsByTagName("td")[0];
  176. y = rows[i + 1].getElementsByTagName("td")[0];
  177.  
  178. if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
  179.  
  180. shouldSwitch= true;
  181. break;
  182. }
  183. }
  184. if (shouldSwitch) {
  185.  
  186. rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  187. switching = true;
  188. }
  189. }
  190. }
  191. </script>
  192.  
  193.  
  194. <script id="jsbin-source-css" type="text/css">table, th, td
  195. {
  196. margin:10px 0;
  197. border:solid 1px #333;
  198. padding:2px 4px;
  199. font:15px Verdana;
  200. }
  201. th {
  202. font-weight:bold;
  203. }
  204. div, input {
  205. display: inline-block;
  206. }</script>
  207.  
  208. <script id="jsbin-source-javascript" type="text/javascript">
  209. function createTableFromJSON() {
  210. var userList = [
  211. {id: 1,
  212. firstName: "Jonathan",
  213. lastName: "Mendoza",
  214. email: "vydryhans@gmail.com",
  215. gender: "male"},
  216. {id: 2,
  217. firstName: "Vladislav",
  218. lastName: "Vydryhan",
  219. email: "vydryhans2@gmail.com",
  220. gender: "male"},
  221. {id: 3,
  222. firstName: "Marina",
  223. lastName: "Kozyk",
  224. email: "vydryhan3@gmail.com",
  225. gender: "female"},
  226. {id: 4,
  227. firstName: "Natasha",
  228. lastName: "Ivanova",
  229. email: "vydryhan4@gmail.com",
  230. gender: "male"},
  231. {id: 5,
  232. firstName: "Jessie",
  233. lastName: "Lingard",
  234. email: "vydryhans5@gmail.com",
  235. gender: "male"},
  236. {id: 6,
  237. firstName: "Kylian",
  238. lastName: "Mbappe",
  239. email: "vydryhans7@gmail.com",
  240. gender: "male"},
  241. {id: 7,
  242. firstName: "Morgana",
  243. lastName: "Devil",
  244. email: "vydryhans7@gmail.com",
  245. gender: "female"},
  246. {id: 8,
  247. firstName: "Cruela",
  248. lastName: "Mascherano",
  249. email: "vydryhans8@gmail.com",
  250. gender: "female"},
  251. {id: 9,
  252. firstName: "Lolik",
  253. lastName: "Bolik",
  254. email: "vydryhans9@gmail.com",
  255. gender: "male"},
  256. {id: 10,
  257. firstName: "Andrew",
  258. lastName: "Pardew",
  259. email: "vydryhans10@gmail.com",
  260. gender: "male"},
  261. {id: 11,
  262. firstName: "Marte",
  263. lastName: "Olsbu",
  264. email: "vydryhan11s@gmail.com",
  265. gender: "female"},
  266. {id: 12,
  267. firstName: "Magdalena",
  268. lastName: "Neuner",
  269. email: "vydryhans13@gmail.com",
  270. gender: "female"},
  271. {id: 13,
  272. firstName: "Gabriela",
  273. lastName: "Koukalova",
  274. email: "vydryhans13@gmail.com",
  275. gender: "female"},
  276. {id: 14,
  277. firstName: "Arsen",
  278. lastName: "Wenger",
  279. email: "vydryhans14@gmail.com",
  280. gender: "male"},
  281. {id: 15,
  282. firstName: "Thiery",
  283. lastName: "Henry",
  284. email: "vydryhans15@gmail.com",
  285. gender: "male"},
  286. ]
  287.  
  288. var tableHeaders = ["fullName", "email", "gender"]
  289.  
  290. var fullName, tableCell, th;
  291. var table = document.createElement("table");
  292. table.id = 'myTable';
  293.  
  294. var tr = table.insertRow(-1);
  295. tableHeaders.map( function(header) {
  296. th = document.createElement("th"); // TABLE HEADER.
  297. th.innerHTML = header;
  298. tr.appendChild(th);
  299. });
  300.  
  301. userList.map( function(user) {
  302. var tr = table.insertRow(-1);
  303.  
  304. tableCell = tr.insertCell(-1);
  305. fullName = user.firstName + ' ' + user.lastName;
  306. tableCell.innerHTML = fullName;
  307.  
  308. tableCell = tr.insertCell(-1);
  309. tableCell.innerHTML = user.email;
  310.  
  311. tableCell = tr.insertCell(-1);
  312. tableCell.innerHTML = user.gender;
  313. });
  314.  
  315. var divContainer = document.getElementById("table-area");
  316. divContainer.innerHTML = "";
  317. divContainer.appendChild(table);
  318.  
  319. }
  320.  
  321. function searchByNames() {
  322. var input, filter, table, tr, td, i;
  323. input = document.getElementById("myInput");
  324. filter = input.value.toUpperCase();
  325. table = document.getElementById("myTable");
  326. tr = table.getElementsByTagName("tr");
  327.  
  328. for (i = 0; i < tr.length; i++) {
  329. td = tr[i].getElementsByTagName("td")[0];
  330. if (td) {
  331. if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
  332. tr[i].style.display = "";
  333. } else {
  334. tr[i].style.display = "none";
  335. }
  336. }
  337. }
  338. }
  339.  
  340. function sortTable() {
  341. var table, rows, switching, i, x, y, shouldSwitch;
  342. table = document.getElementById("myTable");
  343. switching = true;
  344.  
  345. while (switching) {
  346.  
  347. switching = false;
  348. rows = table.getElementsByTagName("tr");
  349.  
  350. for (i = 1; i < (rows.length - 1); i++) {
  351. shouldSwitch = false;
  352.  
  353. x = rows[i].getElementsByTagName("td")[0];
  354. y = rows[i + 1].getElementsByTagName("td")[0];
  355.  
  356. if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
  357.  
  358. shouldSwitch= true;
  359. break;
  360. }
  361. }
  362. if (shouldSwitch) {
  363.  
  364. rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  365. switching = true;
  366. }
  367. }
  368. }</script></body>
  369. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement