primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Calendar - add/support Enabled Dates

Open Canos opened this issue 8 years ago • 10 comments

I'm submitting a ... (check one with "x")

[ ] bug report => Search github for a similar issue or PR before submitting
[x] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Current behavior Currently, you cannot provide a list of dates that are the only enabled dates on the Calendar control

Expected behavior Provide a list of enabled dates, and all other dates would be disabled

What is the motivation / use case for changing the behavior? Provide a way of showing the calendar with only the dates that have data enabled. In my case i have an average of 3 days enabled and the rest of the year disabled. So it's also a perfomance and legibility issue.

This issue is the same that https://github.com/primefaces/primeng/issues/3712 I'd like to attach a PR, so I think I have to file an issue again.

Canos avatar Sep 06 '18 10:09 Canos

If there is more demand, we'll go ahead.

cagataycivici avatar Sep 18 '18 07:09 cagataycivici

I would need that feature, too. Are there any plans to implement it?

christopherthurnher avatar Feb 28 '19 08:02 christopherthurnher

I would need this feature too. In a scenario where you have a datatable and a date filter column, you would expect to be able to filter only on the enabled days (so those dates for which is data in the table for a given date). Setting the enabled dates would then make more sense than looping & setting all possible disabled dates.

sticl avatar Mar 26 '19 10:03 sticl

I need that too

alexRasi avatar May 30 '19 11:05 alexRasi

Same for me. Definitely need this feature.

aphasia0 avatar Jul 15 '20 13:07 aphasia0

This missing feature is preventing me to use this library. Is there any workaround? We have an item ordering feature, where we select the delivery dates, but only the ones that are allowed.

sasos90 avatar Mar 29 '22 15:03 sasos90

If people are still seeking a fix until this is a feature, here's a workaround I did. Somewhere in the startup of your app, create a method called "installHacks()" or such, and in there, put the following:

public installHacks() {
    Calendar.prototype["enabledDaysCache"] = [];
    Calendar.prototype["setEnabledDates"] = function(dates:Date[]):void {
      try {
        this.enabledDaysCache = [];
        for (let eDate of dates) {
          this.enabledDaysCache.push(eDate.getFullYear() + ":" + eDate.getMonth() + ":" + eDate.getDate());
        }
      }
      catch (err) {
        console.error(err);
      }
    }

    // ensure the part below "original code" matches the source function in the GitHub for PrimeNG Calendar that you're using
    // current source: https://github.com/primefaces/primeng/blob/master/src/app/components/calendar/calendar.ts
    Calendar.prototype.isDateDisabled = function(day:number, month:number, year:number):boolean {
      if (this.enabledDaysCache && this.enabledDaysCache.length > 0) {
        try {
          return !this.enabledDaysCache.includes(year + ":" + month + ":" + day);
        }
        catch (err) {
          console.error(err);
        }
      }

      // original code
      if (this.disabledDates) {
        for (let disabledDate of this.disabledDates) {
          if (disabledDate.getFullYear() === year && disabledDate.getMonth() === month && disabledDate.getDate() === day) {
            return true;
          }
        }
      }

      return false;
    }
}

So we created a holder called enabledDaysCache. As we can't create a bindable property (if you need that, add the necessary code and inject, assuming you can get the compiler to accept it).

Where you are using the calendar, give it a #name and fetch it via:

@ViewChild('myCalendar', {read: Calendar}) myCalendar: Calendar;

Then in afterViewInit or similar you'd do this:

        this.myCalendar.disabledDates = [new Date()]; // forces all the regular checks to see if dates are disabled to happen
        this.myCalendar["setEnabledDates"](yourListOfDates);
        this.cd.markForCheck();

This essentially disables every date by default, unless it's in the enabledDays array.

crumhorn avatar May 03 '22 07:05 crumhorn

Hi,

So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!

Best Regards,

mertsincan avatar Nov 09 '22 14:11 mertsincan

Hi,

So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!

Best Regards,

mertsincan avatar Nov 09 '22 14:11 mertsincan

I would need that feature, too. Are there any plans to implement it?

razepu avatar Jul 11 '24 09:07 razepu