Resetting a radio button inside a Vue component

I have implemented a Vue component for creating a dropdown select input. Below is the code for the component:

export default {
    name:"dropdown",
    template:`
    <div class="select">
        <ul>
            <li><label><input type="radio" v-model="val" :value="init" disabled checked><span>{{placeholder}}</span></label></li>
            <li v-for="item in list"><label><input type="radio" v-model="val" :value="item.value" /><span>{{item.name}}</span></label></li>
        </ul>
    </div>`,
    props: {
        list: { type:Array, required:true },
        placeholder: { type:String, default:"Select" },
        value: { required:true }
    },
    data:function() {
        return {
            val:this.value
        }
    },
    computed:{
        init:function() {
            return this.list[0].value instanceof Object ? null : '';
        }
    },
    watch:{
        val:function(val) {
            this.$emit('input', val);
        }
    }
};

Afterward, I used it on a page like this:

<dropdown :list="RESOURCES" v-model="add.resource" placeholder="Select resource" class="input-x2"></dropdown>

The issue I am facing is that when I reset the value to null, the dropdown component does not revert to the first (default) option as expected.

It was functioning correctly before I placed it into a component, leading me to believe there may be an issue with prop propagation.

Answer №1

Be sure to keep an eye on any changes in the props as they are not automatically reactive...

watch:{
    val:function(val) {
        this.$emit('input', val);
    },
    list:function(list) {
        //Insert your custom code here!
    }
}

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

Running JavaScript within Objective-C in a Cordova plugin

I'm working with cordova version 6.5.0 and I'm trying to develop a plugin that can execute some javascript code. I've read on various forums that I can use stringByEvaluatingJavascriptFromString method in my webview, but it seems like it&ap ...

JavaScript Object Retrieval: [object Object]

I am currently facing an issue with my node/express app where I keep running into a problem with [object Object]. When rendering a page, I use the following code: app.get('/members', checkAuthentication, function(req, res){ res.render('m ...

What causes the slow loading of a div from an external URL?

I have created two buttons that open different PHP pages. When I click on one button, it loads the respective URL into a div. However, when switching to the other button, there is a noticeable delay in loading the new page. The transition between buttons ...

Incorporate the Vue-color Third-Party Component into your personalized Vue.js Component

Recently delving into Vue.js, I've embarked on creating a straightforward TODO application. However, I wanted the functionality to customize task colors within the input form. After researching, I stumbled upon a third-party library called vue-color. ...

The React Router path is inaccurately matching to a different route

I'm currently utilizing React version 4.2 for my application, but it appears to be having trouble matching the correct paths for the routes. <Provider store={store}> <BrowserRouter> <div> <Switch> ...

Mastering JavaScript without the use of the `new` keyword: Unleashing the Power of the Good Parts

In his book, "JavaScript: The Good Parts," Crockford emphasizes the importance of giving constructor functions names with an initial capital letter (e.g., Point) and using function names with initial capital letters only with constructor functions, while e ...

Show the text based on the current count of the counter

Currently, I have a simple Javascript counter function: <body> <script type="text/javascript"> var clicks = 0; function onClick() { clicks += 1; document.getElementById("clicks").innerHTML = clicks; }; </ ...

What is the best way to alter the class of a <div> element with a specific ID?

In the web app, I have a tabbed page that allows users to filter restaurants based on budget and cuisine. The search results are categorized into meals, snacks, and drinks tabs. Each tab is designed as a button with a corresponding window displayed below ...

What is the method for invoking a JS function assigned to a variable from the console?

There is a function structured like this: $(document).ready(function() { var funct = (function(){ var foo = 1; function getPrivateVariable(){ return foo; } return { getFoo: getPrivateVariable ...

enhancing objects by incorporating functions derived from JSON responses

Recently, I received a set of user data from a JSON server response. Below is an example of the data: var users = [ {id: 1, name: 'john', surname: 'smith'}, {id: 2, name: 'john', surname: 'smith'}, {id: ...

VueJS loop creating excessive div element

I am facing an issue where extra div is being created while displaying cards from API data, causing unnecessary space on the page. Here is my response: [ ... { "id": 6, "name": "Highway", ...

Error: The variable $ has not been declared in the context of the function iterateId

I understand that there are many questions on SO related to this error, but my issue is a bit unique. In my code, I am using a forEach loop to retrieve the IDs and text of elements as shown below: function iterateId(id){ $("input[id*='" + id + ...

Unable to upload to Storage due to an error in the AWSS3Provider, which is throwing a TypeError: Cannot read property 'byteLength' of undefined

React Native (Expo) AWS Amplify Auth (Authentication) AWS Amplify Predictions (Recognition) Encountering the error message Cannot read property 'byteLength' of undefined when trying to upload media files to an S3 bucket using AWS Amplify Storage ...

What is the best way to retrieve specific values from an ng-bootstrap modal?

I am working on a website that includes an ng-bootstrap modal. This modal contains two <select> elements with dynamic items, allowing the user to click on two buttons. One button simply closes the modal and sets the value of the Reactive Form to new ...

Guide to organizing a one-to-one object map within Angular JS ng-repeat

Is there a way to organize a one-to-one object map in angular.js using filters (or any other technique) while working within an ng-repeat loop? This is what I currently have: obj:{ a:3, c:5, d:1, g:15 } Basically, I want to achieve s ...

The issue with CORS is preventing the AJAX POST request from reaching the Express.js server, even with body-parser configured

Recently, I have been trying to send an AJAX POST request from my HTML page that is hosted on an Apache server. The goal of this request is to post a filename to an express.js server in order to perform some operations with another node. To facilitate this ...

Is it possible to swap squares for circles in a Three.js PointCloud?

Currently, I am loading a .ply file using PLYLoader in three.js and displaying it as a PointCloud. Here is the code snippet: var loader = new THREE.PLYLoader(); loader.addEventListener('load', function (event) { var geometry = event.content; ...

Attempting to use the getCurrentUrl() function to retrieve the current URL and subsequently opening it in a new tab

I am attempting to retrieve a URL and open it in 3 new tabs, but encountering errors in the process: browser.getCurrentUrl().then(url => { browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('t').perform(); browser.sleep ...

Necessary to modify the camera's rotation axis within AFRAME

I have incorporated AFRAME into my metaverse application and am attempting to achieve a Third Person Perspective (TPP) for my avatars. I have implemented a rotate-with-camera component on my model so that the avatar rotates when the camera is rotated. Addi ...

Accessing Parent and Child Values in AngularJS Selections

I am seeking advice from experts on how to achieve the following desired results: Expected workflow chart: https://i.sstatic.net/9ZmmT.png Here is the default view: https://i.sstatic.net/H6xkZ.png Scenario 1: By clicking on the number "1", all items f ...