I have an array, and I want to print them in div tags, and also when I make changes on the array, I want the change also to occur on divs (for example when I delete an item, div print of item also be deleted; or change the value of an item, expect thing happen to the div of the item). I made a little research and I found something I didn't know before that called Proxy object. I wrote the following code:
let fruits = [
"Apple", "Pear", "Oak", "Orange", "Tangerine", "Melon",
"Ananas", "Strawberry", "Cherry", "Sour Cherry","Banana",
"Plum", "Pomegranate", "Apricot", "Peach", "Nectarine",
"Kiwi", "Mulberry", "Fig", "Grapefruit", "Grape",
];
let trees = document.getElementById("trees");
let tree = new Array();
let aproxy = new Proxy(fruits, {
get: (target, key, receiver) => {
let treeDiv = document.createElement("div");
treeDiv.classList.add("tree");
let span = document.createElement("span");
span.textContent = target[key] + " Tree";
treeDiv.appendChild(span);
trees.appendChild(treeDiv);
treeDiv.addEventListener("dblclick", () => {
fruits.splice(key, 1);
//Here I delete all the item print divs before reprint all of them
trees.querySelectorAll("div.tree").forEach(el => {
trees.removeChild(el);
});
//Here I make the iteration to reprint
fruits.forEach((el, index) => {
aproxy[index];
});
});
return treeDiv;
},
set: (target, key, value) => {
fruits.push(value);
aproxy[key];
return true;
},
});
fruits.forEach((el, index) => {
aproxy[index];
});
let input = document.querySelector("#inputTag");
input.addEventListener("keyup", ev => {
if (ev.code === "Enter") {
aproxy[fruits.length] = input.value;
console.log(fruits);
}
});
@import url('https://fonts.googleapis.com/css2?family=Nunito+Sans:ital,wght@0,200;0,300;0,400;0,600;0,700;1,200;1,300;1,400;1,600&display=swap');
body {
background: linear-gradient(135deg,rgb(204, 64, 64),rgb(204, 64, 118));
margin: 0 0 100px 0;
background-attachment: fixed;
zoom: .7;
}
.as-console-wrapper { height: 100px!important; }
#container {
width: 100%;
margin: auto;
padding: 10px;
}
#input {
width: 93%;
}
#input input {
border:none;
padding: 17px 20px;
margin: 0;
border-radius: 15px;
display: block;
width: 100%;
font-size: 14pt;
font-family: 'Nunito Sans', sans-serif;
font-weight: 300;
box-shadow: 0 10px 10px rgba(0,0,0,0.1);
}
#input input::placeholder {
font-family: 'Nunito Sans', sans-serif;
font-size: 14pt;
font-style: italic;
}
#input input:focus {
outline: none;
}
#trees {
display: flex;
margin-top: 15px;
flex-wrap: wrap;
width: 100%;
justify-content: center;
}
#trees .tree {
padding: 10px;
background: rgba(255,255,255,1);
margin: 7px;
border-radius: 10px;
cursor: pointer;
transition: background .3s;
user-select: none;
}
#trees .tree:hover {
background: rgba(255,255,255,.1);
}
#trees .tree:hover span {
color: #fff;
}
#trees .tree span {
font-family: 'Nunito Sans', sans-serif;
font-weight: 600;
color: rgb(204, 64, 64);
transition: color .3s;
}
<div id="container">
<div id="input">
<input placeholder="Enter a tree and press 'Enter'" type="text" name="" id="inputTag">
</div>
<div id="trees">
</div>
</div>
As you can see, I reprint the divs when an item is deleted. Is there another way to do that by Proxy object without reprinting? Can I bond the array items to divs?