Vue.js Field is empty

I have encountered an issue with my Vue date picker.

Although I am able to view and select the date, when I attempt to retrieve the date using the getDate method, it always returns as null.

If anyone could offer assistance with resolving this problem, I would greatly appreciate it. Here is the relevant code:

var overviewVue;

function initOverviewVue(model) {

    overviewVue = new Vue({
        el: '#overviewVue',
        data: {
            OverviewModel: model,
            repeatSchedule: [],
            startDate: ''
        },
        
       
        methods: {
            setOverviewDatePickers: function (target) {
                var $target = $(target);

                $target.daterangepicker({
                    autoUpdateInput: false,
                    locale: {
                        format: 'DD/M hh:mm',
                        cancelLabel: 'Clear'
                    },
                    timePicker24Hour: true,
                    timePicker: true,
                    singleDatePicker: true,
                    showDropdowns: true
                }, function (chosen_date) {
                    this.startDate = chosen_date.format('DD/MM/YYYY HH:mm');
                    $target.val(this.startDate);
                });
            },
            getDate: function () {
                return this.startDate;
            }
        }
    });
}

Answer №1

I've noticed a couple of issues in your code.

It appears that the date picker you're using is a jQuery date picker, which may not be the best choice for Vue. Consider using a date picker specifically built for Vue, such as this one. However, let's set that aside for now and focus on the main issue at hand.

In the callback function where you are assigning a value to this.startDate, there seems to be a scope problem due to entering another function call.

To resolve this, try converting your code to an arrow function (assuming arrow functions are supported in your environment). This way, this will have the correct lexical scope:

(chosen_date) => {
  this.startDate = chosen_date.format('DD/MM/YYYY HH:mm');
  $target.val(this.startDate);
}

By making these changes, this.startDate should now refer to the expected value. It's advisable to use tools and plugins specifically designed for Vue development, like the one mentioned earlier, to avoid encountering similar issues in the future.

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

What is preferable: defining JSON schema properties or utilizing oneOf conditions within an array of objects

I am looking to represent a variety of objects in a schema through an array called contents. This array can include any number of elements, but they must fall into one of two categories: one type represents text while the other type represents images. Up ...

Get the name of the form I am submitting using jQuery

Looking to create a jQuery script that can serialize the formData being submitted from any HTML page. The challenge arises when there are multiple forms on a single page - how do I distinguish which form is being submitted? My goal is to track activities o ...

Is it possible to integrate an "inline-template" component within another "inline-template" component?

I seem to be facing a roadblock in my project. I believe there might be a missing step or this task is simply impossible. I can't seem to wrap my head around it. Inline-templates offer the advantage of utilizing Laravel Blade syntax and harnessing th ...

Is there a way to modify the value of a variable within a document by referencing another variable present in the same document?

In my MongoDB collection, each document has three variables: const moneySchema = mongoose.Schema({ userID: String, tron: Number, sccn: Number }) Using JavaScript, I need to increase the value of sccn by (tron * multiplier) every hour where the mult ...

What is the best way to relay error messages to other sections of the form?

I am currently tackling a challenge with Vue and Laravel. The issue at hand involves an error class that I have created for a form which interacts with 3 different components. For instance, there is the base form component and then a child component of th ...

What is the best way to create a flexible canvas/grid/row/column that can adapt to changes in browser width and height, all while maintaining its aspect ratio

As someone new to the world of JavaScript, CSS, and HTML, I am eager to learn how to create a game site with multiple games. However, I am feeling lost when it comes to determining the right direction to take. My goal is to develop a game board that can ad ...

VueJS, when used in conjunction with Vuetify, might require an extra loader to handle scoped css

While attempting to compile my VueJS code using webpack, I encountered an error with the following code: <template> <v-app-bar app color="blue" flat height="100px"> <v-img class="mx-2" contain max-height="80" m ...

What is the best way to transfer information from an API to a state hook?

Encountering a recurring issue that requires some guidance. I am working on an API call and need to display the results in another component within the form's child elements. I've managed to create a component that can map state to JSX, but I&apo ...

Retrieve information from a JSON object based on the specified value

Here is the JSON data I am working with: { "sales": [{ "manager": "alberic", "surgeon": "Dr Barry Biggins", "amount": "300", "country": "USA", "percent-seller": "30", "antiquity": "June 2017", "d ...

Toggling in JS does not alter the DIV element

I attempted to utilize Bootstrap toggle to modify the div HTML content similar to the example shown at with a slight modification, but unfortunately, it did not function as expected due to a discrepancy in my current JavaScript code. I am unsure of what I ...

Access model information from a controller with the help of Next.js and Sequelize

I'm currently working on a project involving Vue.js as my frontend and Next.js as my backend. While everything seems to be running smoothly, I am facing an issue with retrieving my model data back to my controller... Additional details: I have utili ...

Webpack has made Rails .js.erb templates obsolete

Recently, I migrated my Rails application to use WebPack for handling assets, and it has been operating smoothly. However, I encountered an issue with JS templates located in my views directory (*.js.erb) that require jQuery. Since jQuery is included in my ...

AngularJS seems to have trouble with routing functionality

I've recently started learning AngularJS and have been following CodeSchool's video series. However, I've encountered an error when trying to route through a click on an image. Can someone please assist me with this? Below are my HTML and J ...

I'm having trouble getting the activeStyle to work properly with a <NavLink>

I'm facing an issue with setting activeStyle for the NavLink component when the URL is on the map route. Currently, when I click on a NavLink, I can navigate to the correct route, but only <NavLink to='/' is being highlighted as active. ...

Obtain the ending section of a URL using JavaScript

Is it possible to extract the username from a URL like this example? I'm interested in seeing a regex method for achieving this. The existing code snippet only retrieves the domain name: var url = $(location).attr('href'); alert(get_doma ...

Utilizing zlib Compression with Node.js in a TCP Connection

I am currently embarking on my node.js journey, delving into the world of TCP servers. My goal is to ensure that all messages sent to clients are in a compressed format, utilizing zlib for this task. Here is an example of server-side code: zlib.deflate(r ...

Enhance your websites' search functionality with jQuery autocomplete using AJAX

I am attempting to implement dynamic autocomplete values for a text box. Here is my solution: echo json_encode($res) 0: {type_name: "name1"} 1: {type_name: "name2"} 2: {type_name: "name3"} 3: {type_name: "name4"} 4: {type_name: "name5"} Below is the co ...

Improving a Vue.js notification component with data retrieved from a fetch request result

Struggling with updating the content inside a vuetify v-alert. There's an issue when trying to update Vue component properties within the sessionID.then() block after logging into a system and receiving a session id. Vue.component('query-status ...

Oops! Make sure to call google.charts.load before calling google.charts.setOnLoadCallback to avoid this error

My HTML file includes the following imports: <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> ...

Unable to achieve the desired focus on a specific element using JavaScript's .focus() method

After executing the command $('#someelement').focus(), I am not receiving any error message but the functionality is not working as expected. Even when I attempt to retrieve document.activeElement, it continues to return the body element. Here i ...