2

I want the datepicker to be displayed on click of button and when I select the date it should display in textbox using angular.Pls help me solve this as on button click no datepicker is opened

<button type="button" class="btn btn-icon waves-effect waves-light btn-success" 
                        click="getReport()">
                  <i class="fas fa-info-circle"></i>
</button>
            
<mat-form-field>
    <input matInput type="text" class="form-content" id="pickDate" placeholder="Date" 
           [(ngModel)]="selectedFromDate" bsDatepicker [ngModelOptions]="{standalone: true}"
           [bsConfig]="{ isAnimated: true,adaptivePosition: true,containerClass:'theme-red',dateInputFormat: 'MM/DD/YYYY', selectFromOtherMonth: 'true'}"
           (ngModelChange)="setFromDate($event)" required autocomplete="off">                 
  </mat-form-field> 

Function Call:

async getReport() {
    $(".btn-success").datepicker('show');
  }
1

2 Answers 2

2

You can use the open() method on the MatDatepicker.

<mat-form-field>
  <input matInput [matDatepicker]="picker">
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<button (click)="picker.open()">Open</button>

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work I think
0

I faced a similar problem with the original input element but couldn't find a solution. Sharing it here in case others have encountered the same issue.

If you are working with Typescript and using the HTMLInputElement type, and you need to open the date picker when clicking on another element, you can achieve this by calling the showPicker() method on the element, the date picker will be opened:

const datePickerInput = document.querySelector('.input_date-picker') as HTMLInputElement;
datePickerInput.showPicker();

reference: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#:~:text=does%20not%20validate.-,showPicker(),-Shows%20a%20browser

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.