If a user cancels, the radio button in Vue 3 will revert back to

I'm encountering a problem with radio buttons in vue 3.

When passing an object from the parent component to the child for data display, I want to set one version as default:

<template>
    <table>
        ...
        <tr v-for="(vf, index) in version.files" :key="vf.id">
         ...
         <td>
            <input type="radio" name="defaultVersion" v-model="vf.default" :checked="vf.default" @change="onChangeDefault(index)" @click="onClickDefault">
         </td>
        </tr>
    </table>
</template>

props: {
    version: Object // received prop object
},

setup(props) {
    const {version} = toRefs(props) // making version reactive
    
    let defaultVersionIndex = 0
    
    const onClickDefault = () => {
        for(let i = 0; i < version.value.files.length; i++) {
            if(version.value.files[i].default) {
                defaultVersionIndex = i
            }
        }
    }
    
    const onChangeDefault = (index) => {
        let text = "Change default?\nEither OK or Cancel.";
        if (confirm(text) == true) {
          // make API call upon user confirmation
        } else {
          version.value.files[index].default = false 
          version.value.files[defaultVersionIndex].default = true 
        }
    }
    
    return {
        version,
        onClickDefault,
        onChangeDefault
    }
}

The issue arises when clicking on a radio button - visually it changes but not reflecting in version.files. What am I missing?

Using radio buttons because only one can be default at a time.

UPDATE 1

Changed input by binding the value to ensure boolean values:

<input type="radio" :value="vf.default" name="defaultVersion" v-model="vf.default" :checked="vf.default" @change="onChangeDefault(index)" @click="onClickDefault">

On cancel action:

version.value.files[index].default = !version.value.files[index].default
version.value.files[defaultVersionIndex].default = !version.value.files[defaultVersionIndex].default

Although values in the object version.files change, the visual update is not working correctly...

Answer №1

Issue Resolved.

Upon receiving input:

<input type="radio" :value="vf.id" name="defaultVersion" v-model="defaultValue" :checked="vf.id === defaultValue" @change="onChangeDefault(vf.id)" @click="onClickDefault">

A virtual model was created to store the default value's ID.

const defaultValue = ref(0)

// Storing the previous default value (ID)
let previousDefault = 0
const onClickDefault = () => {
 previousDefault = defaultValue.value // Working like magic :)
}

If the user decides to cancel:

defaultValue.value = previousDefault

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

Utilizing NextJS Image Component in Conjunction with Auth0

I have been working on integrating auth0 with nextJS and encountered an issue with the next.js Image component. Let's review the code snippet: import Image from "next/image" import { useUser } from "@auth0/nextjs-auth0" export def ...

The UseEffect hook continues to run even if the dependency (router.query) remains the same

useEffect(() => { console.log('applying filter'); const updatedFilters = { status: { values: { label: router.query.status, value: router.query.status }, }, // Add additional filter properties here... }; ...

"Simultaneously updating the UpdatePanel, triggering a JavaScript postback, and modifying the querystring in a SharePoint Search

Struggling with a tricky issue here. Let me try to clarify: I have a SharePoint results page where I'm using a Search Results Core WebPart. Now, I want to modify the parameter in the querystring upon postback so that the WebPart displays different re ...

What is the best way to disable the click function for <a> tags that have a specific class?

I am dealing with parent navigation items that have children, and I want to prevent the parent items from being clickable. Here is an example of how they currently look: <a href="parent">Parent Item</a> Is there a way to select the <a> ...

Using AngularJS to show/hide elements within a colgroup tag

Looking to create a dynamic table allowing users to hide certain columns. Wondering if it's possible to use ng-show with colgroup or col tags instead of adding ngshow to each cell individually. Struggling to make it work... <colgroup ng-repeat="mt ...

The art of creating an asynchronous function: A comprehensive guide

My goal is to download files from a Firebase bucket and then store them in a database. I need the download process to be asynchronous, ensuring that each file is fully downloaded and added to an array before moving on to the next one. However, my current ...

Validating data on the server side using Vuelidate

I am currently exploring the integration of both frontend and backend validation techniques. For instance, in a registration form where a user inputs an email address, there is an email validator applied on the frontend and a unique validator enforced on ...

Troubleshooting a perplexing problem with an unresponsive AngularJS directive

Here is the code from my app.js file (main JavaScript file) var currentUserId; window.myApp = angular.module('myApp', ['ajoslin.mobile-navigate', 'ngMobile', 'myApp.Registermdl', 'ngProgress', &apo ...

Creating dynamic content in the Ajax-enabled Smart Admin theme: A step-by-step guide

As I work on developing an application for a client, my focus is on creating a web service using Laravel5 for the backend. To enhance the user interface of this web service, I have chosen to incorporate the Smart Admin Theme, specifically utilizing the Aja ...

Guidelines on populating a Vue array with data fetched from an Axios request

The v-breadcrumbs component is used to display data from the breadcrumbs array, which works seamlessly with static data. <v-row> <!-- Breadcrumbs --> <v-col class="d-flex"> <v-breadcrumbs :items="breadcrumbs"></v ...

How can we make it simple for users to update webpage content using a file from their computer?

I am developing a custom application specifically for use on Firefox 3.6.3 in our internal network. My goal is to dynamically update the content of the page based on a file stored locally on my computer. What would be the most straightforward approach to ...

Issues with Retrieving Nested Arrays

I am encountering an issue with an object within an array and I am looking to specifically display that as an array. data1 const data1 = [ { "id": "01", "info": "fefef", "sub": "hieei&qu ...

Explore various date formats using the datepicker functionality

I'm dealing with the code below: <script type="text/javascript" language="javascript" src="~/Scripts/bootstrap-datepicker.min.js"></script> <script type="text/javascript" language="javascript" src="~/Scripts/locales/bootst ...

The Lerna command for adding packages fails with an error message stating that the packages are not related

Currently, I am utilizing lerna to manage a multirepo containing interrelated packages. This issue only arose after installing a new operating system. Whenever I attempt to utilize lerna add to add a dependency from one package to another, an error occurs ...

angularjs .reject not executing correctly within the then statement

I'm having trouble identifying the bug in my code. For some reason, $q.defer().reject() isn't functioning correctly. defer.resolve works as expected and even reaches the finally segment, but defer.reject (although it doesn't throw an error) ...

Using CSS to design a table-like structure with rows that span multiple columns

I am trying to generate a table dynamically using CSS and a JSON array. For example: In the code snippet provided, I have assigned a class of 'spannedrow' to span certain cells in the table, although the class is not defined yet. This is just ...

Using jQuery .animate() leading to erratic input movements

I am currently utilizing jQuery's .animate() feature to create a smooth animation effect on the width of a <div> element when a child <input> is in focus. Nevertheless, I'm encountering an issue where the input field jumps up and down ...

The call stack size has reached its maximum limit due to running 2 Collection.find commands

I've encountered a Maximum call stack size exceeded exception in my Chrome console while working with Angular2 and Meteor. This issue started after I added the following code to my client side: var userChannelsId = UserChannels.find({idUser: Meteor.u ...

There was an issue with the vue-server-renderer where the render function or template was not defined in the component

In my project with Vue 2.7 and webpack4 for SSR, I encountered an error message stating: render function or template not defined in component: anonymous. This issue arises under the following circumstances: When using a child component. <template> ...

Organize express controller by breaking it up into multiple separate files

Recently, I've set up an express js controller file as follows The File Path: /controllers/usersController.js // Register User module.exports.register = function(req, res) { ... } // Login User module.exports.login = function(req, res) { ... } // ...