Is there a way to retrieve the chosen value of an element within an array when it is clicked on?

I have an array of objects displayed in a table, and I'm trying to retrieve the value of the object that was clicked. However, when I click on one of the numbers, it only returns the value of the last object in the array.

Currently, I am looping through all the objects in the array, but I can't figure out how to only select the one that I click on.

<template>
  <div>
    <tbody>
        <tr v-for="(call, index) in filterSummary" :key="index">
          <td>
            <a
              @click="onSelect"
              class="waves-effect waves-dark green btn-small left"
              id="select_DID_button"
            >{{ call.number }}</a>
          </td>
          <td>{{ call.name }}</td>
          <td>{{ call.dataValues.volume }}</td>
          <td>{{ call.dataValues.inboundVolume }}</td>
          <td>{{ call.dataValues.outboundVolume }}</td>
          <td>{{ call.dataValues.totalDuration | formatDuration }}</td>
          <td>{{ call.dataValues.inboundDuration | formatDuration }}</td>
          <td>{{ call.dataValues.outboundDuration | formatDuration }}</td>
          <td>{{ call.dataValues.averageDuration | formatDuration }}</td>
        </tr>
      </tbody>
  </div>
</template>

<script>
export default {
  data() {
    return {
    }
  },
methods: {
    onSelect() {
      var obj = this.$store.state.callerActivitySummary;
      for (var i = 0; i < obj.length; i++) {
        var newNumber = obj[i].number;
        this.$store.commit("updateNumber", newNumber);
        console.log("Number: ", newNumber);
      }
      this.getCallerActivityDetails();
    }
  }
}
</script>

This is what my array of objects looks like:

{
  "items": [
    {
    "alternativeNames": [],
    "number": "012345678",
    "dataValues": {},
    "name": "Random name"
    }
  ]
}

Here is the output in the console:

Number:  111968948
Number:  49819811
Number:  0566561651
Number:  012345678

I am trying to extract the number from the object that was clicked on.

Answer №1

There seems to be a missing comparison between the selected number and the numbers in each object you are iterating over. Consider adding something like this:

if (selectedNumber === obj[i].number) {
    this.$store.commit("updateNumber", selectedNumber);
    break;
}

In the above code snippet, selectedNumber represents the number being passed to the method.

@click="onSelect(call.number)"

...

methods: {
    onSelect(selectedNumber) { ...

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

Saving HTML form data to a server-side text file

All. I've discovered various scripts that allow you to save form input to your local computer, such as downloading a text document with the submitted information. However, I am trying to create an HTML form that, when the 'submit' button is ...

Changing divs using JavaScript in an ASP.NET MVC application

I am making an AJAX request to retrieve Question1-Question10 from the back-end Below is the code for the request: <script> $(document).ready(function() { question_block(); }); function question_block() { $.ajax({ url: '@Url.Act ...

jquery monitors an element to see if it contains a particular attribute, then takes action accordingly

I'm attempting to conceal a div when an anchor tag (a) has a particular href, and reveal it when the href is different. This is what I've tried: if($('a[href="#intro"]').not(".active")) { $('.navbar-brand').hide();} else ...

Ensuring the next tab is not accessible until all fields in the current tab are filled

I am working on a form with a series of questions displayed one at a time, like a slide show. I need help with preventing the user from moving to the next set of questions if there is an empty field in the current set. Below is the script I am using to nav ...

Exploring the attributes of ExtJS display objects

I am looking for a way to efficiently display JSON content from a servlet in a browser using a list format. While I could use the definition list tag in pure HTML, I need to load everything dynamically without manually parsing and creating the HTML code. ...

Searching for the row value of specific data within a table using Javascript

I have a PHP table populated with values, and I want to make two of the table data cells editable. The table headers include: Task, Due Date, Staff, and Department. The values in the table data are retrieved from a database using Laravel Eloquent. &l ...

Mapping a list of objects from my model to a JavaScript variable for populating fullcalendar events

I have been working on retrieving my fullcalendar events in an object array and then storing it in my view model to pass to JavaScript. However, I encountered an error when trying to store it in the scripts section as eventArray! My service layer code... ...

My date function in Node JS is throwing an error, can someone please help me troubleshoot?

I encountered an error with new date(); while working with node js and express npm plugin. I built a variable date but faced some compilation errors. This is my code .js var update_time = new Date(); update_time.formatDate("y/m/d"); When I run ...

Vite 3: Including a global variable in the configuration results in an additional line break at the end of the string

Vite 3 offers the capability to define environment variables that will replace the key with the corresponding value during project build. The section of the configuration where the version is defined appears as follows: define: { __RELEASE_VERSION__: JS ...

Using Javascript to prevent css from changing the display

I want to make some CSS adjustments to a single element using a single script, but I want all the changes to be displayed at once. For example: $("#someelement").css({width:"100%"}); //this change should not be displayed $("#someelement").width($("#someel ...

Encountered an issue while attempting to transfer data using Marak/faker.js

Does anyone see what I might be doing wrong? I'm having trouble locating the issue in my project, which is built using Meteor and React. Here is the content of my import file: import _ from 'lodash'; import faker from 'faker'; ...

Unable to get the sublocality dropdown list to cascade properly in asp.net mvc

I am dealing with three dropdown lists. The initial action method for the City dropdown is shown below: public ActionResult Create() { List<SelectListItem> li = new List<SelectListItem>(); li.Add(new Sel ...

Displaying JSON data using Vue.js

fetching JSON data save movieData: {} ...... retrieveMovieData (context, parameter) { axios.get(API.movieData + parameter.id) .then(response => { context.commit('MOVIE_DATA', response.data) }) .catch(error => ...

Is it possible to conceal and completely empty the TextBox once the checkbox is deselected?

When the checkbox is checked, the textbox is displayed; otherwise, it remains hidden. However, the value is not being cleared. Can someone please help me with this issue? Thank you in advance. HTML <div class="munna"> <in ...

Assistance required: Click on the button to select and copy all text within the specified paragraph ID

Hey there! I've got a div with a dropdown menu that reveals text and images based on the selected option. What I want to achieve is having a button that allows users to copy all the content inside the div. Below is my sample code for the div dropdown ...

Having trouble getting the Keycloak provider to work with Next-Auth?

It's puzzling to me why my next-auth integration with Keycloak server is failing while the integration with Github is successful. import NextAuth from "next-auth" import KeycloakProvider from "next-auth/providers/keycloak"; import ...

Shadows persist despite light intensity being reduced to 0 during runtime

Struggling to figure out how to get rid of these persistent shadows... During runtime, I attempt: light.intensity = 0.0; This makes the scene darker (which is good), but the shadows created by the light remain visible. I've experimented with both ...

The absence of an index signature in GenericType is causing a missing declaration of the expected key/value type in Flow type

I am encountering difficulties accessing an object property dynamically with a Generic Type. Below is the code snippet: import React, { useState } from 'react' function useForm<FormValues>(initialValues: FormValues) { const [formValue ...

A guide to isolating shared HTML code in a Vue App Component

I have a Vue application that consists of multiple components. Each component contains duplicated code at the top of its template: <section v-if="status || warn" class="messages"> <div class="status">{{status}}</div> <div class="w ...

Suggestion: optimal placement for HTML table data - JavaScript or HTML?

Should I change my Python code to generate a JavaScript file instead of a webpage with a table? I am unsure of the advantages and disadvantages of this approach. Any insights or experiences to share? Are there alternative solutions that I should consider? ...