mtpultz
mtpultz

Reputation: 18268

ngbBootstrap DatePicker Configuration Set firstDayOfWeek

I'm trying to configure the ngbBootstrap datepicker to use Sunday as the first day of the week. Seems like this should be super simple according to the docs. I am using NgbBootstrap v1.1.2, but the documentation in code is the same as the current docs:

Configuration service for the NgbDatepicker component. You can inject this service, typically in your root component, and customize the values of its properties in order to provide default values for all the datepickers used in the application.

import { NgbDatepickerConfig } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  //...

  constructor(
    private ngbDatepickerConfig: NgbDatepickerConfig
  ) {
    ngbDatepickerConfig.firstDayOfWeek = 7;
  }

  //...
}

Any ideas why it's still set to Monday?

Update

Seems to work if I override the service defaults:

{
  provide: NgbDatepickerConfig,
  useClass: class Test {
    dayTemplate: TemplateRef<DayTemplateContext>;
    dayTemplateData: (date: NgbDateStruct, current: { year: number, month: number }) => any;
    footerTemplate: TemplateRef<any>;
    displayMonths = 1;
    firstDayOfWeek = 7;
    markDisabled: (date: NgbDateStruct, current: { year: number, month: number }) => boolean;
    minDate: NgbDateStruct;
    maxDate: NgbDateStruct;
    navigation: 'select' | 'arrows' | 'none' = 'select';
    outsideDays: 'visible' | 'collapsed' | 'hidden' = 'visible';
    showWeekdays = true;
    showWeekNumbers = false;
    startDate: { year: number, month: number };
  }
}

Upvotes: 4

Views: 2397

Answers (2)

Eliseo
Eliseo

Reputation: 57941

My way to do is

1.-Create a class datePicker-config (it's a simple class of TypeScript)

import {NgbDatepickerConfig} from '@ng-bootstrap/ng-bootstrap';

export class CustomDatePickerConfig extends NgbDatepickerConfig {
    firstDayOfWeek=3;
}

2.-Use this class in provider of NgbDatepickerConfig in your modules

@NgModule({
  imports: [...],
  declarations: [...],
  providers:[{provide: NgbDatepickerConfig,useClass: CustomDatePickerConfig}]
  ...
})

Upvotes: 4

mtpultz
mtpultz

Reputation: 18268

Not sure why, but it doesn't work on the root component when all the child components are lazy loaded. I applied it to a common component used as a parent for all feature module route children and it works across the entire application as expected.

Upvotes: 2

Related Questions