Guest User

Untitled

a guest
Jan 21st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. <pre><code>
  2. <body>
  3. <div>
  4. <label for="finder">Find User:</label>
  5. <input type="search" id="searchInput" name="sInput"
  6. onchange="sBar()"
  7. placeholder="Search user">
  8. <button id="sButton">Search</button>
  9. </div>
  10. <table class="table table-responsive">
  11. <thead class="thead-dark">
  12. <tr>
  13. <th scope="col">Id</th>
  14. <th scope="col">Name</th>
  15. <th scope="col">Username</th>
  16. <th scope="col">Email</th>
  17. <th scope="col">Address</th>
  18. <th scope="col">Phone</th>
  19. <th scope="col">Website</th>
  20. <th scope="col">Company</th>
  21. </tr>
  22. </thead>
  23. <tbody name = "tTable">
  24. </tbody>
  25. </table>
  26. </body>
  27. <script
  28. src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
  29. </script>
  30. <script src="script.js">
  31. </script>
  32.  
  33. </code></pre>
  34.  
  35. window.onload = function(){
  36.  
  37. let uList = document.querySelector('[name =tTable]');
  38.  
  39. fetchCall('https://jsonplaceholder.typicode.com/users', getUsers);
  40. sButton.addEventListener('click',
  41. fetchCall('https://jsonplaceholder.typicode.com/users', sBar), false);
  42.  
  43.  
  44. function sBar(getObject) {
  45. let sUser = getObject;
  46. let inputBar = document.getElementById("searchInput");
  47. let text = inputBar.textContent;
  48. let textView = text.toUpperCase();
  49. for (let i = 0; i < getObject.length; i++) {
  50. let uObject = sUser[i];
  51. if (textView == uObject.name || textView ==
  52. uObject.email) {
  53. let new_tTable = document.createElement('tbody');
  54. uList.parentNode.replaceChild(new_tTable, uList)
  55.  
  56.  
  57. let row = uList.insertRow();
  58. let idInput = document.createElement('td');
  59. let nameInput = document.createElement('td');
  60. let usernameInput = document.createElement('td');
  61. let emailInput = document.createElement('td');
  62. let cityInput = document.createElement('td');
  63. let phoneInput = document.createElement('td');
  64. let websiteInput = document.createElement('td');
  65. let companyInput = document.createElement('td');
  66.  
  67. idInput.textContent = uObject.id;
  68. nameInput.textContent = uObject.name;
  69. usernameInput.textContent = uObject.username;
  70. emailInput.textContent = uObject.email;
  71. cityInput.textContent = uObject.address.city;
  72. phoneInput.textContent = uObject.phone;
  73. websiteInput.textContent = uObject.website;
  74. companyInput.textContent = uObject.company.name;
  75. row.appendChild(idInput);
  76. row.appendChild(nameInput);
  77. row.appendChild(usernameInput);
  78. row.appendChild(emailInput);
  79. row.appendChild(cityInput);
  80. row.appendChild(phoneInput);
  81. row.appendChild(websiteInput);
  82. row.appendChild(companyInput);
  83. } else{
  84. alert("User not found");
  85. }
  86. }
  87. }
  88.  
  89.  
  90. function fetchCall(url, fn){
  91. fetch(url)
  92. .then(function(response){
  93. return response.json();
  94. })
  95. .then(function(endPoint){
  96. fn(endPoint);
  97. })
  98. .catch(function(error){
  99. console.error(error);
  100. })
  101. }
  102.  
  103. function getUsers(getObject) {
  104. let user = getObject;
  105. for (let i = 0; i < getObject.length; i++) {
  106. let userObject = user[i];
  107. let row = uList.insertRow();
  108. let idInput = document.createElement('td');
  109. let nameInput = document.createElement('td');
  110. let usernameInput = document.createElement('td');
  111. let emailInput = document.createElement('td');
  112. let cityInput = document.createElement('td');
  113. let phoneInput = document.createElement('td');
  114. let websiteInput = document.createElement('td');
  115. let companyInput = document.createElement('td');
  116.  
  117. idInput.textContent = userObject.id;
  118. nameInput.textContent = userObject.name;
  119. usernameInput.textContent = userObject.username;
  120. emailInput.textContent = userObject.email;
  121. cityInput.textContent = userObject.address.city;
  122. phoneInput.textContent = userObject.phone;
  123. websiteInput.textContent = userObject.website;
  124. companyInput.textContent = userObject.company.name;
  125. row.appendChild(idInput);
  126. row.appendChild(nameInput);
  127. row.appendChild(usernameInput);
  128. row.appendChild(emailInput);
  129. row.appendChild(cityInput);
  130. row.appendChild(phoneInput);
  131. row.appendChild(websiteInput);
  132. row.appendChild(companyInput);
  133. }
  134. }
  135. }
Add Comment
Please, Sign In to add comment