Skip to content
Advertisement

Vue: Removing item from v-for list leaves behind canvas item

This is a hard one to phrase in a title, but pretty easy to explain with some setup.

I have a list of components. Each component in the list looks like the following:

JavaScript

Each component has a small canvas element that is used to display a preview of the item, It’s name, and a delete button to remove it from the list. The list that the v-for is iterating through is stored in Vuex.

Using letters to represent different preview images, a list might look like this:

JavaScript

When the delete button is pressed, the appropriate item is spliced out of the list and the list updates. The names update, and clicking on them results in the correct item being selected. The problem is that the previews stay in their same position. For example, if I deleted Item 2 (with preview X) from the list above I would have the following list:

JavaScript

The previews are WXY, when they should be WYZ since X was deleted. Basically the canvases are remaining in the same order, and one is just being deleted off the end, no matter where the item was deleted from.

I can think of a couple hackey ways of redrawing all the canvases when an item is deleted, but I was wondering if there was a better solution.

EDIT: Here’s the code that deletes the item

Delete button on item emits event to list component

JavaScript

Wrapper then dispatched Vuex action

JavaScript

Vuex action and mutation

JavaScript

Here’s a screenshot of the issue: Screenshot of Vue issue

The rest of the data is correct, as I added a quick test function to print the current ID and data on click and everything prints correctly. It appears it’s just the canvas element that’s not changing.

EDIT 2: The vue code

JavaScript

Advertisement

Answer

Assuming cat_ID can be assigned to more than one asset, you’ll run into problems using it as a key as Vue will not be able to detect list changes if the cat_ID is repeated in any of your selectedList assets.

Since your assets have an ID property, you should use that in your key

JavaScript

See key

The key special attribute is primarily used as a hint for Vue’s virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list. Without keys, Vue uses an algorithm that minimizes element movement and tries to patch/reuse elements of the same type in-place as much as possible. With keys, it will reorder elements based on the order change of keys, and elements with keys that are no longer present will always be removed/destroyed.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement