Introduction
In this article, we will learn how to search data between two dates using Web API and Angular 9.
Prerequisites
- Basic Knowledge of Angular 2 or higher
- Visual Studio Code
- Visual studio and SQL Server Management studio
- Node and NPM installed
- Bootstrap
- ngx-bootstrap
Create an Angular project by using the following command.
- ng new searchdata
Open this project in Visual Studio Code and install bootstrap by using following command
- npm install bootstrap --save
Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
- @import '~bootstrap/dist/css/bootstrap.min.css';
To add datepicker we use ngx bootstrap datepicker, so add ngx bootstrap datepicker in this project using the following command.
- npm install ngx-bootstrap --save
Now open index.html file and add the following cdn link to add css for datepicker.
- <link rel="stylesheet" href="https://unpkg.com/ngx-bootstrap/datepicker/bs-datepicker.css">
Create a new service using the following command
- ng g s searchdata
Now open searchdata.service.ts file and add the following lines:
- import { Injectable } from '@angular/core';
- import {HttpClient} from '@angular/common/http';
- import {HttpHeaders} from '@angular/common/http';
- @Injectable({
- providedIn: 'root'
- })
- export class SearchdataService {
- Url: string;
- constructor(private http : HttpClient) {
- }
- searhhdata(model : any){
- debugger;
- return this.http.post<any>('http://localhost:1141/Api/Searchdata/search',model);
- }
- showdata(){
- debugger;
- return this.http.get<any>('http://localhost:1141/Api/Searchdata/showdata');
- }
- }
Now create a new component by using the following command.
- ng g c searchdata
Now open searchdata.component.ts file and add the following code:
- import { Component, OnInit } from '@angular/core';
- import { SearchdataService } from '../searchdata.service';
- @Component({
- selector: 'app-searchdata',
- templateUrl: './searchdata.component.html',
- styleUrls: ['./searchdata.component.css']
- })
- export class SearchdataComponent implements OnInit {
- constructor(private searchdataService:SearchdataService) { }
- model : any={};
- emp:any;
- ngOnInit(): void {
- this.showdata();
- }
- showdata()
- {
- this.searchdataService.showdata().subscribe((res: any) => {
- this.emp=res;
- console.log(this.emp);
- })
- }
- searchdata() {
- debugger;
- this.searchdataService.searhhdata(this.model).subscribe((res: any) => {
- this.emp=res;
- console.log(this.emp);
- })
- }
- }
Open searchdata.component.html file and add the following lines
- <div class="row">
- <div class="col-sm-12 btn btn-primary">
- How to Search Data Between Two Dates Using Web API and Angular 9
- </div>
- </div>
- <form #addClientForm="ngForm" (ngSubmit)="searchdata()" novalidate>
- <div class="row" style="margin-top:10px;margin-bottom: 10px;">
- <div class="col-sm-3 form-group"> </div>
- <div class="col-sm-3 form-group">
- <input
- type="text" #startdate="ngModel" name="startdate"
- [(ngModel)]="model.startdate"
- placeholder="From Date"
- bsDatepicker
- [bsConfig]="{ isAnimated: true }" class="form-control"/>
- </div>
- <div class="col-sm-3 form-group">
- <input type="text" #enddate="ngModel" name="enddate"
- [(ngModel)]="model.enddate"
- placeholder="To Date"
- bsDatepicker
- [bsConfig]="{ isAnimated: true }" class="form-control"/>
- </div>
- <div class="col-sm-3 form-group">
- <button type="submit" class="btn btn-success" >Search</button>
- </div>
- </div>
- </form>
- <table class="table">
- <thead class="thead-dark">
- <tr>
- <th scope="col">Id</th>
- <th scope="col">Name</th>
- <th scope="col">City</th>
- <th scope="col">JoiningDate</th>
- </tr>
- </thead>
- <tbody>
- <tr *ngFor="let emp of emp">
- <th scope="row">{{emp.Id}}</th>
- <td>{{emp.Name}}</td>
- <td>{{emp.City}}</td>
- <td>{{emp.JoiningDate | date:'yyyy-MM-dd'}}</td>
- </tr>
- <tr *ngIf="!emp||emp.length==0">
- <td colspan="4">
- No Data Found
- </td>
- </tr>
- </tbody>
- </table>










Fli WavePosted Jun 2, 2021, 2:53 PM
Why would you use a stored proc to do the search and not search on the dbContext?