Vuejs failing to update data

Let me begin by describing my scenario. I have a fee_map attribute in my data, which is a list of objects containing information about the fees students have to pay, their balances, payment methods, etc. Additionally, there is a computed property called 'updateOptions' that generates a list of objects with IDs and text suitable for populating a select2 dropdown menu (payment mode). This 'updateOptions' property is called whenever a user interacts with the system. Depending on other user actions, the Program will choose a selected option and update the fee_map structure, which looks like this:

data: {
    fee_map: {
        1: {
            details:{
                1: {
                    option_selected: "1",
                }
            }
            // other attributes
        },
        2: {
            details:{
                1: {
                    option_selected: "2",
                }
            }
            // other attributes
        },
    }

I have a method called UpdateSelected, which updates the selections by iterating through the fee_map using keys and nested forEach loops. The selected option is then set as follows:

var fee_map = this.fee_map;
t_keys = Object.keys(fee_map);
t_keys.forEach(function(t){
    f_keys = Object.keys(fee_map[t].details);
    f_keys.forEach(function(f){
         fee_map[t].details[f].option_selected = "2";
    });
});

However, when I update the 'option_selected' value in this manner, the fee_map does not reflect the new value. What could be the issue here?

Answer №1

The hierarchy of your object elements goes one level deeper than the iteration you are currently applying. This results in the key "details" not being accessed correctly. To solve this issue, you should iterate through the details object as well, as demonstrated in the example below:

https://example.com/jsfiddle123

var fee_map = this.fee_map;
  t_keys = Object.keys(fee_map);
  t_keys.forEach(function(t){
    f_keys = Object.keys(fee_map[t]);
    f_keys.forEach(function(f){
      d_keys = Object.keys(fee_map[t][f]);
      d_keys.forEach(function(d){
         fee_map[t].details[d].option_selected = "5";
      });
    });
  });

Answer №2

After facing numerous challenges, I finally discovered that the code was updating data too rapidly within a loop. However, Vue was only detecting these changes on the subsequent tick. To resolve this issue, I implemented a self-invoking function as a wrapper for Vue.nextTick. This allowed me to maintain the context and successfully update the selected_option within the Vue.nextTick callback, resulting in flawless functionality.

See the functional code snippet below:

(function(fee, ref){
    console.log("Registering vue tick");
    Vue.nextTick(function(){
       console.log("Vue ticked, updating selected option");
       fee.option_selected = ref;
    });
})(fee, ref);

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

Getting started with Babylon.js using npm: A step-by-step guide

I recently posted about my struggles with setting up Babylon.js using npm on Stack Overflow. Since I haven't received any answers yet, I was hoping to rephrase my question: Can someone provide a detailed step-by-step guide on how to set up Babylon.js ...

The animation in Three.js may come to a halt when the "skinning = true" setting is employed alongside THREE.ShaderMaterial

Currently, I am developing a game using WebGL and three.js, and I am in need of a skin shader for my character models. However, I have encountered a problem where if I use THREE.ShaderMaterial, the animation stops. Can anyone provide assistance or guidance ...

I'm curious about the origin and purpose of req.user - where does it come from and

Recently, I've been delving into Nestjs and found myself a bit puzzled by the req.user. Where does this come from and do we have to manually request it? What exactly is req.user and what advantages does it offer? Must I assign payload to it manually? ...

Tips for triggering a function when the range slider is adjusted

I'm looking to trigger a function whenever a user changes a range slider in my Vue.js project, and I also need to pass the new value to that function. The code snippet below shows what I have so far. <div cla ...

Proper Structure for Node System (BASIC)

Overview Recently, I entered the world of Node.js and built some basic back end functionality. However, I realized that everything was clustered in one file (index.js) which led me to explore tutorials on using express router middleware and adapting a mod ...

Retrieve the total number of hours within a designated time frame that falls within a different time frame

Having a difficult time with this, let me present you with a scenario: A waiter at a restaurant earns $15/hour, but between 9:00 PM and 2:30 AM, he gets paid an additional $3/hour. I have the 'start' and 'end' of the shift as Date obje ...

Utilizing ES6 promises in node.js to send a null response

I'm looking for assistance on how to execute a query using ES6 native promises in node.js. The code I have below is what I've been working with: let arr= []; conn.query('select * from table1', (err, b) => { for (let i = 0; i ...

What steps should I take if the slackbot is properly functioning after being invited to the channel?

Using an OAuth2 token I am looking to automate the process of sending data from Google Sheets via Slackbot. Even though I have set up tokens and connections, I find that I still need to manually input the channel id into my script. In order to streamline ...

typescript throwing an unexpected import/export token error

I'm currently exploring TypeScript for the first time and I find myself puzzled by the import/export mechanisms that differ from what I'm used to with ES6. Here is an interface I'm attempting to export in a file named transformedRowInterfac ...

Utilizing mapped data to display numerous Material-UI Dialog elements

On my table, I have a list of users displayed. Each user has a button in their row to delete them. Clicking the delete button triggers a Material-UI Dialog to confirm. An issue arises where 3 dialogs are being rendered due to mapping, and the last dialog ...

What is the best way to pass a variable value from a JavaScript function to a Python function within a Django framework

My goal is to use HTML and JavaScript to scan a QR code in nurse_home.html. I have successfully scanned and viewed the content of the QR code on the website, but I am facing an issue with POSTing the QR code output represented by <output type='POST ...

Initiate an Ajax request solely for the elements currently visible on the screen

I am currently facing an issue that requires a solution. Within our template, there are multiple divs generated with the same classes. Each div contains a hidden input field with the ID of the linked target site. My task is to utilize this ID to make aja ...

Why is a wrapper essential in jQuery?

Similar Question: What is the significance of (function($) {})(jQuery); in jQuery? Why does the function (function($){})() sometimes include the word jQuery? Seen this code snippet used in various places, and while I know it's essential for s ...

Issue: The system is unable to locate the module labeled './lib/binding/napi-v3/argon2.node'

After attempting to install bcrypt or argon2 with the command npm install --ignore-scripts --omit=dev, an error occurred: app-1 | node:internal/modules/cjs/loader:998 app-1 | throw err; app-1 | ^ app-1 | app-1 | Error: Cannot find modul ...

Toggle visibility between 2 distinct Angular components

In my application, I have a Parent component that contains two different child components: inquiryForm and inquiryResponse. In certain situations, I need to toggle the visibility of these components based on specific conditions: If a user clicks the subm ...

Alter appearance of images depending on browser window size

I am working on an angular JavaScript code snippet that uses the ng-repeat command to display a row of small images. I want to ensure that these images do not spill over into a second line when the browser window is resized. Instead, I prefer them to eith ...

Split a string containing integer and decimal values into an array

I encountered an issue when converting a string to an array: var data = "[[1, 2, 3,], [3, 2, 1]]"; var array = JSON.parse(data.replace(/,\s*]/g, ']')); This problem arises when handling floating point numbers in the input: var data = "[[2 ...

Tips for executing specific javascript on small screens or mobile devices

I am currently in the process of developing a Vue web application that needs to be functional on all devices. I have certain code that should only run on small screens or mobile devices. Right now, I am using an if statement with $(window).width() to achie ...

Navigate to the vite Frontend by utilizing the domain specified in the /etc/hosts file

When I was on node v16, I could easily set up my hosts file like this: #/etc/hosts 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 my-domain.test and then access the site through my-domain.test:{port} Howeve ...

Identify 404 errors in multi-file routes with Express

Recently, I've been diving into using Express and I'm facing some challenges with detecting 404 errors. In my main file app.js, I launch the server and specify different files to handle various requests. For instance: const app = express(); app ...