Customize button text in Vue using data variable conditions

There are 3 desks in a table with role IDs 2, 4, and 6. In this scenario, two tables are involved. The goal is to dynamically display a specific role name on a button based on the current user's role ID.

The desired displays are:

  • If the current user's role ID is 2, then show the role name associated with role ID 4
  • If the current user's role ID is 4, then display the role name linked to role ID 6

User Table https://i.stack.imgur.com/XLLyA.png

Registration Table https://i.stack.imgur.com/pVG7N.png

Code Snippet

<span v-if="user.user_role_id ==results.desk_user_role_id">
    <v-btn small color="primary"  v-on:click="getNextDesk" style="width:400px;">Forward to </v-btn>
    <v-btn small color="primary" v-on:click="getPreviousDesk" style="width:400px;">Revert </v-btn>
</span>

Script Implementation

getNextDesk(currentdeskid) {
  if (currentdeskid === 2) {
    return 'Technical Desk';
  }
  if (currentdeskid === 4) {
    return 'Executive Desk';
  }
  return '';
},
getPreviousDesk(currentdeskid) {
  if (currentdeskid === 6) {
    return 'Technical Desk';
  }
  if (currentdeskid === 4) {
    return 'Registration Desk';
  }
  return '';
},

Answer №1

I have a suggestion on how you could approach this situation:

new Vue({
  el: "#app",
  data() {
    return {
      user: { user_role_id: 2 },
      roles: {
        2: { name: 'Registration', next: 4 },
        4: { name: 'Technical', next: 6, previous: 2 },
        6: { name: 'Executive', previous: 4 }
      }
    }
  },
  computed: {
    forwardTo() {
      const { next } = this.roles[this.user.user_role_id];
      return next ? 'Navigate to ' + this.roles[next].name : false;
    },
    revertTo() {
      const { previous } = this.roles[this.user.user_role_id];
      return previous ? 'Return to ' + this.roles[previous].name : false;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div>
  User:
  <select v-model="user.user_role_id">
    <option v-for="(role, id) in roles" :value="id">{{ role.name }}</option>
  </select>
  </div><br />
  
  <button v-show="forwardTo">{{ forwardTo }}</button>
  <button v-show="revertTo">{{ revertTo }}</button>
</div>

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

How can I create clickable table entries using php and html?

I want to create a table on this page that is clickable. When the user clicks on a row, the data from that row will be sent to a PHP file with a form prepopulated with the selected user's information for updating purposes. <!DOCTYPE html> &l ...

Is it possible to modify the HTML/CSS of a child component using the parent component in Angular 2+?

Is there a way to dynamically add text and style to a specific row in a child component based on the parent's logic? (parent): <body> <child-component></child-component> </body> (child): <div *ngfor = "let record in r ...

Steps to retrieve the central coordinates of the displayed region on Google Maps with the Google Maps JavaScript API v3

Is there a way to retrieve the coordinates for the center of the current area being viewed on Google Maps using JavaScript and the Google Maps JavaScript API v3? Any help would be greatly appreciated. Thank you! ...

Is there a way to hide a paragraph or div using javascript?

I am experimenting with using buttons to hide paragraphs using javascript, but even when I set them as "hidden", there is still excess blank space left behind. Is there a way I can eliminate that extra space? Below is the javascript code: function backgro ...

I possess an array of objects that includes both images and documents. My goal is to examine the mime_type of each object and select the first element in React to be displayed within an <img> tag

I have an array of objects that contain images and documents. My goal is to display the first image only if the mime_type is 'image/jpeg' or 'image/png'. I am working with React. Despite my attempts, I keep encountering undefined resul ...

Find out which way the user is scrolling in your React js application

I'm struggling to determine whether the scroll event is moving up or down, and I haven't been able to find a solution yet. import React, { useState, useEffect } from "react"; import { Link } from "react-router-dom"; const Nav ...

Issue with Pagination functionality when using Material-UI component is causing unexpected behavior

My database retrieves data based on the page number and rows per page criteria: const { data: { customerData: recent = null } = {} } = useQuery< .... //removed to de-clutter >(CD_QUERY, { variables: { input: { page: page, perPag ...

Is there a way to retrieve command line arguments from a running Electron Application?

I am currently facing a challenge with retrieving command line arguments from an Electron application. When the application is launched with command line arguments, I need to utilize these arguments in the renderer process (specifically within a webview). ...

Interacting with a Web API using JavaScript following successful authentication with Azure AD B2C

Currently, I am working on a .Net Web App. After going through the authentication process with Azure AD B2C using the Azure AD Connect protocol, my app's controller successfully obtains an access token via the MSAL library (written in C# code) to conn ...

Having difficulties accessing the git repository through the application

I am currently working on a Node.js application that needs to connect to a Git repository via the app. The connection works fine locally, and it also runs smoothly when I docker build and run it within a container on my local machine. However, upon deplo ...

Background loading of child application in single-spa

I'm currently working on setting up a Single-Spa micro frontend with Dynamic Module Loading in React. The user journey I have in mind is as follows: Root Application -> Authentication Application -> User Enters Details -> API Call -> Redir ...

definition of a function with another function

I'm curious about whether it's considered a good practice in JavaScript to define a function within another function. Take a look at this code snippet: module.exports = function() { function foo() { // do something } ... foo() .. ...

Modify text using JQuery when the span is clicked

Currently, I am attempting to retrieve a value from the database: SenderDriver->total_trips. Everything seems fine, but I have a specific id that needs to be placed within onClick(), which then sets the value of the database variable: SenderDriver-> ...

Should the ListItem expand and collapse when clicked?

My react/material-ui component has the following structure: export const TodoItem: React.FC<Props> = ( {todo, itemKey}) => { const [dialogState, setDialogState] = React.useState<boolean>(false); const handleClose = () => { setDial ...

Is there a way to track the number of visits by a 'user' to a website?

Looking to hide certain parts of my website from users who have visited more than a specified number of times. The NY Times site has something similar. Utilizing react & firebase for this project. Considered using IP addresses, but it seems to identify l ...

The issue arises when attempting to utilize ExpressJS middleware in conjunction with NextJS Link feature

Incorporating Next with Express routes, I have set up a scenario where /a should only be accessible to authorized individuals, while /b is open to the public. ... other imports... const app = next({ isDev }) const handle = app.getRequestHandler() async f ...

Updating with Setstate

Refreshing the page with Setstate, registering twice and doubling the count of both heads and tails counter every time on click instead of adding just +1 class CoinFlip extends Component { constructor(props) { super(props); ...

Complete the dynamic form submission in a non-blocking manner

My goal is to dynamically add text fields with the click of a button I also aim to extract data from these text fields and insert it into the database Currently, all functions work smoothly, I just need this additional feature. Per ...

Express server unable to process Fetch POST request body

I'm currently developing a React app and I've configured a basic Express API to store user details in the database app.post("/register", jsonParser, (req, res) => { console.log("body is ", req.body); let { usern ...

What is the best way to integrate AJAX with draggable columns in a Laravel application?

I have successfully integrated draggable functionality for table columns using jQuery Sortable within my Laravel application. I am now facing the challenge of updating the database with the data from these columns through AJAX. Despite researching online ...