Javascript 在更改阵列项的属性后呈现阵列项

Javascript 在更改阵列项的属性后呈现阵列项,javascript,arrays,rendering,Javascript,Arrays,Rendering,在对数组中的元素进行更改后,重新呈现数组中的项时出现问题。无论我是通过按压添加还是通过拼接删除,当数组再次呈现在页面上时,似乎有更多的项被添加到数组中。因此,如果我推到数组上,将添加项,但旧项将复制到数组中。当我删除项目时,也会发生类似的情况。该项看起来要被删除,但数组中的元素显示在页面上,然后它们被复制,拼接的项也消失了 我试图避免位置。请重新加载“/edit.html”以刷新页面。有点作弊。它似乎可以工作,但我正在尝试使用RenderingCredits函数刷新页面。当我检查一个项目时,To

在对数组中的元素进行更改后,重新呈现数组中的项时出现问题。无论我是通过按压添加还是通过拼接删除,当数组再次呈现在页面上时,似乎有更多的项被添加到数组中。因此,如果我推到数组上,将添加项,但旧项将复制到数组中。当我删除项目时,也会发生类似的情况。该项看起来要被删除,但数组中的元素显示在页面上,然后它们被复制,拼接的项也消失了

我试图避免位置。请重新加载“/edit.html”以刷新页面。有点作弊。它似乎可以工作,但我正在尝试使用RenderingCredits函数刷新页面。当我检查一个项目时,ToggleComponent函数也会复制项目列表

import { initializeEditPage, generateLastEdited } from './views'
import { updateRecipe, removeRecipe, saveRecipes, getRecipes, createIngredient } from './recipes'

const titleElement = document.querySelector('#recipe-title')
const bodyElement = document.querySelector('#recipe-body')
const removeElement = document.querySelector('#remove-recipe')
const addElement = document.querySelector('#add-recipe')
const dateElement = document.querySelector('#last-updated')
const addIngredient = document.querySelector('#new-ingredient')
const recipeStatus = document.querySelector('#recipe-status')

const recipeId = location.hash.substring(1)
const recipeOnPage = getRecipes().find((item) => item.id === recipeId)

titleElement.addEventListener('input', (e) => {
    const recipe = updateRecipe(recipeId, {
        title: e.target.value
    })
    dateElement.textContent = generateLastEdited(recipe.updatedAt)
})

bodyElement.addEventListener('input', (e) => {
    const recipe = updateRecipe(recipeId, {
        body: e.target.value
    })
    dateElement.textContent = generateLastEdited(recipe.updatedAt)
})

addElement.addEventListener('click', () => {
    saveRecipes()
    location.assign('/index.html')
})

removeElement.addEventListener('click', () => {
    removeRecipe(recipeId)
    location.assign('/index.html')
})

addIngredient.addEventListener('submit', (e) => {
    const text = e.target.elements.text.value.trim()
    e.preventDefault()

    if (text.length > 0) {
        createIngredient(recipeId, text)
        e.target.elements.text.value = ''
    }
    renderIngredients(recipeId)
    saveRecipes()
    //location.reload('/edit.html')
})

const removeIngredient = (text) => {
    const ingredientIndex = recipeOnPage.ingredients.findIndex((ingredient)=> ingredient.text === text)
    if (ingredientIndex > -1) {
        recipeOnPage.ingredients.splice(ingredientIndex, 1)
    }
    saveRecipes()
    renderIngredients(recipeId)
    //location.reload('/edit.html')
}

const toggleIngredient = (text) => {
    const ingredient = recipeOnPage.ingredients.find((ingredient) => ingredient.text === text)
    if (ingredient.included) {
        ingredient.included = false
    } else {
        ingredient.included = true
    }
    //location.reload('/edit.html')
}

const ingredientSummary = (recipe) => {
    let message
    const allUnchecked = recipeOnPage.ingredients.every((ingredient) => ingredient.included === false)
    const allChecked = recipeOnPage.ingredients.every((ingredient) => ingredient.included === true)

    if (allUnchecked) {
        message = `none`
    } else if (allChecked) {
        message = `all`
    } else {
        message = `some`
    }
    return `You have ${message} ingredients for this recipe`
}

const generateIngredientDOM = (ingredient) => {
    const ingredientEl = document.createElement('label')
    const containerEl = document.createElement('div')
    const checkbox = document.createElement('input')
    const ingredientText = document.createElement('span')
    const removeButton = document.createElement('button')
    recipeStatus.textContent = ingredientSummary(recipeOnPage)

    // Setup ingredient container
    ingredientEl.classList.add('list-item')
    containerEl.classList.add('list-item__container')
    ingredientEl.appendChild(containerEl)

    // Setup ingredient checkbox
    checkbox.setAttribute('type', 'checkbox')
    checkbox.checked = ingredient.included
    containerEl.appendChild(checkbox)
    // Create checkbox button in ingredient div
    checkbox.addEventListener('click', () => {
        toggleIngredient(ingredient.text)
        saveRecipes()
        renderIngredients(recipeId)
    })

    // Setup ingredient text
    ingredientText.textContent = ingredient.text
    containerEl.appendChild(ingredientText)

    // Setup the remove button
    removeButton.textContent = 'remove'
    removeButton.classList.add('button', 'button--text')
    ingredientEl.appendChild(removeButton)
    // Create remove button in ingredient div
    removeButton.addEventListener('click', () => {
        removeIngredient(ingredient.text)
        saveRecipes()
        renderIngredients(recipeId)
    }) 

    return ingredientEl
}

const renderIngredients = (recipeId) => {
    // Grab the ingredient display from the DOM
    const ingredientList = document.querySelector('#ingredients-display')
    const recipe = getRecipes().find((item) => {
        return item.id === recipeId
    })

    // Iterate through the list of ingredients on the page and render all items from recipeDOM
    recipe.ingredients.forEach((ingredient) => {
        const recipeDOM = generateIngredientDOM(ingredient)
        ingredientList.appendChild(recipeDOM)
    })
}

renderIngredients(recipeId)

我相信这个问题源于我的RenderingCredits功能,但我不知道如何解决它。同样,当我刷新页面时,我希望显示结果,但我希望避免使用location.reload。我希望RemoveIngCredit函数通过单击按钮删除成分,然后使用RenderingCredits函数刷新页面。还希望ToggleComponent函数只在我选中的配料旁边显示一个复选框,但实际情况并非如此。当我使用AddingCredit函数时也会发生同样的事情,添加的是成分,但是页面上已经存在的成分正在被复制

我想您应该在再次添加元素之前清除列表:

ingredientList.innerHTML = "";

天啊,一直以来,我只需要将它添加到RenderingCreditions函数的开头。谢谢