Vue fails to automatically update computed properties without manual intervention

I need to trigger Vue to recalculate a computed property by adding extra rows. Here's the code snippet:

var foo = this.groups;
this.groups = {};
this.groups = foo;

You can see it in action in this fiddle: https://jsfiddle.net/8bqv29dg/. If these extra rows are not added, the available_groups property does not update.

What is causing this behavior and how can I make sure that available_groups updates along with groups?

I've already attempted to add groups to "deep-watched", but unfortunately, it didn't solve the issue.

Answer №1

When updating data objects in Vue, utilize $set to add a new property:

   methods: {
    add_group: function(key, name) {
      this.$set(this.groups, key, {key, name});
    },
  }

For more information on Vue reactivity, check out the details here.

Answer №2

Vue's reactivity system does not automatically detect new elements added to an object:

Check out this link for more information on Change Detection Caveats in Vue

To overcome this, you can either use Vue.set or reassign the entire object as shown in the example below:

new Vue({
  el: "#app",
  data: {
    groups: {1: {key: 1, label: 'Those guys'}},
  },

  computed: {
    available_groups: function() {
        return [{value: 0, label: 'Anyone'}].concat(Object.values(this.groups)); 
        },
    },
  methods: {
    add_group: function(key, name) {
        Vue.set(this.groups, key, {key: key, name: name})
    },
  }
})

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

Looking to attach the "addEventListener" method to multiple elements that share the same class?

I'm trying to use an event listener in JS to handle the logic in the "onClick" function, but it's only executing once. I've applied the same class to all four buttons, so I'm not sure why it's only working for the first one. HTML: ...

React and Express failing to display content

After relocating my React frontend folder to my collaborator's Express backend folder, here is our updated file structure. https://i.stack.imgur.com/i77XJ.png This code snippet represents app.js which is responsible for rendering the website. const ...

Transfer the values of two divs into a unified output div

I am working on a task that involves two text boxes, one with an id and the other with a class. The goal is to copy the values from both text boxes into a single div when a button is clicked. So far, I have made an attempt at this but only the value from t ...

implement CSS styles within a JavaScript function

I am trying to change the background every second with the following code, but I am having trouble including CSS properties like centering, covering, and positioning the image properly. How can I modify this function to incorporate these settings for the ...

What are 4 different methods to tap into app closure?

Currently, I am in the process of creating regression test scripts for an express 4 application. However, when the tests are completed and the server is shutting down, an issue arises with mongodb not being closed properly. Is there a method to incorpora ...

Local variables in AngularJS across multiple Angular applications

Looking for a method to retain a local variable not affected by path or angular app changes. Attempted using $window.localStorage.set and get item, rootScope, and $window.variable with no success. ...

Exploring Grails Assets, Redirections, Secure Sockets Layer, and Chrome

Using Grails 2.1.1 with the resources plugin, I have encountered an issue when incorporating the jstree library which comes with themes configuration: "themes":{ "theme":"default", "dots":false, "icons":true } The JavaScript in the library locat ...

It is impossible to alter the data contained within the input box

Objective: Upon clicking a button to display the modal, the selected data (first and last name) should be inserted into an input box and editable. The user should be able to modify the data. Issue: The data entered into the input box cannot be edited ...

Is it possible to display events on fullcalendar.io using PHP and MySQL?

I'm currently trying to integrate a calendar feature on my website using PHP's "while" command. I don't have knowledge of JSON or AJAX, so I'm unsure if those are viable options for me. The issue I'm facing is that the current code ...

Issue with Mockjax: asynchronous form submissions are not being intercepted

Currently, I am facing an issue while using qUnit and mockjax to handle a basic async form submission. It seems like the async POST request is passing through mockjax for some reason. test 'RuleModal closes the modal on a successful form submission e ...

The issue arises when the jQuery $.get() method fails to deliver the expected response to the client, despite returning a status code of

I am having trouble with sending a REQUEST to a server in order to retrieve a message. I have tried using the jQuery method $.get(), and it seems to have successfully reached the server. However, I am facing an issue where I am unable to send a RESPONSE b ...

Practical steps for programmatically adding or removing md-disable-backdrop in md-sidenav

Can the md-disable-backdrop attribute be removed from HTML? The issue arises when opening the side navigation as the md-sidenav does not have the md-disable-backdrop attribute. After making a selection from the form displayed in the side navigation, I now ...

Strategies for selecting glyphs in SVG fonts based on their Unicode properties

My goal is to extract specific characters from SVG fonts created for music engraving. Music fonts typically include a large collection of characters (> 3500), but I only require a small subset of them for caching glyphs in compressed form to ensure quic ...

Issue with jQuery.parseJSON causing empty data structure to return

I am facing a puzzling problem with manipulating data in JavaScript. The code snippet I am using in JavaScript is to fetch data from a PHP/MySQL source. var _response = jQuery.ajax({ url: "../data", async: false, type: "post", data: oPara ...

The functionality of $j is not available

I am dealing with a simple jQuery event. Within the HTML document, I have the following code: <div class="opsContainer"></div> To utilize jQuery functionality, the following script is added: <script src="https://cdnjs.cloudflare.com/ajax ...

The jQuery plugin is not functioning properly on the subdomain

Encountered an issue where a jQuery plugin is not loading in Chromium for a peculiar reason. Interestingly, the plugin loads fine when accessing the page through the root domain but fails to load when accessed via a subdomain in Chromium. However, everyth ...

Creating a ReactJS table with content that updates dynamically

I am working on creating a dynamic table that can display an array of objects with the ability to toggle between Show More and Show Less. Although I have successfully implemented the dynamic content, I am struggling to add the functionality for Show More/ ...

Sending data between Angular and Python using both strings and JSON formats

Seeking assistance with a Python script that sends events to a server. Here is the code snippet: LOGGER = logging.getLogger("send_event") POST_EVENT_URL = "http://localhost:3000/event/" def send(name, data): url = POST_EVENT_URL + name headers = {& ...

Incorporating min.js file into a ReactJS application

I have received a min.js file from a third-party source and now I am looking to integrate it into my existing ReactJS project. While exploring some sample codes on how to start using their third-party features, I noticed that each example follows a simila ...

Maintain Bullet and Arrow Size with JSSOR Scaling Feature

I am currently utilizing the jssor image slider with jquery and overall, it is functioning well. However, I have encountered an issue when attempting to resize the slider. My goal is to dynamically resize the slider whenever the window width changes. The ...