SHARE
TWEET

Untitled

a guest Nov 12th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener("load", getCompanies);
  2.  
  3. let output = document.getElementById("output");
  4.  
  5. function getCompanies() {
  6.   fetch("db.json")
  7.     .then(function(response) {
  8.       return response.json();
  9.     })
  10.     .then(function(data) {
  11.       let companies = data.companies.sort((x, y) => {
  12.         data.users.filter(obj => {
  13.           return obj.uris.company == x.uri;
  14.         }).length >
  15.         data.users.filter(obj => {
  16.           return obj.uris.company == y.uri;
  17.         }).length
  18.           ? 1
  19.           : data.users.filter(obj => {
  20.               return obj.uris.company == x.uri;
  21.             }).length <
  22.             data.users.filter(obj => {
  23.               return obj.uris.company == y.uri;
  24.             }).length
  25.           ? -1
  26.           : 0;
  27.       });
  28.       data.companies = companies;
  29.       for (let i = 0; i < data.companies.length; i++) {
  30.         output.innerHTML += `<div class="center"><div class="companies" id=${i}><div class="companyName">${data.companies[i].name}</div></div><div class = "expand btn">+</div></div></br>`;
  31.       }
  32.       document.querySelectorAll(".btn").forEach(function(e) {
  33.         etc = e.previousSibling;
  34.  
  35.         data.users.filter(function(ur) {
  36.           if (ur.uris.company === data.companies[etc.id].uri) {
  37.             etc.innerHTML += `<div class="display-none">Name: ${ur.name} : </br>Email: ${ur.email}</br></div>`;
  38.           }
  39.         });
  40.  
  41.         e.addEventListener("click", function(el) {
  42.           for (
  43.             let i = 1;
  44.             i < el.target.parentElement.children[0].children.length;
  45.             i++
  46.           ) {
  47.             if (el.target.innerHTML === "+") {
  48.               el.target.parentElement.children[0].children[i].className =
  49.                 "users";
  50.             } else if (el.target.innerHTML === "-") {
  51.               el.target.parentElement.children[0].children[i].className =
  52.                 "display-none";
  53.             }
  54.           }
  55.           if (el.target.innerHTML === "-") {
  56.             el.target.innerHTML = "+";
  57.           } else {
  58.             el.target.innerHTML = "-";
  59.           }
  60.         });
  61.       });
  62.     });
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top