What could be causing VueJs2 computed properties to not update?

Check out this updated grid example from the official examples. In my application, I need to reposition the sortOrders array.

 created: function () {
        var vm = this;
        this.columns.forEach(function (key) {
      vm.sortOrders[key] = 1
    })
  },

I attempted to sort a column, but the sorting only occurs once.

To replicate the issue: Click on the "name" column header more than once. Expectation: The column should be sorted each time the header is clicked. Reality: The column is only sorted once.

Link to code sample

I'm puzzled as to why

Vue.set(this.sortOrders, key, order);

does not update the this.sortOrders variable.

It works correctly if a temporary variable is used like so:

   var order = this.sortOrders[key]*-1;
   var tmp = this.sortOrders ;
   Vue.set(tmp, key, order); 
   this.sortOrders = [];
   this.sortOrders = tmp;

Alternative code sample

Answer №1

It seems like Vue is not triggering the updates when switching from 1 to -1, which appears to be unexpected behavior. Consider reporting an issue on Vue's Github page for clarification from a vue developer. As a temporary solution, you can reset this.sortKey to null before updating the key value:

sortBy: function (key) {
  this.sortKey = null;
  this.sortKey = key;
  var order = this.sortOrders[key]*-1;
  Vue.set(this.sortOrders, key, order);
}

Check out the JSFiddle example here: https://jsfiddle.net/xkkbfL3L/1920/

Answer №2

Received a helpful response on the Vue Github page https://github.com/vuejs/vue/issues/6933

The issue stems from the fact that the app is already creating those keys before that specific moment, particularly in the created phase - where $set() is not being utilized.

created: function () {
    var vm = this;
    this.columns.forEach(function (key) {
  vm.$set(vm.sortOrders, key, 1)
})

},

https://jsfiddle.net/Linusborg/xkkbfL3L/1927/

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

Creating Personalized Checkboxes for Internet Explorer Versions 7 and 8

I am currently in the process of implementing custom checkboxes for my website. Everything is functioning smoothly on browsers such as IE9 and other modern ones. However, I am encountering issues with IE8 and 7. Below is a snippet of my CSS code: input[typ ...

splitting xmlhttp.responsetext using loops

Currently, I have a JavaScript function that utilizes xmlhttp.responsetext to fetch data from MySQL. This data is then used to populate form fields in a dropdown menu, which has been functioning perfectly so far. However, the issue arises when I attempt to ...

The Chrome browser allows interaction between two separate divs, where a button located in one div can impact

One issue I encountered involves a button located within a div that has unintended consequences for another div. Here is the basic structure of the elements: <div> <div> <div> <div> <butto ...

Are there any user interface frameworks available that can replicate the aesthetic of a Mac application?

I've been searching high and low but I haven't come across any answers yet. It caught my attention that the wunderlist mac app was developed using HTML/CSS/JS, but I'm curious if they incorporated a pre-existing UI JavaScript framework into ...

When attempting to use dynamic imports with `react-icons`, NextJS will import all necessary components and dependencies

My current task involves incorporating an Icon from the react-icons package into my project. However, when I attempt to do so using an import statement, the resulting bundle size looks like this: Route (pages) Size First Lo ...

Instructions for launching an Android app from a website

Is it possible to invoke my app from HTML? For instance, I am able to successfully call a webpage from my app using this code. android code: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse( "myDomain.com"))); ...

What could be causing my node.js postgres function to return undefined even though I verified the value is displayed in the console?

Currently, I am in the process of developing a node.js application with a PostgreSQL backend. I have implemented a pool of connections and made an INSERT query to the database where I anticipate the last inserted ID to be returned. However, while the query ...

Error! Unable to Inject ComponentFactoryResolver

Recently, I attempted to utilize ComponentFactoryResolver in order to generate dynamic Angular components. Below is the code snippet where I am injecting ComponentFactoryResolver. import { Component, ComponentFactoryResolver, OnInit, ViewChild } from "@an ...

There seems to be an issue with the way Meteor and React are displaying data

Having trouble displaying paint data in my code - all I see is [object,object]. Tried using .toString() without success. Any assistance would be appreciated. I understand that this indicates objects inside arrays but unsure how the array is generated or h ...

Exploring the Differences: NodeJS and ExpressJS for Server-Side and Client-Side HTML Rendering

Embarking on my journey with Node.js, I find myself grappling with the concept of server-side and client-side HTML pages. My objective is to hone my skills by creating an e-commerce web store. The technology stack that I have set my sights on includes Node ...

Handling a jQuery Ajax success callback when it fails

I am encountering an issue with my ajax post request where even though I have separate success, failure, and status code handlers, when the request fails with a 400 error, part of the success function is still being executed. Can someone provide insight in ...

The link does not respond to the first click

I'm having an issue with the search feature on my website. The page includes an auto-focused text input element. As the user types, an ajax request is made and JQuery fills a div with search results. Each result is represented by an <li> elemen ...

the pause in execution before my function redirects to a different route

Currently, I am developing a page using nodeJs with express which is supposed to display a table. However, I encountered an issue with my variable "allMusique" that contains the data for my page. When trying to access it initially, there seems to be an err ...

Tips for creating an onChange function in an input field using jQuery

I am looking to dynamically create inputs where each input has an `onChange` function with a variable. Here is my code: var distinct_inputs = 0; $('.icon1').click( function(){ distinct_inputs = distinct_inputs + 1 ; $('#insert-file&apo ...

Executing Javascript with Ajax requested data - A guide to making your script run smoothly

Battlefield Page In the graphic provided, there is a battlefield page featuring 20 users. To capture and store this data in a MySQL database, I have created a JavaScript script. However, an issue arises when trying to collect data from the next page after ...

Why is `screen` important?

Recent articles in June 2020 discussing "how to utilize react testing library" often showcase a setup similar to the one below: import React from 'react'; import { render, screen } from '@testing-library/react'; import App from '. ...

`Why setRequestHeader is essential for Ajax and XMLHttpRequest`

Should I ever manually specify the setRequestHeader as 'application/x-www-form-urlencoded' for an ajax POST request? Is it necessary to manually define the setRequestHeader as 'multipart/form-data' when uploading files via ajax? Do XMLH ...

Is there a way to prevent redundancy when exporting functions in Node.js?

Is there a way to avoid repeating module.exports or usuariosControllers when writing a small API in express for fun? module.exports.getUsuarios = (request, response) => {}; module.exports.crearUsuario = (request, response) => {}; module.exports. ...

The issue at hand is that one of the JavaScript buttons is functioning properly, while the other is not playing the video as intended. What steps can

I've been working on a script for my school project website that should make buttons play videos, but I'm running into an issue where it only works for the first button (btn). Programming isn't my strong suit, and I put this together based o ...

Is the JavaScript progress bar dysfunctional when used with object-oriented JavaScript code, but functions properly with arrow functions?

After posting a question about the JavaScript progress bar not working with object-oriented JavaScript code on Stack Overflow, I decided to try rewriting the script using arrow functions. Here is the new code: document.getElementById("barButton").addEve ...