Advertisement
didkoslawow

Untitled

Jul 5th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.      window.addEventListener('load', loadTableData);
  3.      const tbody = document.querySelector('tbody');
  4.      const input = document.getElementById('searchField');
  5.  
  6.      document.querySelector('#searchBtn').addEventListener('click', onClick);
  7.  
  8.      function onClick() {
  9.          const rows = tbody.querySelectorAll('tr');
  10.          rows.forEach((r) => r.classList.remove('select'));
  11.          const search = input.value.toLowerCase();
  12.  
  13.          if (!rows || search == '') {
  14.              return;
  15.          }
  16.  
  17.          rows.forEach((r) => {
  18.              if (r.textContent.toLocaleLowerCase().includes(search)) {
  19.                  r.classList.add('select');
  20.              }
  21.          });
  22.  
  23.          input.value = '';
  24.      }
  25.  
  26.      async function loadTableData() {
  27.          const data = await getTableData();
  28.          const tableData = Object.values(data).map((t) => {
  29.              const row = createElement('tr', null, null, [
  30.                  createElement('td', `${t.firstName} ${t.lastName}`),
  31.                  createElement('td', t.email),
  32.                  createElement('td', t.course),
  33.              ]);
  34.              return row;
  35.          });
  36.          tbody.append(...tableData);
  37.      }
  38.      async function getTableData() {
  39.          try {
  40.              const response = await fetch('http://localhost:3030/jsonstore/advanced/table');
  41.  
  42.              if (response.ok != true) {
  43.                  const err = await response.json();
  44.                  throw new Error(err.message);
  45.              }
  46.  
  47.              const data = await response.json();
  48.  
  49.              console.log(data);
  50.              return data;
  51.          } catch (error) {
  52.              alert(error.message);
  53.          }
  54.      }
  55.  
  56.      function createElement(tagName, textContent, attributes, children = []) {
  57.          const element = document.createElement(tagName);
  58.          const PARAMS = {
  59.              colspan: (value) => element.setAttribute('colspan', value),
  60.              class: (value) => element.classList.add(value),
  61.              id: (value) => (element.id = value),
  62.              onclick: (value) => element.addEventListener('click', value),
  63.              disabled: () => element.setAttribute('disabled', ''),
  64.              src: (value) => element.setAttribute('src', value),
  65.          };
  66.  
  67.          if (textContent) {
  68.              element.textContent = textContent;
  69.          }
  70.  
  71.          if (attributes) {
  72.              Object.entries(attributes).forEach(([param, value]) => PARAMS[param](value));
  73.          }
  74.  
  75.          if (children.length == 0) {
  76.              return element;
  77.          }
  78.  
  79.          children.forEach((c) => element.appendChild(c));
  80.  
  81.          return element;
  82.      }
  83.  }
  84.  solve();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement