Advertisement
guillermo11bq

Untitled

May 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Objetivos:
  3.     - Listar Usuarios
  4.     - Buscar usuarios por Nombre.
  5.     - Mostar una alerta cuando se da click sobre un usuario con el nombre del usuario.
  6. */
  7.  
  8. var userListEl = document.querySelector('ul#userList');
  9. var userSearchEl = document.querySelector('input#userSearch');
  10. var users = [];
  11.  
  12. userSearchEl.oninput = () => {
  13.     let search_string = userSearchEl.value;
  14.   let result_users = [];
  15.  
  16.   users.forEach(function(user){
  17.   console.log(user.name.indexOf(search_string));
  18.     if(user.name.indexOf(search_string) != -1){
  19.         result_users.push(user);
  20.     }
  21.   });
  22.  
  23.   renderUserList(result_users);
  24. }
  25.  
  26. function fetchUsers() {
  27.     return fetch('https://jsonplaceholder.typicode.com/users')
  28.         .then(response => response.json())
  29.         .then(array => {
  30.             users = array;
  31.             renderUserList(array);
  32.         })
  33.         /*
  34.             [{
  35.                 "id": 1,
  36.                 "name": "Emmanuel Garcia",
  37.                 "email": "eagarcia@tiempodevelopment.com"
  38.             }]
  39.         */
  40.         .then(json => console.log(json));
  41. }
  42.  
  43.  
  44. function renderUserList (list) {
  45.     userListEl.innerHTML = '';
  46.     list.forEach(function(user){
  47.         let li = document.createElement('li');
  48.         li.setAttribute('id', user.id);
  49.        
  50.         let text = user.name + ' ' + '(' + user.email + ')';
  51.       li.appendChild(document.createTextNode(text));
  52.       li.onclick = () => console.log(user.name);
  53.     userListEl.appendChild(li);
  54.     });
  55.    
  56. }
  57.  
  58. fetchUsers();
  59. renderUserList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement