The onShown event in ngx-bootstrap's datePicker is fired just before the calendar actually becomes visible

Recently, I've been exploring the capabilities of ngx-bootstrap's rangeDatePicker. My current challenge involves attempting to automatically navigate to the previous month as soon as the user opens the rangeDatePicker. To accomplish this, I have implemented a handler function that is triggered by the onShown event.

The code snippet below illustrates how I have set up the rangeDatePicker in my HTML:

<input id="input-range-dates" type="text" class="form-control" bsDaterangepicker #drp="bsDaterangepicker" [bsConfig]="bsConfig"
                 [(ngModel)]="bsRangeValue" (ngModelChange)="onChanges($event, drp)" [maxDate]="maxDate" placement="bottom"
                     (onShown)="handler('onShown')" (onHidden)="handler('onHidden')" readonly/>

While there are various properties defined for the rangeDatePicker, the key one related to my issue is:

(onShown)="handler('onShown')"

In the JavaScript file, I have created the following handler function:

handler(value: string): void {
if ('onShown' === value) {
      [].slice.call(document.getElementsByClassName('previous')).filter(element =>
        element.innerText === '‹')[0].click(); // Navigate to the previous month
  }
}

Despite my efforts, when I debug the application in Chrome, it seems like the calendar and button have not fully loaded yet, resulting in the click action failing to switch to the previous month. Can anyone provide assistance or suggestions on how to address this roadblock?

Answer №1

Why not opt for a different approach when dealing with the onShow and onHidden events?

(onShown)="handleEvent('onShown')" (onHidden)="handleEvent('onHidden')"

.

handleEvent(value: string): void {
if ('onShown' === value) {
      //...
  }
}

Consider creating two distinct methods and passing the actual $event as an argument:

(onShown)="showDetail($event)" (onHidden)="hideDetail($event)"

This way, you can observe the event details and better understand its behavior:

showDetail(event) {
  console.log(event);
  // ...
}

hideDetail(event) {
  // ...
}

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

Make sure a Typescript array contains a particular value

Currently, I am in the process of developing a dropdown-style component where the value and options are separate props. My goal is to incorporate Typescript to ensure that the value remains a valid option. Although I could perform this validation at runtim ...

Error: The browser threw a TypeError because Object(...) is not a function

I am having an issue with using Ionic Image Loader for image loading and caching. While there are no errors during compilation, I encounter the following error in the browser. Below are the details from my package.json file. As a newcomer to Ionic and Angu ...

Preventing event bubbling/propagation in custom events: a step-by-step guide

Incorporating a module-project from Github into my Angular project, I am able to resize the DOM elements that are displayed on top of a div functioning as a drawing board. To configure my initial rectangles, I am utilizing a combination of mouseDown - mou ...

When working with React and Typescript, I encountered an issue with refs causing a typescript error: "The property 'current' does not exist on the type '(instance: HTMLInputElement | null) => void'"

I am working on a component that requires a ref prop: import React, {forwardRef, ReactElement, ReactNode, HTMLProps, useRef} from 'react' import styles from './index.module.css' import classNames from 'classnames' import { Ico ...

transfer information from a Node.js server to an Angular client using the get() method

I am trying to access an array of data from a node.js file using angular. In my node.js file, I have the following code: app.get('/search', function(req, res){ res.send(data); }); However, I am facing difficulties retrieving this data in an ...

Problem with Invoking method of parent component from child component in Angular 4

Despite having all my event emitters set up correctly, there's one that seems to be causing issues. child.ts: @Component({ ... outputs: ['fileUploaded'] }) export class childComponent implements OnInit { ... fileUploaded ...

How can you verify the anticipated log output in the midst of a function execution with Jest unit testing?

Below is a demonstration function I have: export async function myHandler( param1: string, param2: string, req: Request, next: NextFunction, ) { const log = req.log.prefix(`[my=prefix]`); let res; If (param1 === 'param1&a ...

What is the best way to send a list of data as either strings or integers through a REST API using Angular's

While trying to post data from Angular formData to a Django REST API, I encountered an error saying "Incorrect type. Expected pk value, received str." Here is how I am currently sending the data using form data: let noticeData = this.announceForm.value; i ...

Guide on implementing ng2-completer with translation features

Recently delving into Angular, I'm experimenting with integrating the ng2-completer module alongside the TranslateModule. The issue arises when attempting to fetch JSON data from the server-side. The retrieved JSON structure is as follows: [{"id":10 ...

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 enum values in the HTML value attribute with Angular 2

I'm attempting to utilize an enum value in order to set the selected value of an HTML attribute: export enum MyEnum { FirstValue, SecondValue } export function MyEnumAware(constructor: Function) { constructor.prototype.MyEnum = MyEnum; } ...

Is it recommended to utilize the `never` type for a function that invokes `location.replace`?

I'm facing an issue with my TypeScript code snippet: function askLogin(): never { location.replace('/login'); } The TypeScript compiler is flagging an error: A function returning 'never' cannot have a reachable end point. Do ...

What is the process for creating a new element and utilizing its reference to add child elements in React?

I've been struggling to create an HTML element in a parent component in React, and then access that component's div from a child component in order to add new elements to it. Despite multiple attempts, I can't seem to resolve the issue of p ...

When attempting to instantiate a list from JSON in Angular, the type '{}' is lacking the following properties compared to type 'Datos[]'

I'm encountering issues when trying to incorporate JSON data into my Angular application. My Process: Importing the JSON file into a variable: import _data from '../../../assets/data.json'; Following the advice from responses in this t ...

Retrieve a result from an observable within a function

While attempting to solve what appears to be a simple problem, I'm struggling to find any solutions in my research. The issue arises when trying to subscribe to an Observable within a method and return a value from it. Here is an example of what I h ...

Discover the Prisma findMany method for implementing tanstack react table functionality

I'm looking to build a table (using tanstack table) populated with data fetched from Prisma.findMany. Let's suppose I have a User model: model User { id Int @id @default(autoincrement()) name String age String email String } Now, in my p ...

I'm having trouble incorporating TypeScript into my React useState hooks. Can someone help me troubleshoot?

I've been encountering challenges when trying to integrate Typescript into my React code, especially with the useSate hooks. I've dedicated several days to researching how to resolve this issue, but I'm uncertain about what should be passed ...

Using jQuery with Angular 4 allows for powerful front-end development

To include jQuery in an Angular4 project, I follow these steps: npm install --save jquery npm install --save-dev @types/jquery In the app.component.ts file import $ from 'jquery'; or import * as $ from 'jquery'; When running "ng se ...

Adding to an existing array in SQLite by updating a column using Sequelize

My code includes a model definition for saving product data using Sequelize: This is how the Product model looks: import {Optional, Model, Sequelize, DataTypes } from 'sequelize'; /*This is the Product model used to save the data about products* ...

Angular: Retrieve the source after navigating

Hello there, I am facing a simple problem. I have 2 components navigating to 1 component and in that one component, I need to distinguish which component the navigation came from so I can take appropriate action (such as refreshing a list). The issue is t ...