Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <pre><code>
- <body>
- <div>
- <label for="finder">Find User:</label>
- <input type="search" id="searchInput" name="sInput"
- onchange="sBar()"
- placeholder="Search user">
- <button id="sButton">Search</button>
- </div>
- <table class="table table-responsive">
- <thead class="thead-dark">
- <tr>
- <th scope="col">Id</th>
- <th scope="col">Name</th>
- <th scope="col">Username</th>
- <th scope="col">Email</th>
- <th scope="col">Address</th>
- <th scope="col">Phone</th>
- <th scope="col">Website</th>
- <th scope="col">Company</th>
- </tr>
- </thead>
- <tbody name = "tTable">
- </tbody>
- </table>
- </body>
- <script
- src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
- </script>
- <script src="script.js">
- </script>
- </code></pre>
- window.onload = function(){
- let uList = document.querySelector('[name =tTable]');
- fetchCall('https://jsonplaceholder.typicode.com/users', getUsers);
- sButton.addEventListener('click',
- fetchCall('https://jsonplaceholder.typicode.com/users', sBar), false);
- function sBar(getObject) {
- let sUser = getObject;
- let inputBar = document.getElementById("searchInput");
- let text = inputBar.textContent;
- let textView = text.toUpperCase();
- for (let i = 0; i < getObject.length; i++) {
- let uObject = sUser[i];
- if (textView == uObject.name || textView ==
- uObject.email) {
- let new_tTable = document.createElement('tbody');
- uList.parentNode.replaceChild(new_tTable, uList)
- let row = uList.insertRow();
- let idInput = document.createElement('td');
- let nameInput = document.createElement('td');
- let usernameInput = document.createElement('td');
- let emailInput = document.createElement('td');
- let cityInput = document.createElement('td');
- let phoneInput = document.createElement('td');
- let websiteInput = document.createElement('td');
- let companyInput = document.createElement('td');
- idInput.textContent = uObject.id;
- nameInput.textContent = uObject.name;
- usernameInput.textContent = uObject.username;
- emailInput.textContent = uObject.email;
- cityInput.textContent = uObject.address.city;
- phoneInput.textContent = uObject.phone;
- websiteInput.textContent = uObject.website;
- companyInput.textContent = uObject.company.name;
- row.appendChild(idInput);
- row.appendChild(nameInput);
- row.appendChild(usernameInput);
- row.appendChild(emailInput);
- row.appendChild(cityInput);
- row.appendChild(phoneInput);
- row.appendChild(websiteInput);
- row.appendChild(companyInput);
- } else{
- alert("User not found");
- }
- }
- }
- function fetchCall(url, fn){
- fetch(url)
- .then(function(response){
- return response.json();
- })
- .then(function(endPoint){
- fn(endPoint);
- })
- .catch(function(error){
- console.error(error);
- })
- }
- function getUsers(getObject) {
- let user = getObject;
- for (let i = 0; i < getObject.length; i++) {
- let userObject = user[i];
- let row = uList.insertRow();
- let idInput = document.createElement('td');
- let nameInput = document.createElement('td');
- let usernameInput = document.createElement('td');
- let emailInput = document.createElement('td');
- let cityInput = document.createElement('td');
- let phoneInput = document.createElement('td');
- let websiteInput = document.createElement('td');
- let companyInput = document.createElement('td');
- idInput.textContent = userObject.id;
- nameInput.textContent = userObject.name;
- usernameInput.textContent = userObject.username;
- emailInput.textContent = userObject.email;
- cityInput.textContent = userObject.address.city;
- phoneInput.textContent = userObject.phone;
- websiteInput.textContent = userObject.website;
- companyInput.textContent = userObject.company.name;
- row.appendChild(idInput);
- row.appendChild(nameInput);
- row.appendChild(usernameInput);
- row.appendChild(emailInput);
- row.appendChild(cityInput);
- row.appendChild(phoneInput);
- row.appendChild(websiteInput);
- row.appendChild(companyInput);
- }
- }
- }
Add Comment
Please, Sign In to add comment