Angular 8/7 Material Inline MatCalender/ Datepicker Tutorial by Example

We have already discussed how to use Material UI framework in Angular project and how to use Datepicker component in a view.

But sometimes we may need to show Datepicker inline in a view so that it will be always visible to the user for selections.

In this tutorial, we will go through steps to demonstrate how to use Calendar inline in component with available options like:

  • Set Max Date
  • Set Min Date
  • Filter some of the dates; Disable Sundays and Saturdays in Inline MatCalender
  • Start At property
  • Event trigger control on date selected.

First, let’s go through steps to create a new Angular project and install Angular Material.

You can jump to Calendar Implementation here.

Create a new Angular Project

As now we have NG CLI tool install, we can easily create a new Angular project by running following command in CLI

After running above command it will ask if you want Routing in the project? and also about CSS style formats you want to opt for the project.

Install Angular Material

To install Angular Material in Angular project run following npm CLI command to install Material, CDK and Animation packages.

Enable Animation for Material Components

Animations like smooth scrolling effect, ripple effect on button click need to be enabled or disabled by importing following modules.

In the app.module.ts file make the following changes:

Enable Animations:

Disable Animations:

Add Material Theme

Components used in the project are styles using CSS available in the material CSS theme file, so just add the following theme file link in styles.css available at the project root.

you can use any one of these available themes.

That’s it … we are done with Angular material configuration.

Add Inline Material Datepicker Calendar

For using an Angular Material UI component, we need to import its API in the app’s main module.

Here we will import the following modules:

MatDatepickerModule and MatNativeDateModule are required modules to show Inline datepicker.

MatGridListModule(optional) here we used this module to use Grid structure and align out Datepicker better in view

After importing these modules, the app.module.ts file will look like this:

Now to show Inline Datepicker Calender we will use mat-calendar component with following option properties:

Input Properties:

selected: Set selected/ highlighted date in calendar datepicker.

startAt: Date from which calender view will start.

minDate: Set Minimum date in calendar datepicker.

maxDate: Set Maximum date in calendar datepicker.

dateFilter: Filter method used to enable or disable dates. ie disable Saturdays and Sundays in the calendar view.

Output Properties:

selectedChange: Event-triggered when a date is selected by the user.

In the app.component.html file place following code:

In the app.component.ts file add following code with property bindings, filter, and method:

It will look like this:

Thanks for visiting :) Let me know if you have some suggestions or feedback for this post in the comment section. Looking for any tutorial or demo on FreakyJolly, just drop me a message here

Leave a Reply

avatar
  Subscribe  
Notify of