Create a correct v-model value by utilizing a dot notation string as a reference to the data object

Recently, I created a proxy-component that dynamically renders different components based on the specified :type. Everything was functioning smoothly until I encountered an issue with nested objects within the formData object.

An example of this problem is when dealing with test.test1.

I am looking for a way to make the value of v-model dynamic, depending on the provided string.

Here is a snippet of my Component:

<proxy-component
                v-for="(scheme, index) in personSchema.list"
                :key="index"
                :type="scheme.type"
                :props="scheme.props"
                v-model="formData[personSchema.prefix][scheme.model]"
                v-validate="'required'"
                data-vv-value-path="innerValue"
                :data-vv-name="scheme.model"
                :error-txt="errors.first(scheme.model)"
></proxy-component>

And here is the Data section:

data() {
            return {
                selectOptions,
                formData: {
                    person: {
                        given_names: '',
                        surname: '',
                        sex: '',
                        title: '',
                        date_of_birth: '',
                        place_of_birth: '',
                        nationality: '',
                        country_of_residence: '',
                        acting_as: '',
                        test: {
                            test1: 'test',
                        },
                    },
                },
                personSchema: {
                    prefix: 'person',
                    list: [
                        {
                            model: 'given_names',
                            type: 'custom-input-component',
                            props: {
                                title: 'Name',
                            },
                        },
                        {
                            model: 'surname',
                            type: 'custom-input-componentt',
                            props: {
                                title: 'Surname',
                            },
                        },
                        {
                            model: 'test.test1',
                            type: 'custom-input-component',
                            props: {
                                title: 'test 1',
                            },
                        },
                        {
                            model: 'sex',
                            type: 'custom-select-component',
                            props: {
                                title: 'Sex',
                                options: selectOptions.SEX_TYPES,
                                trackBy: 'value',
                                label: 'name',
                            },
                        },
                    ],
                },

            };
        },

Answer №1

I suggest creating a custom method in Vue, specifically under the data section, that generates the object needed for the v-model attribute.

For example:

v-model="generateObject(formData[personSchema.prefix][scheme.model])"
or
v-model="generateObject([personSchema.prefix], [scheme.model])"

This method can help manage dot notation and retrieve the correct nested property when needed.

Answer №2

It doesn't seem to be achievable directly using v-model, but you might find solutions by referring to:

https://v2.vuejs.org/v2/guide/reactivity.html

You could also try implementing a watch function (deep: true) as a workaround. (Consider exploring the usage of watch properties within

formData[personSchema.prefix][scheme.model]
first.)

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

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

Include the await keyword within the .then block

I'm trying to execute an await after receiving a response in the .then callback of my code: const info = new getInfo(this.fetchDetails); info .retrieve() .then((res) => { const details = this.getLatestInfo(res, 'John'); }) .ca ...

AngularJS ui-router, html5Mode, and Express causing "Cannot GET" error in nested state

Currently, I am facing an issue while trying to implement $locationProvider.html5Mode(true) with AngularJS, ui-router, and ExpressJS. The problem arises when navigating to a nested state parent.child with URL /child, as refreshing the page or directly typi ...

How can I utilize a variable in a v-for loop as a label for a q-btn in Vue.js?

I have a list: myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] I'd like to generate buttons using this list where the label corresponds to the number: <q-btn v-for="number in myList" v-bind:key="number" color="primary" label=&q ...

Interactive image popups with JavaScript

Seeking assistance in generating a popup when selecting an area on an image map using Javascript. As a novice in JS, I have successfully implemented popups for buttons before but encountered issues with this code within the image map - the popup appears br ...

When working with jQuery, I encountered the error message "is not a function" because I mistakenly tried to use a function more than

While working on a pager, I encountered an issue with a function that is initially invoked when the document loads. However, when attempting to use it a second time, an error "is not a function" occurs. I am curious about the reason behind this phenomenon. ...

What is the process for uploading an image with express-fileupload?

Looking to upload an image to Cloudinary via Postman using the express-fileupload library for handling multipart forms. Here is a snippet from my index.ts file: import fileUpload from "express-fileupload"; app.use(fileUpload()); In my controller ...

A different approach for managing lengthy API processes in Node without using the traditional "Notification URL"

Developing an API that involves a lengthy data conversion process lasting up to 60 seconds presents the challenge of keeping users informed about any potential errors and the progress of the conversion stage. While sending WebSocket events is feasible for ...

What is the best method for extracting a value from a JSON file within an array using electron and node.js?

Hey there, I'm currently working with a JSON file that contains an array of value sets. I'm trying to figure out how to extract the first value from the initial set, specifically the value 3 under the key "pink". This is all being done in Node.js ...

JavaScript array contains duplicate values

I have developed a custom Asp.Net user control. To keep things simple, I have omitted some of the HTML code. <asp:Panel ID="PanelInputControl" runat="server" CssClass="input-control input-period"> <div ID="InputWrapperMonth" runat="server" c ...

I am encountering issues with my PostCSS plugin not functioning properly within a Vue-cli 3 project

I developed a custom postcss plugin that was working perfectly according to the postcss guidelines until I tried to implement it in a real project. For reference, here's the plugin on GitHub My goal is to integrate it into a Vue-cli app using Webpac ...

Tips for incorporating 'and' in the 'on' clause of 'join' in knex.js

I need assistance implementing the following SQL code in knex.js: select c.id,c.parent_id,c.comment,u.username,c.postid from comments as c join post_details as p on (p.id = c.postid and c.postid=15)join users as u on (u.id = c.userid); I attempt ...

Adjusting the size of the snap increments

When using gridstack, I have the ability to resize my widgets. However, I've noticed that when dragging on the widgets' handles, they snap to specific sizes, which seems to be a fixed amount. If I wanted to set the widget's size to something ...

Ionic 2: Unveiling the Flipclock Component

Can anyone provide guidance on integrating the Flipclock 24-hours feature into my Ionic 2 application? I'm unsure about the compatibility of the JavaScript library with Ionic 2 in typescript. I have searched for information on using Flipclock in Ionic ...

Obtaining the value of an input field in HTML

I am currently working on a text input field that triggers a JavaScript function when a numeric value is entered. <input type="text" value="key" ng-keypress="submit(key)" ng-model="pager.page"/> Controller $scope.submit = function (val) { ...

Bringing in a feature within the Vue 3 setup

At the moment, I am attempting to utilize a throttle/debounce function within my Vue component. However, each time it is invoked, an error of Uncaught TypeError: functionTD is not a function is thrown. Below is the code snippet: useThrottleDebounce.ts imp ...

Integrating JQuery with Sencha Touch: A Comprehensive Guide

Can you show me how to use JQuery with Sencha Touch? I have a Sencha button but when I click on it, nothing happens. I've checked that JQuery is working. xtype: 'button', text: 'Get result', docked: 'bottom', id: 'm ...

Using Angular's ng-href directive in conjunction with the Split method

I am encountering an issue with a recursive variable that has a specific value path = <d:URL>http://www.google.com, Google</d:URL> My goal is to bind this path into an Angular component. I have attempted to use the following method to bind th ...

What is the best way to handle multiple JSON data using jQuery?

Understanding how to utilize jquery for parsing simple json is a valuable skill. json { "Symbol": "AAL.L", "Name": "ANGLO AMERICAN", "Last": "3061.50", "Date": "7/26/2011", "Time": "11:35am", "Change": "+3 ...

Troubleshooting issue with file upload feature in Angular for Internet Explorer 9

I have implemented a file upload method using the following code: <input type="file" name="upload-file" ng-model= "excelFile" accept=".xlsx" onchange="angular.element(this).scope().fileChanged(this);" ...