Customize the appearance of the Vue.js datepicker starting from today's date

I am currently using the vue-datepicker component to display a date input field in my form. I want to set the default date to the current day and disable the selection of past dates. Additionally, I need to change the language of the component but it seems like I can't apply custom styles to it.

You can find the component I am referring to here: https://github.com/charliekassel/vuejs-datepicker

Below is a snippet of the code I have:

<template>
  <form>
     <datepicker :bootstrap-styling="true"
               class="form-control"
     </datepicker>
  </form>
</template>
<sctipt>
import Datepicker from 'vuejs-datepicker';
export default {  
  components: { 
    Datepicker,
  },
  data () {
    return {
          selectedDate: ''
    },
    method: {

    }
  }
</script>

Has anyone encountered a similar issue? I'm working with Vue.js 2 and vuejs-datepicker.

Answer №1

There is a syntax error in your code - you need to move method: {} out of data(), and it should be methods instead of method.

Once you fix that, follow the tutorial to customize your calendar (refer to Available Props in the tutorial).

Don't forget to open your browser console to check for any errors.

Below is a demo allowing you to customize background, open date, and bootstrap styling within the <datepick>.

PS: According to the tutorial, if using CDN directly, you must use <vuejs-datepicker> instead of <datepicker>

PS: If the props provided by datepicker don't meet your needs, you may have to inspect the DOM tree for the calendar and add your CSS selectors to customize it, like I did to change the background.

Update: Be sure to clone a new Date object every time the open Date changes, otherwise reactivity will not trigger.

app = new Vue({
  el: "#app",
  components: {
    vuejsDatepicker
  },
  data() {
    return {
      selectedDate: '',
      bootstrapStyling: true,
      openDate: new Date()
    }
  },
  methods: {
    changeBootstrapStyling: function () {
      this.bootstrapStyling = !this.bootstrapStyling
    },
    changeLanguage: function () {
      this.openDate = new Date(this.openDate.setDate(this.openDate.getDate() + 90))
    }
  }
})
.bg-red > div > div {
  background-color:red
}

.bg-white > div > div {
  background-color:white
}
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e686b7b5e2c302b302f28">[email protected]</a>/dist/vue.js"></script>
<script src="https://unpkg.com/vuejs-datepicker"></script>
<div id="app">
  <button @click="changeBootstrapStyling()">Change Bootstrap Styling</button>
  <button @click="changeLanguage()">Change Open Date</button>
  <form>
    <vuejs-datepicker :bootstrap-styling="true" :open-date="openDate"
               class="form-control" :class="{'bg-red':bootstrapStyling, 'bg-white':!bootstrapStyling}"></vuejs-datepicker>
  </form>
</div>

Answer №2

Your inquiries can be categorized into three main parts: 1. How to style the vue.js date picker 2. How to set the date picker to start on the current date (disabling previous dates) 3. How to change the language

Note: There is a syntax error in your code where you have used the method property inside the data method. Please address this issue first.

1. Styling your date picker: I have previously addressed this issue in the following link, please refer to it: 2. Setting the calendar of your date picker to start from the current date: Answer: In the Vue date picker package, there are certain available props that allow you to achieve this, such as the :disabledDates prop. Here is an example:

<datepicker
  v-model="startDate"
  placeholder="Ends On"
  :open-date="new Date()"
  :disabledDates="disabledDates">
 </datepicker>    

In your data property, make the following adjustments:

data (){
  return{
      disabledDates:{
       to: new Date(Date.now() - 8640000)
      }
    }
  }

Upon implementation, you will notice that the previous dates are now disabled in the calendar view.

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

Customizing the MUI X Sparkline: Incorporating the percentage symbol at the end of the tooltip data within the MUI Sparklinechart

Presented below is a SparklineChart component imported from MUI X: import * as React from 'react'; import Stack from '@mui/material/Stack'; import Box from '@mui/material/Box'; import { SparkLineChart } from '@mui/x-chart ...

Gathering Servlet details from non-form elements

I am currently in the process of developing an application that is capable of parsing JSON strings. At this stage, I am able to input a JSON string using Java, write it to an HTML document, and then have a JavaScript program read and parse it before ultima ...

Using AngularJS date picker to set value in Spring model setter

When using AngularJS with Spring, I noticed a discrepancy in the date values between my view file and the POJO User object. In my view file, I have used an input type date to bind the "user.dateOfBirth" attribute. When I select a date, it is displayed cor ...

The UglifyJsPlugin in Webpack encounters an issue when processing Node modules that contain the "let" keyword

Below is the code snippet from my project which utilizes Vue.js' Webpack official template: .babelrc: "presets": [ "babel-preset-es2015", "babel-preset-stage-2", ] webpack.prod.config.js new webpack.optimize.UglifyJsPlugin({ compress: { ...

Unchecking random checkboxes within a div using jQuery after checking them all

Once a link is clicked on, all checkboxes within that particular div will be checked. function initSelectAll() { $("form").find("a.selectAll").click(function() { var cb = $(this).closest("div").find("input[type=checkbox]"); cb.not(":checked" ...

Using the Rails cocoon gem to populate numerous input fields

I'm currently implementing the cocoon gem in my rails application, where I have a form with two nested fields (categories and subcategories). Initially, only the first field is visible while the second one remains hidden. When the first select field h ...

What is causing the failure of success function commands to be executed?

Currently, I am facing some inconsistencies between my locally hosted app on WAMP (working perfectly) and the deployed version of my app. In an attempt to resolve this issue, I am working with CodeIgniter and jQuery AJAX. As part of my troubleshooting proc ...

Seems like the ng-show events inside are not being triggered, almost like an invisible image

I am encountering an issue where no JavaScript events are triggering inside an ng-show div in my code. You can access my code through the following link on Plnkr: http://plnkr.co/edit/kGqk8x?p=preview Upon loading data from JSON, I set ng-show to true. Ho ...

Setting up Vue.js API endpoints

I'm a newbie in VueJS, and I recently started using RxJS, Vue rx, and Vue Resource in a mixin to facilitate making HTTP calls and receiving observables anywhere... it's fantastic! Now, I decided to experiment with the following code: subscripti ...

Pixel information from previous canvas remains intact post resizing

I have crafted a canvas and loaded pixel data at specific locations using the following code snippet. let maskCanvas = document.createElement("canvas"); let patchWidth = 30; let patchHeight = 30; let scale = 3; maskCanvas.setAttribute("class", "mask"); ...

Encountering a persistent Unhandled rejection Error while utilizing NodeJs with Bluebird library

Currently in the process of developing a daemon that listens to TCP connections, sends commands, and listens for events. I made the decision to utilize bluebird to eliminate callbacks, but I'm encountering an issue. I can't seem to catch a rejec ...

Changes are not being detected in new Angular 2 files

Currently, I am enhancing an Angular 2 project by incorporating new modules. However, the changes I made in these files are not being recognized within the project. I have attempted to research how change detection functions in Angular 2, but have not bee ...

In the console, a JSON string is displayed, however the PHP variable outputs as null

Below is the snippet of javascript code I am working with: $('.showEditForm').click(function () { var webpagefileID = this.id; if($('#editForm').css('display', 'none')) { $('#editForm').css ...

What is the best way to trigger a controller action when the datepicker's value changes?

Hello, I have a custom datepicker and I am trying to perform a calculation when the year is changed. The code provided below does not seem to work on onchange. I also attempted using the onchange attribute and calling a JavaScript function like this oncha ...

Troubleshooting Problems with Promises in Node.js Express

I am currently developing a Node.JS/Express application with Jade as the template engine, but I am encountering some unexpected behavior. The issue arises when trying to retrieve data from a mock API and pass it to my Jade template. Despite confirming tha ...

How to utilize ref in Vue to access nested child components

Within my Vue component, I have an array of subelements that I use to render nested components in a loop based on the nesting level. Now, I want to be able to run a method from a child component within its parent and then recursively call it on its childr ...

Using jQuery to set the background-image on the body's after pseudo-element with CSS

I am currently utilizing body:after for setting the page wallpaper. body:after { background-image: url('assets/img/wallpapers/<?php echo $getWallpaperFile; ?>'); } CSS content: ' '; display: block; position: absolute; left: ...

Please type the file name into the provided text box

Is there a way to automatically insert the filename of an uploaded file into an existing text input using either or valums.com/ajax-upload/? (I am working with php) For example, in a textbox: <input name="image" type="text" value="file_name" /> ...

Do you need to finish the Subject when implementing the takeUntil approach to unsubscribing from Observables?

In order to prevent memory leaks in my Angular application, I make sure to unsubscribe from Observables using the following established pattern: unsubscribe = new Subject(); ngOnInit() { this.myService.getStuff() .pipe(takeUntil(this.unsubscr ...

Having issues with Sequelize and SQLite auto increment functionality. No errors when using AutoIncrement, but the auto increment feature is not working. On the other hand, errors arise when using

I am currently in the process of developing a discord bot, which includes 2 slash commands. One command is called /setup, used for adding the guildId and an adminChannel to a database. The other command is named /onduty, which is used for adding the user, ...