The array is not being updated with the new value entered in the input field for "address"

Currently, I have a table that loops through an array and displays the content in a list format. The address field is displayed as an input with the value set to

:value="location.address"
.

The input fields are initially disabled, but when the edit button is clicked, the disabled property is toggled to allow editing. To manage this process, I introduced a new property called editedAddress: null, which stores the current address being edited

this.editedAddress = this.locations[index].address
.

The issue arises when attempting to update the address after clicking the edit button. The following code snippet for the update button does not work:

btnUpdate(index){
       this.locations[index].address = this.editedAddress;
        this.locations[index].disabled = !this.locations[index].disabled
    }

Here is the complete code snippet for reference:

<template>
  <div>
  // Table structure and data binding
  </div>
</template>

<script>
// Vue component with data and methods
export default {
  data(){
    return{
      locations:[
        // Array of location objects with addresses and disabled properties
      ],
      address: '',
      editedAddress: null
    }
  },
  methods:{
    btnEdit(index){
     // Method to enable/disable editing mode and assign values
    },
    btnUpdate(index){
       // Method to update the address and toggle disabled property
    },
    btnDelete(index){
      // Method to delete a location entry from the array
    },
    addBtn(){
      // Method to add a new location entry to the array
    }
  }
}
</script>

If you have any suggestions or spot any errors in my implementation, please let me know. Thank you.

Answer №1

Your entry field is linked to location.address. Therefore, you are not altering your editedAddress in any way.

To modify the editedAddress, you can add

@change="editedAddress = $event.target.value"
to your input field

<input type="text" :value="location.address" :disabled="location.disabled" @change="editedAddress = $event.target.value">

Hint: utilize Vue Dev Tools or JSON.stringify to inspect the data in your vue app

JSON.stringify(editedAddress): {{JSON.stringify(editedAddress)}}

Here is the updated link playground with the adjustment made

Answer №2

It seems like there may be a misunderstanding with the code you provided for the update button not working as expected. Based on your description, it appears that the UI is not updating properly.

This issue could be related to reactivity in Vue's reactive system, which can be complex. You can learn more about reactivity in Vue at this link.

One possible reason for Vue not detecting changes in your array item 'locations' is that you need to explicitly notify Vue of the change using Vue.set:

btnUpdate(index){
  Vue.set(this.locations[index], 'address', this.editedAddress);
  // Repeat this process for any other lines that need updating
},

By implementing this workaround, Vue should recognize the changes and update the UI accordingly.

Alternatively, you can consider replacing the entire array if necessary, although this approach might be excessive in most scenarios:

btnUpdate(index){
  const locations = [...this.locations];
  
  locations[index].address = this.editedAddress;

  this.locations = locations;
},

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

Error in Webpack 5: Main module not found - Unable to locate './src'

When trying to build only Express and gql server-related files separately using webpack5, an error occurs during the process. ERROR in main Module not found: Error: Can't resolve './src' in '/Users/leedonghee/Dropbox/Project/observe ...

Switch from using fetch to utilizing axios for making API requests

I used fetch for a 'get' request, but now I want to switch to axios. Can someone please help me convert this code to use axios instead? More details: I am fetching an image and using the blob method to display it to the user. I would like to ach ...

What is the best way to include a JavaScript function in a dynamically generated div?

By clicking a button, I am able to dynamically generate a table row that contains a div element with the class "contents." $(document).on("click", ".vote", function() { // Removing selected row var rowLoc = this.parentNode.parentNode. ...

Steps to create a new window using Raphael JS

Is there a way to create a new window in Raphael similar to using "_blank"? ...

PHP AJAX Request Failing to Transfer Files

I am having trouble with sending files in a PHP AJAX Request: Here is the form code: <form class="frmContact" action="#" method="post"> <div class="col-md-6"> <input type="hidden" name="emailTo" id= ...

Ways to emphasize the chosen option in the Vuetify menu:

In the sidebar, I have a menu that utilizes vue-router: <v-list> <v-list-tile value="true" v-for="(item, i) in menu" :key="i" :to="item.path" > {{item.name}} </v-list-tile> </v-list& ...

I am looking to dynamically alter the CSS background URL using JavaScript

Currently, I am looking to update the background image url using JavaScript. Most tutorials I've come across involve having the image downloaded on the desktop and then providing its path. However, in my scenario, the user will input an image link whi ...

Errors with pointer events occurring within nested iframes on Chromium 78

At first glance, it seems like a bug specific to Chromium. I have already reported this issue in a bug report. Since progress is slow on that front, I am posting a question here primarily to see if anyone else has encountered similar or related issues and ...

Sending a variable to the resize() function in jQuery

Currently, I am working with two specific divs in my project: "#sidebar-wrapper" and ".j1". The height of ".j1" is dynamic as it changes based on its content. My objective is to set this height value to the "#sidebar-wrapper" element. I have attempted to ...

A bespoke Typescript implementation of nested lists containing numbers

Currently, I am trying to figure out how to declare and populate a TypeScript list of lists. The structure of the list should be as follows: List<CustomList<>, number> Typically, I would create a standard list like this: someList: { text: a ...

Leveraging JavaScript to incorporate smooth transitions like fade in and fade out during text changes

Below is my JavaScript code that switches between 2 words every 2.5 seconds. I want to enhance the transition between the words by adding a fading effect using jQuery. How can I integrate this into the recursive function? Any suggestions? var text = ["f ...

Activate Jquery to display the submenu when clicked and conceal any other open submenus at the same time

I'm trying to create a responsive menu with menus and submenus using JQuery. However, as a newbie to JQuery, I'm struggling to hide a previous submenu when displaying another one. Here's the CodePen link Below is the HTML code: <nav cl ...

Modifying the content of a webpage with the help of Tampermonkey

I'm working on changing a W Text into B using the code below. It's functional, but I'm experiencing two issues. First, there is a lag when refreshing the page where it briefly shows the W text before replacing it with a B. Second, every 20 o ...

Changes in React BrowserRouter URLs are not reflected on the page or components, requiring a manual refresh for them to

Greetings, fellow developers! I have developed an app using React with a remote menu component. Despite trying numerous techniques, I am facing an issue where my URL changes but the components are not rendering on the screen. You can check out the code h ...

The following page shrouded in mystery is experiencing technical issues

Encountering some issues on the live server with this code. It's functioning perfectly fine on my local machine, but once I try to deploy it on Netlify, I'm running into this error message: ⨯ useSearchParams() should be wrapped in a suspense bo ...

Utilizing JavaScript to update the content of a React page

Recently, I came across an API using Swagger for documentation. Swagger generates a ReactJs webpage to document and test the API endpoints. However, my lack of knowledge in React architecture has led to a problem: Prior to accessing any endpoint, an authe ...

Having trouble establishing a connection to SQL Server with tedious in Node.js

Trying to connect to the SQL Server "SQL.SCHOOL.EDU\STUDENTSQLSERVER,4500" from my school has been a real challenge for me using tedious. I am currently working on setting up a connection between my express back end and react front end. For now, I am ...

Assistance with Javascript Objects Using JSON and AJAX

Currently, I am utilizing Ajax to retrieve data from my Json file. A problem I am facing is that in one particular div of my html, I need to include both a heading and a paragraph. I attempted to create a property like so: "headingpara": "<h1> blah ...

Passing the state variable from a hook function to a separate component

I have a hook function or file where I need to export a state named 'isAuthenticated'. However, when I try to import this state using import {isAuthenticated} from '../AuthService/AuthRoute', I encounter an import error. My goal is to m ...

Issue with fullcalendar.js: event times are not appearing on agenda view

I'm struggling to get timed events to display correctly on the agendaWeek view. The first two events, which are mine, show up as all day events instead of at their specified times. However, the last event from the documentation displays in the correct ...