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

Angular JS is encountering an issue where the promise object is failing to render correctly

I'm currently learning Angular and I have a question about fetching custom errors from a promise object in Angular JS. I can't seem to display the custom error message on my HTML page. What am I missing? Below is my HTML file - <!DOCTYPE htm ...

Using buttons as spinners for input elements with identical styles but unique identifiers

Currently, I am in the process of developing a project that involves users. In order to display all users, I am executing a query on an SQL database. After styling the interface, I have added an input element beside each user, which initializes at zero. Ad ...

Neither Output nor EventEmitter are transmitting data

I am struggling to pass data from my child component to my parent component using the Output() and EventEmitter methods. Despite the fact that the emitter function in the child component is being called, it seems like no data is actually being sent through ...

Tips for parsing a JSON file within my node.js application?

I am working on a node.js service that involves validating JSON data against a schema defined in jsonSchema. Below is the code snippet for my service: app.post('/*', function (req, res) { var isvalid = require('isvalid'); ...

Having trouble retrieving the JSON value as it returns undefined

Having trouble extracting specific values from JSON data. I'm trying to retrieve the value of result.datafeed[0].prod[0].vertical[0].deviceProductJson[0].product_brand, but it keeps returning as undefined. To investigate further, I examined the stru ...

Establishing a distinct registry for a particular package within the .npmrc configuration file

Today, I encountered a new challenge that I've never faced before. I am currently in need of having private node packages published in both a private and public repository under the same @scope. The packages on npmjs.org are stable and open to the pu ...

Incorporate fresh Google sites into different web pages using iFrame integration

Wishing you a fantastic day! I am currently working on embedding a brand new Google site into another webpage. I attempted to use an iframe for this purpose, but unfortunately it did not work as expected. Here is the code snippet: <iframe width="1280 ...

What are the best techniques for organizing SCSS in Next.js?

There are multiple pages with some unused items. Is there a method to search and delete all the items based on their classname? Appreciate any assistance you can provide! ...

Exploring the internet with Internet Explorer is like navigating a jungle of if statements

I am facing an issue with my code that works well in Chrome, but encounters errors in IE. The if condition functions correctly in Chrome, however, in IE it seems like the first condition always gets executed despite the value of resData. Upon analysis thro ...

What sets apart .ejs from .html files?

In my Node Js project, all front end files are in .ejs format and call the server.js for backend work. 1) Is there additional functionality provided by ejs compared to html? 2) Does this pertain to expressJs functionality in Node? 3) Can I use angularJs ...

Ways to update the entire MobX observable array with new values

When working with MobX, is there a way to update the values of an observable array without re-setting every value? At first glance, one might think the solution is: let arr = observable([]); autorun(() => { console.log('my array changed!') ...

Enhancing Kendo Grid with Checkbox Columns

I am facing a situation with my kendo grid where I need to insert two checkbox columns in addition to the existing set of columns. <script id="sectionPage" type="text/kendo-tmpl"> @(Html.Kendo().Grid<SectionPageModel>() .Na ...

Class for Eliminating the Background Image Using Bootstrap

Is there a Bootstrap class that can be used to remove a background image from a div? Currently, I have this style defined in my CSS: background-image: linear-gradient(to bottom, rgba(0,0,0,0.1), rgba(0,0,0,0)); I would like to remove it using: bg-img-non ...

Only match the character if it is not at the beginning of the line and if another character is not on the

Is there a way to only match the character "=" in a string if it is not at the beginning of a line and no other character, for example "$", appears on the same line? The equal sign should not be at the beginning of a line No other character, such as "$", ...

Interactive material design drop-down menu

Currently, I am working on a dynamic drop-down menu that utilizes material-ui js. However, I have encountered an issue where clicking on one menu opens all the menus simultaneously, and vice versa when trying to close them. If you would like to view the c ...

Dynamic Content Sorting with JavaScript and jQuery

I am presenting various courses that are available, and I am utilizing JavaScript/jQuery to display or hide them based on element classes. You can view the page here. Currently, the script conceals all content on the page and then reveals items that matc ...

Update all items in the menu to be active, rather than only the chosen one

Here is the layout of my menu along with the jQuery code included below. The functionality is such that when I click on Home Page, its parent element (list item) receives an active class. Currently, when I am on the Home Page, the list item for Account Co ...

Updating a Rails partial using JSON response from an AJAX post request

I recently implemented an AJAX post request in my backend Rails application to upload a file. Upon successful posting, the response contains a JSON list of files. Now, I face the challenge of reloading the file list on my 'detalhes.html.erb' vie ...

Locate the line number of a specific word within a paragraph using JavaScript

Imagine a scenario where there is a lengthy paragraph. By clicking on a specific line, JavaScript/jQuery will dynamically insert an empty <span> tag at the beginning of that particular line - just before the initial word. For example, take a look at ...

The React Stripe API is functioning perfectly on local servers but encountering issues in the live environment

Just when I thought I was almost finished, reality hits me hard! My deadline is right around the corner! I finally got everything working on my local machine with a stripe payment form. However, when I pushed it live, I received an error message from my A ...