Tips for updating the background color of a single date cell in ngb-datepicker using Angular

Recently, I started using ngb-datepicker and I'm curious if it's possible to customize the background-color of a specific day, like the 8th of November.

HTML

<ngb-datepicker name="dp" [(ngModel)]="model" ngbDatepicker [dayTemplate]="customDay" [markDisabled]="isDisabled"
    #d="ngbDatepicker"></ngb-datepicker>


<ng-template #customDay let-date let-currentMonth="currentMonth" let-selected="selected" let-disabled="disabled"
  let-focused="focused">
  <span class="custom-day" [class.weekend]="isMarkedDay(date)" [class.focused]="focused" [class.bg-primary]="selected"
    [class.hidden]="date.month !== currentMonth" [class.text-muted]="disabled">
    {{ date.day }}
  </span>
</ng-template>

Typescript

export class HistoryComponent implements OnInit {
  model: NgbDateStruct;
  accomplishedDays: NgbDate[] = [];
  day = new Date().getDate();
  month = new Date().getMonth() + 1;
  year = new Date().getFullYear();
  today: NgbDate = new NgbDate(this.year, this.month, this.day); // July, 14 1789

  constructor(
    public mockdataService: MockDataService,
    private calendar: NgbCalendar
  ) {}

  ngOnInit() {}
  isDisabled = (date: NgbDate, current: { month: number }) =>
    date.month !== current.month;

  isMarkedDay = () => this.calendar.getToday() == this.today; // .getWeekday(date) >= this.today;
}

I've been experimenting with this for some time now, but I'm struggling to get it to work. Currently, my goal is to highlight the current day, but it's not functioning as expected. I've even verified in the console that this.calendar.getToday() and this.today are identical, yet the comparison yields false.

Answer №1

Achieving this is definitely possible - you have the ability to provide a custom HTML template for each day in the DP, allowing you to easily apply a CSS class based on the specific day.

  • For more information, refer to the official documentation. You can also check out this example for reference.

UPDATE

  • When comparing dates, keep in mind that JS objects cannot be compared directly in that manner. You may want to explore more about JS Object comparison to understand the concept better.

  • To effectively compare dates, consider using the built-in method on the NgbDate for comparison. Implementing this in your code would look something like this:

    isMarkedDay = () => this.calendar.getToday().equals(this.today));

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

Strategies for handling asynchronous requests and effectively presenting the retrieved data

On my HTML page, I have a badge component that shows the number of unread messages. Here is the HTML code: <button class="font" mat-menu-item routerLink="/message"> <mat-icon>notifications</mat-icon> <span [matBadgeHidden]="newM ...

Tips for toggling the visibility of a <div> element with a click event, even when there is already a click event assigned

No matter what I try, nothing seems to be working for me. I'm looking to hide the <div id="disqus_thread"> at first and then reveal it when I click on the link "commenting", after the comments have loaded. This particular link is located at the ...

Add some text onto the Box Component in Material UI

I am looking to replicate the hover functionality (UI) seen in this example: Desired UI Source: Since adjusting background image styles can be complex, I have opted to use the Box Component from Material UI. Within the Box Component, I have implemented th ...

Prevent Promise / Property not found error in Angular2 by Instantiating Class

When working with my "export class", I encountered an issue that led to a Promise error if I didn't include this line of code: purchase = new Purchase(); right before the constructor. The error indicated that the property "name" was not found. Alth ...

The error was caused by my use of ng-template instead of the traditional template tag

I am currently working on an Angular 2 rc.1 application and facing an issue with overriding the existing pager by nesting a ng-template with the directive kendoPagerTemplate inside the kendo-grid. Here is an example of what I am trying to achieve: <ke ...

Keep fancybox items in their designated spot

When the window is opened at full size, everything looks fine. However, when you resize the window horizontally, the thumbnails shift and disappear. I want them to remain fixed in their position like the large pop-up image so that users can still see them ...

Having trouble retrieving a value from the img.onload event handler. A 'boolean' type error is being thrown, indicating it cannot be assigned to type '(this: GlobalEventHandlers, ev: Event) => any'

In my Angular application, I have implemented a method that verifies the size and dimensions of an image file and returns either false or true based on the validation result. Below is the code snippet for this function: checkFileValidity(file: any, multipl ...

I am currently attempting to find a color picker element within a parent class using Cypress CSS Locator. While I am able to reach the parent element, I am unsure of the correct method to target the

My user interface has a list of elements displayed in 2 columns. The first column shows the name of the item, such as Manager, Operator, and the list is expected to grow. The second column consists of a color picker element where you can choose a color. I ...

Is it possible to continuously re-render a React Functional Component with Axios and useState/useEffect?

Seeking assistance with creating a React Functional Component using Typescript to fetch data from an API and pass it to another component. However, encountering the error message "Error: Too many re-renders. React limits the number of renders to prevent an ...

The property functions normally outside the promise, but is undefined when within the promise context

I am currently working on filtering an array based on another array of different objects but with the same key field. Although I have made some progress, I keep encountering errors that I am unable to resolve. @Component({ selector: 'equipment&ap ...

What is the best way to access the ngClass value in an Angular test?

Here is the content of my component.html file with the ngClass attribute used in the second span element: <span class="container"> <button mat-icon-button [disabled]="disabled" (click)="onButtonClicked()"> <mat-icon>{{ico ...

Center-align the text in the navigation bar

Is there a way to center the text within my navigation and ensure it remains centered across all resolutions (left at 1920x1080, centered at 1420)? I understand that much of this code is inefficient and not working correctly, but for now I just want to fi ...

TypeScript declaration: discovering modules and defining namespaces

I'm currently in the process of creating a declaration file for h3. For guidance, you can refer to the function available here. Initially, I'm unsure of how typescript identifies declaration files. It seems to detect my declaration when placed ...

What methods can be used to incorporate animation when the display attribute transitions to none?

Is there a way to add animation in a Vue app when replacing elements? I would like the transition from, for example, clicking on a div with 'Num 1' to the divs with class 'showing' not disappear abruptly but smoothly, such as moving to ...

Changing the default font size has no effect on ChartJS

I'm trying to customize the font size for a chart by changing the default value from 40px to 14px. However, when I set Chart.defaults.global.defaultFontSize to 14, the changes don't seem to take effect. Below is the code snippet for reference. An ...

Tips for adjusting the legend on a chart in chart.js when the text is being truncated

https://i.sstatic.net/5IzUO.png Is there a way to eliminate the extra space on the left and right side of the bar so that the legend can be fully displayed with the entire text? ...

What are the repercussions of labeling a function, TypeScript interface, or TypeScript type with export but never actually importing it? Is this considered poor practice or is there a potential consequence?

I find myself grappling with a seemingly straightforward question that surprisingly has not been asked before by others. I am currently immersed in a TypeScript project involving Vue, and one of the developers has taken to labeling numerous interfaces and ...

Change the content inside a div depending on the value of a button

I am currently exploring how to change the content of a div based on button values, utilizing angular2+. Below is my implementation in HTML and js export class TestComponent implements OnInit { title:string = "provas"; constructor() { } popula ...

The Droppable feature in jQuery/jQueryUI molds itself to match the shape of the Dragged

In my current setup, I am working with a single column table where each cell is a droppable element that only accepts draggables of a specific class. While the borders of the table are visible, I am not a fan of the fixed size and colored cells within, a ...

Discrepancy in the vertical pattern repetition of the SVG background

When I use a simple div with an SVG set as a background image with vertical repeat, I noticed a gap of varying sizes on Chrome and Firefox depending on the screen size (please resize the window). Check out the issue here .bg { width: 50%; height: 2 ...