javascript - Javascript dynamic button to delete item by id and delete himself (infinite loop)
问题描述
I am trying to push a ingredient to a list of array. This ingredient is an object and has its id
from uuid
or any library and an ingredient value of whatever the user types on the input.
I wrote the simplest example of it in a single function so its clear to understand the core of my doubt.
How can I delete the item by id and re-render, if the button is part of the renderization.
I could create a render function and invoke it inside the remove button. The problem is that the button would be part of that function.
Code
let ingredients = []
document.querySelector('#ingredients-input').addEventListener('change', (e) => {
e.preventDefault()
const id = uuid()
ingredients.push({
id: id,
title: e.target.value
})
const ingredientUl = document.createElement('p')
const removeButton = document.createElement('button')
ingredientUl.textContent = e.target.value
removeButton.textContent = 'remove'
document.querySelector('#ingredients').append(ingredientUl, removeButton)
removeButton.addEventListener('click', (e) => {
const ingredientIndex = ingredients.findIndex(el => el.id === id)
ingredients.splice(ingredientIndex, 1)
})
})
<div id="ingredients"></div>
<input id="ingredients-input">
CodePen: https://codepen.io/notnishi/pen/QWyPdLL
Any help would be appreciated.
解决方案
Instead of re rendering, you can delete the p tag and the remove button from#ingredients
. Deleting only the specific elements from DOM is much more efficient than rerendering the whole ingredients list for just one change.
The remove button can be removed by using ChildNode.remove() and for deleting the ingredientUl, we can iterate over the child list of #ingredients
and find the item to removed first and then delete it using Node.removeChild()
let ingredients = [];
const ingredientsNodeList = document.querySelector("#ingredients");
document.querySelector("#ingredients-input").addEventListener("change", (e) => {
e.preventDefault();
const id = uuid();
ingredients.push({
id: id,
title: e.target.value,
});
const ingredientUl = document.createElement("p");
const removeButton = document.createElement("button");
ingredientUl.textContent = e.target.value;
ingredientUl.id = id;
e.target.value = ""; // Clearing the input after entering it to ingredients list
removeButton.textContent = "remove";
ingredientsNodeList.append(ingredientUl, removeButton);
removeButton.addEventListener("click", (e) => {
const ingredientIndex = ingredients.findIndex((el) => el.id === id);
ingredients.splice(ingredientIndex, 1);
const itemToDelete = [...ingredientsNodeList.children].find(el => el.id === id);
ingredientsNodeList.removeChild(itemToDelete); // Removing ingredientUl
e.target.remove(); // Deleting removeButton
});
});
推荐阅读
- sql - 从 databricks 表的字符串列中收集用户信息(电子邮件、IP 地址等)
- regex - 如何在 WhitelistingTextInputFormatter 的正则表达式中允许单引号?
- python - 当我将标签位置添加到 python pptx 甜甜圈图时,文件出错
- android - 在 Android 中通过 Instagram 登录
- node.js - 同时 JS 应用程序管道安装和构建挂起(Express js 用于服务器,Create-React-App 用于客户端)
- python - no-build-isolation 有什么作用?
- c++ - 无法在 MacOs 下用 C++ 打开文件
- javascript - 如何在 React 中过滤多个值
- html - 使用选定的 Wordpress 更改单选按钮的颜色
- sql - 如何在一个查询中插入和更新