Prevent selection of future dates and display them in a muted grey color in the p-calendar component

I am attempting to prevent users from selecting future dates and visually distinguish them by setting a grey color background. However, I am having trouble disabling the future dates while the grey color background is functioning correctly. Any ideas on how to address this issue?

HTML file:

<p-calendar [(ngModel)]="dateInDateFormat" [monthNavigator]="true"
                        [yearNavigator]="true" yearRange="1970:2099" [dateFormat]="const.dateFormatCal"
                        formControlName="daateFromControl" dataType="string">
                        <ng-template pTemplate="date" let-date>
                          <div *ngIf="checkFutureDate(date);then highlightedDate else normalDate"></div>
                          <ng-template #highlightedDate>
                              <span [ngStyle]="{backgroundColor: 'inherit'}" style="border-radius:75%">{{date.day}}</span>
                          </ng-template>
                          <ng-template #normalDate>
                                  <span [ngStyle]="{backgroundColor: '#d3d3d3'}" style="border-radius:75%">{{date.day}}</span>
                          </ng-template>
                  
                      </ng-template></p-calendar>

TypeScript:

  checkFutureDate(date:any){
    var calendarDate = new Date(date.year,date.month,date.day);
    calendarDate.setHours(0,0,0,0);
   console.log(""+calendarDate+""+(calendarDate < this.todayDate));
    return calendarDate < this.todayDate;
  }

output:

https://i.sstatic.net/RBDS6.png

I have tried using [maxDate]="todayDate", which successfully disables future dates but does not apply the grey color background. Can someone provide any suggestions or solutions?

Answer №1

To customize the appearance of disabled elements within the p-calendar component, use the p-disabled class.

If you set the background-color to grey, it will apply a grey background color to the disabled fields.

https://i.sstatic.net/JWCls.png

Here is an example in HTML:

<p-calendar [(ngModel)]="date" 
            [minDate]="minDate" 
            [maxDate]="maxDate" 
            [readonlyInput]="true" 
            inputId="min-max" 
            styleClass="custom-theme">
</p-calendar>

For styling in CSS:

.custom-theme .p-disabled {
  background: grey !important;
}

You can see a live demo here on stackblitz

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

Customize the size of data points on your Angular 2 chart with variable

In my Angular 2 application, I am utilizing ng2-charts to create a line chart. The chart functions properly, showing a change in color when hovering over a point with the mouse. However, I want to replicate this behavior manually through code. Upon clicki ...

Checkbox selection can alternate based on conditions for formgroup controls

My FormGroup named 'fruits' has been set up fruits: FormGroup = formBuilder.group({ numberOfFruits: [0,Validators.min(0)], apple: false, mangoes: false }); Here is the corresponding HTML code: <div formGroupName ...

The Concept of Static Block in TypeScript

For the purpose of testing Network Encoding/Decoding Logic, I have implemented a pair of test cases in both Java and JavaScript. These tests utilize Data Providers which essentially consist of various Constants. In my Java test case, I have a Data Provide ...

Error: Unable to use import statement outside of a module when deploying Firebase with TypeScript

Every time I try to run firebase deploy, an error pops up while parsing my function triggers. It specifically points to this line of code: import * as functions from 'firebase-functions'; at the beginning of my file. This is a new problem for me ...

Hide the FormArray in a Reactive Form if it is not populated or cannot be determined

As a newcomer to Angular 4, I've embarked on my first project and started learning the ropes. I have devised a Reactive Form to showcase a form structure that looks like this: Form (FormGroup) | --> aggLevels (FormArray) | --> ...

Issue with Angular ngFor binding. What could be causing this error to occur?

I have a main component called DOCUMENT. This document receives a URL segment and retrieves an array of associated objects from my database. Then, using @Output() documents = new EventEmitter() and an @Input() in a DOCUMENT VIEW component, I loop through t ...

Make an indirect mention of a distant JavaScript web address

Our company is looking to incorporate Rollup with Angular 4/Typescript and NPM, and we have a specific set of requirements: Various teams develop JS libraries that need to be centralized, similar to a CDN These libraries are hosted at remote URLs and sho ...

"Utilizing Firebase Functions to update information in the Firebase Realtime Database on a daily basis

Currently, I am in the process of working on a project where I aim to provide users with a daily percentage of points based on their current available points and update this data in my Firebase database. My goal is to add points for users on a day-to-day b ...

Issues with path in ngx-cookie-service while using Angular 9

I'm currently utilizing the ngx-cookie-service package to store important data for my application. It is crucial for me to save this cookie on the base path '/' so that I can easily retrieve it when needed. The cookie requires periodic updat ...

An example of using the index as an attribute within the same tag in Angular 2 *ng

Currently, I am in the process of creating a bootstrap carousel. To accomplish this, I have utilized *ngFor to add elements and carousel-indicators (small circles that show the current position). <li data-target="#myCarousel" *ngFor="#item of items; #i ...

Angular 8 - Utilizing External URL Routing

After creating a route that generates a token, I encountered an error when trying to retrieve the token from the URL: if (!this.session.token) { let urlToken= 'https://www.url-system.com.br//auth&redirect_uri=http://8000/api/res-token'; let r ...

Issue with Next.js hook: Uncaught TypeError - Unable to define properties of undefined (setting 'type')

Encountered an error while attempting to build my nextjs app. Strangely, this error wasn't present in the previous version of the app. I didn't make any changes to the config files, just added a few animation libraries and that's all, along ...

Setting up Material-UI for React in conjunction with Typescript: A step-by-step guide

I've encountered issues while trying to integrate Material UI into my React project that's written in Typescript. Following the tutorial, I began by adding the react-tab-event-plugin. import injectTapEventPlugin from 'react-tap-event-plugi ...

Encountered an unexpected import token in Angular2 (SystemJS)

i am trying to find a solution for this issue in my Angular2 project. I encountered an error and need assistance: `"(SystemJS) Unexpected token import SyntaxError: Unexpected token import at Object.eval (http://....../app.module.js:14:25) at eval (h ...

Is it recommended to use prefixes for name attributes in Angular components?

In my custom angular component, I have radio buttons that look like this. These same buttons are also used in the main component, but they do not function correctly unless the name attributes are changed. Here is the code for the custom component: <di ...

Utilizing precise data types for return values in React hooks with Typescript based on argument types

I developed a react hook that resembles the following structure: export const useForm = <T>(values: T) => { const [formData, setFormData] = useState<FormFieldData<T>>({}); useEffect(() => { const fields = {}; for (const ...

When executing the "node app.js" command, the <app-root> element appears empty in the browser

After creating an Angular 7 application with the Angular CLI and adding my express server, I encountered a strange issue. When starting my app with the command "node server/app.js", the browser displayed <app-root></app-root> in the "Elements" ...

I encountered an error stating "Buffer is not defined" originating from the Deode/Encode Stream Bundle.js script, not from my own code

I've encountered a major issue while attempting to update my npm project to webpack 5, and now I'm left with just one persistent error: bundle.js:1088566 Uncaught ReferenceError: Buffer is not defined at bundle.js:1044980:24 ...

Tips for hiding a bootstrap modal in Angular4 when it has no content

I am currently working on an Angular 4 e-commerce application. One of the requirements is to hide a bootstrap modal when there are no products in the cart. When a user selects some products, they are added to the mycart modal screen. The user also has the ...

TS2345: The argument provided, which is of type 'Event', cannot be assigned to the parameter expected, which is of type 'HtmlInputEvent'

I am facing an issue while trying to upload a file, and I could use some assistance in resolving it. Angular-----Error: src/app/admin/producto/create-producto-dialog.html:38:47 - error TS2345: Argument of type 'Event' is not assignable to parame ...