V-chip fails to trigger parent input event

Struggling to resolve this issue independently, I find myself completely stuck. Any assistance is appreciated. Essentially, this component generates a 100x10 matrix like the one shown below:

Threat1: AssetType1 AssetType2 AssetType3 AssetType4 [...]

Threat2: AssetType1 AssetType2 AssetType3 AssetType4 [...]

Threat3: AssetType1 AssetType2 AssetType3 AssetType4 [...]

Threat4: AssetType1 AssetType2 AssetType3 AssetType4 [...]

[...]

Each threat corresponds to different types of assets, both of which are dynamic and fetched from a GraphQL backend.

Every row consists of a v-chip-group that manages the v-chips (Asset types) in that row, and the entire matrix is dynamically constructed using a couple of v-fors:

<v-row  
  v-for="(threat, index) in threats"
  :key="threat.short">
      <v-col>
        <v-card">
          {{threat.id}}.- {{threat.name}}
        </v-card>
      </v-col>
      <v-col>
          <v-chip-group 
            :value="associations[index]"
            multiple
          >
          <v-chip 
            v-for="(type, index2) in assettypes" 
            :key="type.id"
            @click="getClick(index, index2)"
          >
          </v-chip>
      </v-chip-group>
      </v-col>
</v-row>

[...]

// This function gets the row and the column clicked
getClick(row, column){
    //this.$refs.$emit("input")

    var position = this.associations[row].indexOf(column)
    if (position > -1){
      this.associations[row][position] = -1
    } else {
      this.associations[row][this.associations[row].length] = column
    }
  },

Encountering several challenges with this setup. While the matrix isn't overly large, it's sufficiently sizeable to avoid frequent rendering (and a similar, larger matrix may be needed later on, perhaps 150 x 100). The data is stored in a multidimensional array named "associations," initialized at the start with push().

Initially, all v-chips were rendered upon any click due to v-model. I suspected the lack of unique v-chip keys as the possible cause and switched to v-bind along with a custom event handler for @click.

Further, utilizing splice() and push() resulted in re-rendering issues, prompting me to revert to direct assignments (array[] = something) to reduce re-renders to only the clicked v-chip.

While I may be overcomplicating matters, the present resolution seems effective, but another problem has surfaced. The first click on each row appears to "disappear." In essence, the V-chip fails to emit the input() event and update only during the initial click. Subsequent clicks on the same row work smoothly. Additionally, this initial click triggers a re-render of all table elements, though the reason remains obscure.

An interesting discovery followed my attempt to troubleshoot by adding the commented line "this.$refs.$emit("input")" at the beginning of the click handler. Despite receiving a Vue warning "Error in v-on handler: TypeError: this.$refs.$emit is not a function," strangely, the solution worked: the input event was emitted by v-chip, preventing page rendering with the first click. The logic behind this workaround eludes me.

I suspect a correlation between the initial re-rendering and non-emission of the input event, yet the root cause eludes detection. Suggestions or insights would be greatly welcomed.

Answer №1

A few points to consider:

It is possible that the issue you are experiencing is due to your :key not being unique.

Instead of using :key="threat.short", try using

:key="`threat-short-${index}`"

If there are any duplicate key warnings, be sure to check your console logs.

The problem may also be related to how Vue handles reactivity for arrays when using

:value="associations[index]"
.

Vue may not react properly to the first click when dealing with an array in this way.

You could try wrapping it in a function like this:

getRow(index){
   return this.associations[index];
}

Alternatively, you can test creating a computed property for just one row to see if that resolves the issue. If it does, then this might be the root of the problem.

If possible, could you provide an online demo demonstrating the bug? This would help me assist you more effectively.

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

What is the best way to extract the modal into its own file?

Greetings, fellow React developer who is new to the field! I have successfully implemented a React Class Component that includes a popup Modal. This Modal displays when a user clicks on an Item Card: import React from 'react'; import { Card, Bu ...

Explore the wonders of our solar system using three.js!

I am embarking on my first project using three.js to create a miniature solar system consisting of 1 star, 2 planets, and 1 moon orbiting each planet. As a beginner to both three.js and JavaScript in general, I am eager to learn. Currently, I have success ...

Guide on determining if the value in a JSON object is a string or an array in Node.js

Running a Node.js application, I encountered the following JSON array structure. First JSON object: var json1= { bookmarkname: 'My Health Circles', bookmarkurl: 'http://localhost:3000/', bookmark_system_category: [ '22&apos ...

What is the best way to toggle a specific div element within an ng-repeat loop?

I have a list of products. When I click on a product, it should be added to the database. If I click on the same product again, it should be removed from the database. I am toggling the wishlistFlag using ng-click. This works correctly for one div element, ...

Exchange information among scripts in the background environment, such as the background script, browser action, page action, options page, and more

I am facing a challenge with transmitting data from my background script to the script for my pageAction. The content script I have adds an <iframe />, and the JavaScript within the <iframe /> successfully receives the data from the background ...

What is the most efficient way to iterate through an array to push properties into an object nested within another array?

I have been working on a small Angular application that functions as a scheduler, allowing users to input a Name, Start and End dates, and toggle a boolean checkbox through a form. One challenge I am facing is trying to assign the names entered by each use ...

Jquery Plugin for Click Selection Activation

After receiving no response to my previous inquiry, I decided to proceed with this particular plugin setup. (function ( $ ) { $.fn.myPlugin = function (options) { options = $.extend( {}, $.fn.myPlugin.defaults, options ); return this.each(function (i ...

I really want to execute a loop and add it to a list, but when I try to include another loop using template literals inside the addition function, I encounter an error

Could you please review my code and provide some solutions for me? I am trying to run a loop and append it, but I want to run another loop inside the append using template literals which is causing an error: Uncaught SyntaxError: Missing } in template expr ...

Utilizing html5 mode in AngularJS alongside an external NodeJS server

Despite reading numerous SO answers and questions on this topic, I still find myself with lingering uncertainties. To outline the issue: I am utilizing an AngularJS app with html5 enabled to eliminate the '#' sign. $locationProvider.html5Mode( ...

What is causing the error that app.js file cannot be located?

Here is the layout of my directory: ReactCourse // Main folder public // Subfolder within ReactCourse index.html // HTML file with linked js file app.js // JavaScript file This is the content of index.html: <!DOCTYPE ...

Step-by-step guide: Mocking an image using a fixture in Cypress

I'm currently utilizing cypress to run tests on my VueJS application. One issue I am facing involves simulating the display of an image on a page. Specifically, I am trying to load a user profile using the following code snippet: describe('Test ...

Adjust the header as the user scrolls

Having a little issue with my header. I'm new to Bootstrap and would like to change my header when scrolling down. Everything works perfectly, but when I reload the page, the header remains in the "scrolling state". Apologies for any language mistakes ...

What is the functionality of the arguments in the ajax function?

I'm currently following a tutorial that includes the code below: https://i.sstatic.net/a68AO.png However, I'm confused about the purpose of url, cache, and success. Are they arrays? How do they function? ...

Anticipating the conclusion of a pending GET request

Currently, I am developing a system to create user accounts. There is a function in place that checks for the existence of a username and returns false if it doesn't exist. However, when another function calls this username checker, it automatically ...

grab the content from a text editor and insert it into a div element

Currently, I am trying to extract text from my content editable div and place it in another div (similar to the one seen on stack overflow). However, I am encountering a frustrating issue. 1. My content div seems to be lagging behind. When I type in my ed ...

Choose children input textboxes based on the parent class during the onFocus and onBlur events

How can I dynamically add and remove the "invalid-class" in my iti class div based on focus events in an input textbox, utilizing jQuery? <div class="form-group col-md-6"> <div class="d-flex position-relative"> & ...

Running a bash command within a Node.js application while having root privileges

There is a slight issue that I'm encountering. I have a nodejs app that needs to execute a command in the OS's bash with root privileges, for example: The command is: echo "$password" | /usr/bin/sudo /usr/bin/abc --key "$username" Below is my c ...

I'm encountering an issue where my information doesn't seem to be getting through to the Django

For some reason, the data I am trying to post to /api/recipe/recipes/ is not showing up in my HTML {% extends 'base.html' %} {% block content %} <!DOCTYPE html> <html> <head> <script src="h ...

You can use the following code to retrieve the value of the checked radio button with the name "nameOfradio", but make sure that the radio button

I am attempting to extract the value from a radio button using the following code: document.querySelector('input[name=nameOfradio]:checked').value; However, I would also like to retrieve a value even if none of the radio buttons are checked. Fo ...

Enhance your camera control experience with seamless transitions on Three.js

I've been developing a website that allows users to explore celestial objects in the sky using VRControls and focus on their favorites with Trackball controls, which are activated by clicking on the object. Check out a demo of the project here: https: ...