Cannot update VUEjs array following the splice operation

My array is only updating in the console and not in the DOM. I've already tried using :key but it's still not working. Here is my code:

<div style="margin-top: 5px;" v-for="(file, index) in editedItem.file_name" 
                                         :key="editedItem.file_id[index]" >
    <a :href="`/biodidb/storage/${file}.${editedItem.file_type[index]}`" target="_blank">
        {{`${file}.${editedItem.file_type[index]}`}}
    </a>
    <span style="cursor: pointer;" @click="deleteData(editedItem.file_id[index], 'photos', index)">
        <v-icon
            small
            class="ml-3"
        >
           delete
        </v-icon>
        Delete 
    </span>
</div>

JS:

deleteData(id, type, index) {
    this.editedItem.file_name.splice(index,1);
    this.editedItem.file_type.splice(index,1);
    this.editedItem.file_id.splice(index,1);
}
enter code here

I have a method called update as well. When clicked, it assigns an object to the 'editedItem' in my data.

data() {
    return {
        editedItem: {}
    }
},
methods: {
      update(id, item) {
             this.editedItem = Object.assign({}, item);
      }
}

Answer №1

What does your editedItem structure look like? Are you using the v-for directive to iterate over data in the following manner?


  data: {
    editedItem: {
      file_id: ['2'],
      file_type: ['type 2'],
      file_name: ['example'],
    }

Answer №2

If your information is structured in this way:

...
data:function(){
    return{
        editedItem{}
    }
}
...

Simply add an array to the editedItem like so

editedItem.file_name = ['myFileName'];

Afterwards, your new file_name attribute will not be reactive and will not be tracked by Vue.

To ensure that Vue tracks the arrays you intend to use, you must specify them in the data function:

...
data:function(){
    return{
        editedItem:{
            file_name:[]
        }
    }
}
...

Alternatively, you can add a new property to editedItem using Vue.set()

Vue.set(editedItem,'file_name',['myFileName'];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on getting a website name from a URL using Javascript

Is there a simple method to extract the site name from a URL string? For example: http://www.mysite.com/mypath/mypage -> www.mysite.com http://mysite.com/mypath/mypage -> mysite.com The JavaScript code runs on the mongodb CLI side, not within ...

Steps for creating a JSON object to send batch emails using Mailgun

I am looking to dynamically create a JSON object named recipient-variables using two separate arrays - one for emails and the other for first names and IDs. How can I write JavaScript code to achieve this? 'recipient-variables': '{"[em ...

How can I incorporate the 'client' application into the 'loading.js' file?

I implemented a Lottie component in my loading.js file. Utilizing the App router (Next13). This is the code for the lottie component: import { Player } from '@lottiefiles/react-lottie-player'; import LoadingLottie from '@/assets/loading.j ...

Using regular expressions to modify parameter values in a command-line argument between nodes and npm scripts

While experimenting with node.js, I encountered a perplexing behavior related to command line arguments: I have a program that utilizes a regex pattern to identify test files. This regex is passed as a command line argument: node index.js --require src/** ...

Explanation requested for previous response about returning ajax data to the parent function

After coming across a helpful answer in the question titled How do I return the response from an asynchronous call?, I attempted to implement it without success. Reviewing Hemant Bavle's answer (currently with 62 votes) gave me hope, but my implement ...

Insert text within the switch component using Material-UI

Looking to customize Material UI's Switch component with text inside? Check out the image below for an idea of what I'm going for. https://i.stack.imgur.com/KadRZ.png Take a look at my code snippet: import React from 'react'; import S ...

Utilize Express Node to display API information on HTML pages using Handlebars template

I'm seeking assistance in rendering data from an API to HTML using handlebars. I'm a bit puzzled on how to properly showcase the data on the webpage. Here's what I have so far: ROUTES FOLDER/FILE: const express = require('express&ap ...

What is preventing me from accessing React state within Tracker.Autorun?

I'm feeling lost on this one. I have implemented a Tracker.autorun function to monitor when my Mongo subscription is ready for querying (following the advice given in this previous Meteor subscribe callback). It seems to be working fine as it triggers ...

Is there anything else I should attempt in order to fix this npm start error?

I have been troubleshooting this issue by researching other stack overflow posts, but I continue to encounter the same error message repeatedly. My goal is to execute a Javascript program that incorporates ReactJS. Initially, everything was functioning sm ...

Draggable HighStock element causing issues with Gridster dragging

I have integrated a stocks chart from HighStocks with gridster, where each gridster block is draggable. However, the stocks time slider gadget can also be dragged and resized. When I move the slider on top of a gridster widget, the entire widget moves alon ...

Executing database queries in a synchronous manner in JavaScript

let positionConfig = require('setting'); function retrieveConfig(element) { let setting; positionConfig.find({element: element}, function (err,docs) { console.log(docs[0].current); // show the value setting = docs[0].curr ...

The connected callback does not trigger when custom HTML elements are being created

Attempting to craft a custom HTML tag using JavaScript, I aimed to create the custom element with ES6 JavaScript syntax. The code devised for this task reads as follows: customElements.define('neo-element', NeoElement); function NeoElement (){ ...

Retrieving user input in a JavaScript/React app

I'm currently developing a search feature for my website using Algolia. When the user types in their search term, the results are updated to show relevant matches as they go. Here is an example below of what I am working on: https://codesandbox.io/s ...

How can I send the special $event variable as an argument in Vue?

When using Vue, I am trying to listen for a change event and pass the $event variable to an action. Here is how I have set it up in my template: <template> <div> <span>Filter Todos:</span> <select @change="filter ...

How can one identify a concealed glitch that exclusively occurs for a particular individual or hardware in a React environment?

Is it possible to identify a bug that occurs only with a particular individual or hardware in a React application? This bug is invisible and never appears during tests, but only manifests with a specific client within my company. Do you have any tips on h ...

Utilizing Vue.js to dynamically load JavaScript files depending on the environment

I have a JavaScript file that is generated by AWS Cognito, and its content varies depending on the environment. As I am not supposed to manually modify it, using Vue's environment variables may not be a feasible solution for me. How can I dynamical ...

Displaying dates on the Amcharts category axis for instances with empty data

I am currently creating a fruit consumption chart for each day, and so far everything is working correctly in the provided example. var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "hideCredits": true, "fixedColumnWidth": '10px& ...

Counting up in Angular from a starting number of seconds on a timer

Is there a way to create a countup timer in Angular starting from a specific number of seconds? Also, I would like the format to be displayed as hh:mm:ss if possible. I attempted to accomplish this by utilizing the getAlarmDuration function within the tem ...

Issues with JavaScript causing slideshow to malfunction

I'm experiencing some issues with my image slider. It seems that during the initial loop, the slideshow keeps reverting back to 'image3'. However, after the first loop, everything appears to work correctly. I am looking for a solution to ens ...

What is the best way to generate conditional test scenarios with Protractor for testing?

Currently, there are certain test cases that I need to run only under specific conditions. it ('user can successfully log in', function() { if(siteAllowsLogin) { ..... } The problem with the above approach is that even when sitesNo ...