Why isn't my Vue2 data updating in the HTML?

Starting my journey with Vue, I have been finding my way through the documentation and seeking support from the Vue community on Stack Overflow. Slowly but steadily, I am gaining a better understanding of how to create more complex components.

The issue I am facing pertains to a custom select component where the data for the selected option is accurate, but the displayed HTML is not reflecting it correctly. This problem arises when the custom select component is nested within multiple other components in my application.

To provide a clear example, I have created a Minimal Working Example which can be accessed via Code Sandbox. Each component is organized into its own file for easier readability.

Description of the Component:

https://i.sstatic.net/T4Xwf.png

The purpose of this component is to act as an advanced data filter. It accepts an object that mimics a database structure, where each key represents the "id" of its corresponding value ("record"). The component allows users to filter based on the keys (properties/fields) of the record.

The main top-level component is called AdvancedFilterTable, which enables users to dynamically add and remove filters represented by AdvancedFilterRows.

A filter row consists of 5 simple components:

  • logic: represents the filter condition (AdvancedFilterSelectLogic)
  • function: specifies the operation to be performed on the field (AdvancedFilterSelectFunction)
  • property: indicates the field to be filtered on (AdvancedFilterSelectProperty)
  • conditional: determines the test to apply to each record's specified field (AdvancedFilterSelectConditional)
  • value: the value against which the conditional statement is tested (AdvancedFilterInput)

For example:

{ logic: 'and', function: 'identity', property: 'x', conditional: 'gt', value: 5 }

I recently introduced a feature which caused a bug - dynamic options for AdvancedFilterSelectFunction and AdvancedFilterSelectConditional based on the type of AdvancedFilterSelectProperty. When a specific property is selected, such as 'x' which corresponds to an array of numbers, the available options should change accordingly in the function and conditional selectors. However, if a certain function like 'mean' is applied (which changes the data type), the available conditionals should be adjusted again.

In the provided MWE, I have dummy data structured as follows:

let records = {
  a: { x: 1, y: "a string", z: [1, 2, 3, 4] },
  b: { x: 2, y: "strange?", z: [1, 2, 4] },
  c: { x: 3, y: "starts w", z: [1, 2, 3, 4] },
  d: { x: 10, y: "some let", z: [1, 2, 4, 5, 6, 7, 8] },
  e: { x: 2, y: "? qwerty", z: [1, 40] }
};

Where 'x' is of type number, 'y' is a string, and 'z' is an array of numbers.

Steps to Reproduce the Bug:

  1. Open the MWE link here: Code Sandbox https://i.sstatic.net/qZUtp.png
  2. Click the green plus icon to add a new filter

https://i.sstatic.net/Ldy0T.png

  1. Change the property selection from 'x' to 'z'

https://i.sstatic.net/EtQgw.png

Upon examining the components, all the relevant data seems to be set accurately. Even the function selector and the conditional selector dynamically update to include options specific to array-type data. However, the display still shows 'x'. Attempting to reset the selection back to 'x' does not work as it remains stuck on 'x'...

All the data of the custom select components flow through the various encapsulating components according to the technique explained in Vue2: handling multi-child prop synchronization with models. An illustrative fiddle of the solution can be found at: https://jsfiddle.net/SumNeuron/ejq86c73/9/

Solution:

Answer №1

Within the AdvancedFilterSelectProperty.vue component, there is a prop named selected that updates as the user makes selections. However, the <select /> element's selected property is comparing against the expression key == value.

The value being compared comes from the data of the component. The issue arises because the data does not update when the user selects a different option, but the selected prop does.

To fix this, modify the comparison in the <select /> element to check against the selected prop instead of the value in data.

<select @change='updateSelected($event)'>
    <option
        v-for='(val, key) in options'
        :value='key'
        :key="key"
        :selected='key == selected'
    >
        {{val.text}}
    </option>
</select>

By making this adjustment, the issue should be resolved.

View a working example at https://codesandbox.io/s/38vlwq4yx6

Answer №2

The select inputs you are using do not have any model associated with them, so the value is not displayed. However, it is still being broadcast. Since you are delegating the value manipulation from the parent using props, you will need a computed property to handle this. Here is an example:

computed: {
  selectedVal: {
    get() {
      return this.selected
    },
    set(value) {
      this.$emit('updateSelected', value)
    }
  }
}

You can then apply this to your select (especially the AdvancedFilterSelectProperty component):

<select v-model="selectedVal">

It is not necessary to use the @input function here since our setter is emitting to the parent when we select an <option> and the value changes.

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

A guide on tallying the characters within an input text

I need to develop a JavaScript program that includes a text field for entering formatted text, and simultaneously displays the count of each character in another div. For instance: Displaying characters appearing 17 times in a text of 100 characters; Ca ...

I'm looking to customize the background color and font size for an accordion in Bootstrap 4 with Vue.js. Can anyone provide guidance

Hello, I am currently using an accordion with Bootstrap 4 and Vue.js. Here is the code snippet: <div class="faq_accordion" role="tablist"> <b-card no-body class="mb-1"> <b-card-header header-tag=" ...

What are some creative ways to visually distinguish a TextField that is in readOnly mode?

I'm currently working on creating a form using the Material-UI library. I'm having difficulty figuring out how to distinguish my TextField when they are in readOnly mode versus edit mode. At the moment, they appear identical and I would like the ...

Typescript is unable to comprehend that the initial item in an array of strings is considered to be a string

Here are the functions I am working with: const transitionGroup = ( propertyName: string, durationMultiple = 1, timingFunction = 'linear', delayMultiple = 0, ): string => { // ...more logic here return [propertyName, duration, tim ...

The span's onclick function seems to be malfunctioning

I am encountering an issue where the Onclick event is not triggering on a specific tag. Strangely, the same onclick event works perfectly fine when bound to an image element. I am currently developing a follow and unfollow feature using PHP and jQuery. How ...

Presenting information extracted from the Meteor 1.0 framework

At the beginning of my Meteor application, I import data from a JSON file using the following code: 13 if (Meteor.isServer) { 14 15 //startup 16 Meteor.startup(function () { 17 if(Plugins.find().count() === 0) { 18 var plugins_data = J ...

Encountering a problem with CRUD operations when attempting to edit and save data from a table using

When attempting to edit the information by clicking the radio button, the details are displayed in the appropriate boxes but the existing data in the table is deleted. My goal is to utilize a single array/scope variable for editing, displaying, and deletin ...

What is the process for deleting an event in Vue?

My Vue instance currently includes the following code: async mounted () { document.addEventListener('paste', this.onPasteEvent) }, beforeDestroy () { document.removeEventListener('paste', this.onPasteEvent) }, methods: { onPasteEv ...

The occurrence of the "contextmenu" event can cause disruption to the execution of a function triggered by the "onmousemove" event

Currently in the process of developing a Vue application utilizing a Pinia store system. Within my BoxView.vue component, I have created a grid layout with draggable elements that have flip functionality implemented within the BoxItem.vue component. Spec ...

Utilize Laravel Echo and broadcasting to generate interactive channels

I'm currently working on implementing a chat feature that allows users to engage in 1 to 1 conversations or group conversations. The technologies I am using for this project are Laravel 5.5 and Vue.js. After reviewing the documentation, I discovered ...

What is the best way to display a component based on a certain condition?

There are two instances where the Items component needs to be displayed. However, in one instance an additional component, Filters, is required while in another it is not. The Filters component is nested inside Items. When attempting to implement this, ...

The Angular custom modal service is malfunctioning as the component is not getting the necessary updates

As I develop a service in Angular to display components inside a modal, I have encountered an issue. After injecting the component content into the modal and adding it to the page's HTML, the functionality within the component seems to be affected. F ...

What is the best way to transfer data from my browser to the backend of my application?

I am currently working on developing a basic weather application using Express and Node.js. To accomplish this, I need to automatically retrieve the latitude and longitude of the user. While I understand how to achieve this through HTML5 Geolocation in t ...

Steps to load data using ajax-php with specific parameters

I have a situation where I need to trigger a JavaScript function when a link is clicked. <input type='hidden' id='name'> <a href='#' onclick='getUsers(1)'>Click here</a> function getUsers(id){ $( ...

Saving Data in an Angular Material Table: A How-to Guide

I have a basic angular material table and I am looking for a way to save the data displayed in each row when a button is clicked. Is it possible to save each row of data as an object and push it to an array? If so, how can I achieve this? <div class=& ...

Encountering a 500 internal server error while accessing the web server

Anyone out there able to assist? My web service seems to be throwing an error. Receiving a 500 Internal Server Error and 304 Not Modified message The requested XML data is not displaying the body content as expected. var soapMessage ='<soap:E ...

Tips on effectively transferring formarray to another component

I'm attempting to pass a formarray to a child component in order to display the values within the formarray there. Here is my current code, but I am struggling to figure out how to show the formarray values in the child component. app.component.html ...

Generate a collection of elements using a different collection as a reference

I am struggling with an array of objects: let data = [{ createdDate: "2222", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087c6d7b7c3d487c6d7b7c266b6765">[email protected]</a>", histories: [ ...

Steps for showcasing the data entered in the input box

I am looking to enhance my skills in working with Angular and JavaScript. I would greatly appreciate any feedback on the issue I am facing here. My goal is for the text box input to display: "hello {name} , would you like to play a game? However, curr ...

Is there anyone who can assist me with the problem I'm facing where Javascript post call is sending a null value to my mongoDB

As a beginner in JS, NodeJS, and MongoDB, I decided to create a quiz website to sharpen my coding skills. However, I have encountered an issue while trying to send the username (string) and total marks (int) to MongoDB using the Post method. Surprisingly, ...