Adding and removing users version 2.0

Added: sort and search

Java Script code:

let arrObj = [];

function addElemIntoArr() {
  let inputValueName = document.getElementById("firstInput").value;
  arrObj.push({
    name: inputValueName,
    id: Date.now()
  });

  renderArr(arrObj)
  document.getElementById("firstInput").value = "";
}

function renderArr(arr) {
  document.getElementById("formsDiv").innerHTML = "";
  arr.forEach(el => {
    showElementArr(el)
    console.log(el)
  })
}

function deleteElem(id) {
  arrObj = arrObj.filter((elem) => {
    if (elem.id == id) {
      return false;
    }
    return true;
  })
  renderArr()
}

function showElementArr(el) {
  let formsDiv = document.getElementById("formsDiv");
  let mainDiv = document.createElement("div");
  let delitBtn = document.createElement("div");
  delitBtn.onclick = () => {
    deleteElem(el.id);
  }

  mainDiv.innerHTML = el.name;
  delitBtn.innerHTML = "Delete"

  mainDiv.classList = ("mainDiv")
  delitBtn.classList = ("btnStyle")

  mainDiv.appendChild(delitBtn);
  formsDiv.appendChild(mainDiv);
}


function search() {
  let inputSearchValue = document.getElementById("firstInput").value;
  let result = arrObj.filter((inp) => inp.name.includes(inputSearchValue));
  renderArr(result)
}

function sort() {
  let result = arrObj.sort((a, b) => sortArr(a, b))
  renderArr(result)
}

function sortArr(a, b) {
  if (a.name > b.name) {
    return 1;
  }
  if (a.name < b.name) {
    return -1;
  }
  return 0;
}

HTML code:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body class="body">
  <div class="nav">
    <input type="text" class="input" placeholder="Name" id="firstInput">
    <div class="line"></div>
    <div class="div" onclick="addElemIntoArr()">Add User</div>
    <div class="line"></div>
    <div class="div" onclick="search()">Search</div>
    <div class="line"></div>
    <div class="div" onclick="sort()">Sort</div>
  </div>

  <div class="formsDiv" id="formsDiv">

  </div>

  <script src="./js.js"></script>

</body>

</html>

CSS code:

.body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: row;
  width: 100%;
  height: 100%;
  background-color: rgb(235, 166, 29);
}

.nav {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 200px;
  height: 100vh;
  gap: 5px;
  background-color: lemonchiffon;
}

.formsDiv {
  padding: 5px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 15px;
  overflow: auto;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}

.line {
  background-color: lightgrey;
  width: 190px;
  height: 1px;
}

.div {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
  width: 190px;
  height: 50px;
  border-radius: 3px;
  background-color: rgb(235, 166, 29);
  box-shadow: 0px 3px 3px 0px lightgray;
  transition: 1s all;
}

input {
  font-size: 30px;
  margin-top: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 180px;
  height: 50px;
  border-radius: 3px;
  background-color: rgb(235, 166, 29);
  box-shadow: 0px 3px 3px 0px lightgray;
}

.mainDiv {
  border-radius: 3px;
  background-color: lemonchiffon;
  width: calc(100% / 5 - 12px);
  height: 50px;
  box-shadow: 0px 3px 3px 0px lightgray;
  display: flex;
  align-items: center;
  font-size: 30px;
  justify-content: space-around;
}

.btnStyle {
  width: 50%;
  height: 75%;
  font-size: 25px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 3px;
  background-color: rgb(235, 166, 29);
  box-shadow: 0px 3px 3px 0px lightgray;
  transition: 1s all;
}

.btnStyle:hover {
  background-color: red;
  cursor: pointer;
}

.div:hover {
  background-color: rgb(235, 215, 29);
  cursor: pointer;
}

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :