Retrieving data from an array in VueJS

As a newcomer to Vue, I'm facing some difficulties in extracting a single value from an array. My Axios get request returns an array of users with fields such as name, display_name, role, and more. I have successfully retrieved all these values and displayed them in my table component:

<template>
<div class="container">
    <table class="mt-3 table roboto table-stripped table-hover table-sm">
        <thead class="thead-light">
            <th>Name</th>
            <th>Username</th>
            <th>Profile</th>
            <th class="text-center">Is manager</th>
        </thead>
        <tbody v-for="user in users">
            <td v-text="user.name"></td>
            <td v-text="user.username"></td>
            <td v-text="user.display_name"></td>
            <td class="text-center">
                <button 
                    class="letter-shadow btn-sm font-500 grey-darkest roboto letter-spacing"
                    :class="showRole">Activate
                </button>
            </td>
        </tbody>
    </table>
</div>
</template>

<script>
export default {

    data() {
        return {
            users: [],
        }
    },

    computed: {
        showRole() {
            // Need to change button color based on user's display_name
        }
    },

    mounted() {
        axios.get('/api/users').then(response => this.users = response.data);
    },

}
</script>

I am trying to dynamically change the button color based on the value of user.display_name. If the user is a "Manager", I want to apply the btn-danger class. However, I am unable to access user.display_name in the showRole method as it returns undefined. Any assistance would be greatly appreciated.

Answer №1

Perhaps it would be more effective to utilize a method instead, since computed properties do not support parameter passing.

Here is an example:

...
methods : {
    showRole(user){
        //code that determines the button class based on user
    }
}
...

<button :class="showRole(user)">Activate</button>

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

The art of integrating partial rendering into a template

I'm currently working on a project using Angular 2 and I need to display a partial inside a template without having to create a new component. Is this doable? import {Component} from 'angular2/core'; import {RouteConfig, ROUTER_DIRECTIVES} ...

Creating a fixed-size set in React with randomly ordered elements

I am currently working on a project where I need to retrieve a random array from a .json file using React. My goal is to create 16 cards, each displaying a number that appears twice. To ensure uniqueness, I am utilizing a Set, but I am facing an issue with ...

Parsing JSON data returned from ASP.NET in JavaScript is proving to be problematic

After successfully converting a DataSet to JSON using a jQuery AJAX call, everything seems fine. The response I get back is as follows: {"Table":[[2,"BlackBerry Tour","2013-09-10","2013-09-13","Project"],null]} The JSON response looks valid and even pass ...

What is the best way to anticipate the vue.js created hook?

I have a vue.js application where I am facing an issue with awaiting a call to the Prismic API. The call is initiated from the created hook, but I am unsure of how to make Vue await the hook itself. Here is an example of the code: ... async created() { ...

What is the best way to assign JSON data to a Class variable within Angular?

In my code, I have a class called Projects export class Projects { project_id: number; project_name: string; category_id: number; project_type: string; start_date: Date; completion_date: Date; working_status: string; project_info: string; area: string; add ...

The alert box for model validation summary errors is deactivated when hidden using .hide() method

In my MVC web application form, there is an optional postcode finder. If there is no match for the entered postcode, I add a custom error message to the .validation-summary-errors section. After the error is fixed, I remove all instances of the postcode cl ...

What is the best way to determine the amount of distinct elements in an array of objects based on a specific object property?

I am working with an array called orders. orders = [ {table_id: 3, food_id: 5}, {table_id: 4, food_id: 2}, {table_id: 1, food_id: 6}, {table_id: 3, food_id: 4}, {table_id: 4, food_id: 6}, ]; I am looking to create a function that can calculate ...

Move a pin on Mapbox

I have a question regarding my map markers: https://i.sstatic.net/2HndV.png Currently, the center of the blue markers align with my desired coordinates. Is there a way to adjust the markers slightly upwards so that the tip sits exactly on my specified po ...

Prevent the reloading of the page by utilizing Ajax technology when submitting a form in Laravel

I'm facing a challenge with processing a form submit using ajax instead of Laravel to prevent page reloads. Unfortunately, it's not working as expected and I'm struggling to figure out the issue. Despite researching numerous examples online, ...

Setting up Okta authentication with the Composition API in a Vue application

Currently, I am in the process of setting up a Vue authentication application with Okta. The foundation of this project is based on the code repository found at https://github.com/okta/okta-vue. My main objective is to migrate the routing and sign-in redir ...

Interacting with APIs using jQuery, managing responses with AJAX, handling parsererror in our development environment XAMPP on Windows 8

I've come across numerous questions regarding the dreaded "parsererror" but I just can't seem to find a solution that fits my specific issue. Here's the code causing me grief: <!DOCTYPE html> <html> <head> <meta http-eq ...

Deleting a row from a table in Angular when a form is submitted

I am new to using Angular and I have been attempting to remove certain elements from a table upon submission. <tr ng-repeat="val in values "> <td ng-bind="$index"></td> <td ng-bind="val.rec">ED1500322</td> <td& ...

Stop the slider when a video pops up

Years ago, I had a slider created for me that supports both images and video with a timer. However, the issue I'm facing is that when the timer runs every 10 seconds, the video gets cut off if it's not finished playing. Below is the json file st ...

The location of errors is not displayed in VueJS stack traces

My Current VueJS Setup Check out the Source Code here I am working on a project using VueJS with webpack. I have chosen not to use the vue-loader plugin or .vue files. My project structure resembles a typical Javascript webpack project where I import vu ...

Loading tabs dynamically in Angular 2 based on header click event

I successfully created a custom tab component using Angular 2, and it is functioning well with the following usage: <us-tab-verticle> <vtab-content [tabTitle]="'Basic Information'"><basic-info> </basic-info></vtab- ...

Discover the hidden truth: Unveiling the enigma of React

I'm currently learning React and I've been working on some apps to enhance my skills and deepen my understanding. Right now, I am facing a challenge where I need to incorporate the logged user information into the Redux state. However, whenever I ...

Retrieve dynamically generated v-select elements' selected options in Vue Vuetify

In my Vue project, I am utilizing Vuetify to populate a list of objects retrieved from an API. The JSON data structure looks like this: users = [ { "id": "1234", "name": "John Doe", "description": "Male, 25 years old", " ...

Step-by-step guide on installing both Angular and Nodejs within a single folder

I'm diving into the world of NodeJs and Angular, and I recently created a simple NodeJS application following instructions from this link. However, I encountered an issue when trying to install Angular alongside NodeJS. After running ng new angular-cr ...

Exploring the integration of Vuetify within a Rails project using Capybara Selenium testing

This website is a frequent visit for me, but I've never found myself in a situation where I needed to ask a question. However, I am currently facing a block and it seems like the perfect time to ask my first question. My query pertains to testing a s ...

How can I make an AJAX request in Rails 3 to retrieve an HTML page?

I am experimenting with using Rails to display an .html.erb template via an ajax request under specific conditions. By default, I am consistently receiving the .js.erb file when making an ajax request. Without delving into the reasons behind this choice, ...