Setting a default value for a pair of buttons in VueJS depending on the API response is a straightforward process

One of the challenges I am facing involves toggling a pair of buttons based on the response received from an API. These buttons need to be set only once when the page initially loads.

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

Below is the code snippet I am currently utilizing:

<div class="small-7 column btn-switch">
          <span class="switch-statement">"Is it mandatory"</span>
          <button
            :class="!isMandatory ? 'button--secondary' : 'button--tertiary'"
            class="button button-border"
            @click="setMandatory(false)"
          >
           "No"
          </button>
          <button
            :class="isMandatory ? 'button--secondary' : 'button--tertiary'"
            class="button"
            role=`your text`"button"
            @click="setisMandatory(true)"
          >
            "Yes"
          </button>
        </div>

I attempted to use v-if to control the visibility of the buttons but unfortunately it did not yield the desired result. Are there any alternative methods that can be suggested for achieving this functionality?

Answer №1

It is possible that the variable isMandatory is dynamic and changes based on the response from the API call. Initially, when the page loads, it will have the default value assigned to it. However, when the API responds at a later point, the page does not refresh.

To provide a more accurate solution, I would need more insight into your code structure. Based on the information provided, it seems like converting isMandatory into a ref could be a potential solution.

For instance, if your code resembles the following:

<script setup>
  isMandatory = false;

  // -> handling of API call
  isMandatory = apiResponse
</script>
<template>
  ...
</template>

You could transform it to:

<script setup>
  isMandatory = ref(false);

  // -> handling of API call
  isMandatory.value = apiResponse
</script>
<template>
  ...
</template>

By doing so, the template will automatically update whenever there is a change in the value of isMandatory.

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

When I upgraded my "react-router-dom" from version "5.2.0" to "6.14.0", I encountered a warning stating "No routes matched location"

After updating react-router-dom from version "5.2.0" to "6.14.0", I encountered a warning saying "No routes matched location." Initially, I received an error stating that "<Route> is only meant to be used as a child of <Routes>, not rendered d ...

Looking for the function to activate when the enter key is pressed

I have a search box which is a text type and has a click button that initiates the Find() function. How can I enable searching by pressing Enter inside the textbox? ...

Changing the height of tablerows in Material UI v5: A step-by-step guide

I am attempting to adjust the height of the rows in this material-ui code example. import * as React from "react"; import { styled } from "@mui/material/styles"; import Table from "@mui/material/Table"; import ...

Issue encountered in Ionic/React/Typescript - Incorrect props supplied to React.FC<'erroneous props provided here'>

Having struggled with this issue for a while now without any success, I have searched through numerous questions here but none seem to address my specific case. Therefore, I kindly request your assistance. I am encountering difficulties passing props thro ...

Using JavaScript to Redirect to Homepage upon successful Ajax response on local server

I need assistance with redirecting to the Homepage from the SignIn Page once the user credentials have been validated. The response is working correctly, and upon receiving a successful response, I want to navigate to the Homepage. My setup involves Javasc ...

``Is there a more SEO-friendly option instead of using an iframe?

I am looking for a solution to easily share my content with other websites without the issues I currently face. Presently, I use an iframe which poses two problems: <iframe width=“540”; height=“700” frameborder=“0” src=“http://www.energi ...

Why doesn't ngSubmit function inside a modal?

I am experiencing an issue where my submit button is not activating the ng-click angular directive, and I cannot seem to identify the cause. Most people who faced a similar problem did not have their submit button placed inside their form, but I am confi ...

What is the best way to convert a Nextjs page into a PDF file?

I am currently working on a page structure that looks like this: export default function FichaTecnica(props) { const data = props.data; return ( <> <Container> <TopData info={data} /> <DataSheet datasheet= ...

Having difficulty entering text into the Material UI TextField

I am encountering an issue with a button that opens up a Material UI Dialog containing a TextField. However, I am unable to click into the TextField to input any text. Additionally, when clicking on the button to open the Dialog, I receive the error messag ...

The attempt to run 'readAsBinaryString' on 'FileReader' was unsuccessful. The first parameter is not the expected type 'Blob'

I am currently working on parsing an xls file. You can find the file by clicking on the following link: However, I am encountering an error that says: 'Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of ...

"Using JavaScript to toggle a radio button and display specific form fields according to the selected

Currently, I am attempting to show specific fields based on the selected radio button, and it seems like I am close to the solution. However, despite my efforts, the functionality is not working as expected and no errors are being displayed. I have define ...

Selecting Content Dynamically with jQuery

I have a webpage with multiple dynamic content sections. <div id="content-1"> <div id="subcontent-1"></div> <i id="delete-1"></i> </div> . . . <div id="content-10"> <div id="subcontent-10"></d ...

Getting the correct JSON field value from a JavaScript Promise object in the right way

Trying to fetch data in JSON format from a URL, I encountered an issue where the value of responseData.title differed between two calls made within the second .then() function below: var getTitleForId = async function(id) { if (!id) return fal ...

Display the current position of the caret in a field that cannot be edited

Can a virtual caret be displayed between two letter boundaries in HTML/CSS/JavaScript, for example in a regular div without using contenteditable=true? Imagine having the following: <div>Hello world</div> If I were to click between the "w" a ...

Unable to input text by clicking using FabricJS combined with React and Jquery

I am currently working on a project using react and FabricJs. My goal is to add text to the fabricJS canvas by clicking on a button using jQuery. This is the code I have so far: import React, {Component} from 'react' import {fabric} from ' ...

Guide to eliminating the clearable icon in the Vuetify file input

I am struggling to find any props to deactivate the clearable icon. Additionally, there doesn't seem to be an event related to this issue. Is there a method available to eliminate the clearable icon? <v-file-input ref="profilePictureFileInput" ...

How to Call a Nested Object in JavaScript Dynamically?

var myObj = { bar_foo : "test", bar : { foo : "hi there"; }, foo : { bar : { foo: "and here we go!" } } } How can we achieve the following: var arr = [["bar", "foo"], ...

What is the easiest method to transfer a variable from Express to Vue.js?

Hey there! I'm currently diving into Vue.js and finding it quite challenging compared to using EJS. I'm struggling with passing res.locals.something from my Express server to my Vue frontend. Specifically, I am working with Passport.js for user a ...

Can CSS be used to separate elements within a div from the overall page styles, similar to how an iFrame functions?

Is there a way to isolate elements within a div, similar to how it would behave in an iFrame? I am facing issues with the global SharePoint styles affecting my app inside SharePoint. I want to completely disable these global styles so that my app only use ...

Learn how to successfully upload an image using React and Node without having to rely on

I am currently exploring file uploading in react/node. Instead of passing files directly into the API request, I am passing them within the body of the request. Here is a snippet of my react code: import React, { Component } from 'react'; import ...