Ngx-bootstrap: feat(datepicker): add listening to the change of month property

Created on 24 Oct 2016  路  14Comments  路  Source: valor-software/ngx-bootstrap

Is there a way, how to listen to the change of the month property of datepicker? I need to send a request to the server in order to load a new set of disabled dates. Than I need to disable the dates in datepicker before it loads.

comp(datepicker) easy (hours) feat-request low

Most helpful comment

@goku321 cool!
You can start work on it! I'll assign it to you

All 14 comments

nice use case description!
it will be done

have any one find solution for month change using next & previous arrow event?

Any position on this?

Need it really hard :(

Is it still open? If yes, would like to work on this if that's okay.

@goku321 cool!
You can start work on it! I'll assign it to you

Any progress here?

Any updates on this topic?
Maybe take a look at how it was implemented in this datepicker?
https://ng-bootstrap.github.io/#/components/datepicker/api#NgbDatepickerNavigateEvent

Any progress on this?

Any progress on this?

As for me - I had to switch my datepicker to ng-bootstrap, unfortunately I had no time to wait until it is implemented here...

IN TEMPLATE

<input [(ngModel)]="modelDate" autocomplete="off" class="form-control" name="date" bsDatepicker [bsConfig]="{dateInputFormat: 'DD/MM/YYYY'}" (onShown)="onOpenCalendar($event)" >

IN COMPONENT

onOpenCalendar(container) {

    container.navigateTo  = (event: any): void => {
      container._store.dispatch(container._actions.navigateStep(event.step));
      console.log(event);
      // you will get step as month -1 or +1 and based on that you can do your stuff
    }; 
  }

IN TEMPLATE

<input [(ngModel)]="modelDate" autocomplete="off" class="form-control" name="date" bsDatepicker [bsConfig]="{dateInputFormat: 'DD/MM/YYYY'}" (onShown)="onOpenCalendar($event)" >

No, this does not work when the user clicks the month navigation control to move forward (or backward)

I would very much like to see this implemented as well.

I solved it by subscribing to its internal store. When the view was changed to another month you will get the date of the view accordingly.

Add this utils class to your project

import { BsDatepickerInlineDirective } from 'ngx-bootstrap/datepicker';
import { BehaviorSubject } from 'rxjs';

interface BsDatePickerStoreView {
  date: Date,
  mode: keyof { 'day', 'month', 'year'}
}

interface BsDatePickerStoreData {
  view:BsDatePickerStoreView
}

export class BsDatePickerUtils
{
  public viewChanged = new BehaviorSubject<Date>(undefined);

  private lastKnownViewDate: Date = undefined;

  constructor(datePicker: BsDatepickerInlineDirective) {
    const store = (datePicker as any)._datepicker.instance._store.source as BehaviorSubject<BsDatePickerStoreData>;
    store.subscribe((data) => {
      const viewMode = data.view.mode;
      const date = data.view.date as Date;
      if (!this.lastKnownViewDate || this.lastKnownViewDate.toDateString() !== date.toDateString()) {
        if (data.view.mode === 'day') {
          this.lastKnownViewDate = date;
          this.viewChanged.next(data.view.date);
        }
      }
    });
  }
}
// Usage example
@Component({
  selector: 'my-component',
  template: `
<div>
  <bs-datepicker-inline #datePicker></<bs-datepicker-inline>
</div>
  `
})
export class MyComponent implements OnInit, AfterViewInit {

  @ViewChild(BsDatepickerInlineDirective) datePicker: BsDatepickerInlineDirective;
  bsDatePickerUtils: BsDatePickerUtils;

  ngAfterViewInit() {
    this.bsDatePickerUtils = new BsDatePickerUtils(this.datePicker);
    this.bsDatePickerUtils.viewChanged.subscribe(date => {
      console.log('Calendar changed: ' + date);
    });
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tuoitrexuquang picture tuoitrexuquang  路  3Comments

PascalHonegger picture PascalHonegger  路  3Comments

srjkrl20011991 picture srjkrl20011991  路  3Comments

webdev48 picture webdev48  路  3Comments

phmello picture phmello  路  3Comments