nav a

nav a

  • NA
  • 29
  • 2.4k

Angular4: need filter on start data and end date select

May 19 2018 12:43 PM
need to write code for two select box to filter on click.
  1. //please correct the pipe  
  2. import { Pipe, PipeTransform } from '@angular/core';  
  3. import { DatePipe } from '@angular/common';  
  4. @Pipe({  
  5. name: 'filterMonthYear'  
  6. })  
  7. export class FilterMonthYearPipe implements PipeTransform {  
  8. transform(value:any, startmonth: string, endmonth : string): any{  
  9. if(value.length === 0){  
  10. return value;  
  11. }  
  12. const resultArray = [];  
  13. console.log(startmonth);  
  14. console.log(endmonth);  
  15. var from_date = Date.parse(startmonth);  
  16. var to_date = Date.parse(endmonth);  
  17. for(const item of value)  
  18. {  
  19. if(item.startmonth > from_date && item.endmonth < to_date) {  
  20. resultArray.push(item);  
  21. }  
  22. };  
  23. return resultArray;  
  24. }  
  25. }  
html
  1. <label>Start Month</label>  
  2. <select class="form-control" name="selectedstart" [(ngModel)]="selectedstartmonth">  
  3. <option >Choose..</option>  
  4. <option *ngFor="let table of TableData" >{{table.startmonth}}</option>  
  5. </select>  
  6. <p>{{selectedstart}}</p>  
  7. </div>  
  8. <div class="col-xs-6 col-sm-6 col-md-6">  
  9. <label>End Month</label>  
  10. <select class="form-control" name="selectedend" [(ngModel)]="selectedendmonth">  
  11. <option >Choose..</option>  
  12. <option *ngFor="let table of TableData">{{table.endmonth}}</option>  
  13. </select>  
  14. <button class="btn btn-primary" type="submit"  
  15. (click)="getFilteredDate(startmonth.value, endmonth.value)">  
  16. Submit  
  17. </button>  
  18. <tr *ngFor="let table of TableData | filterMonthYear:selectedstartmonth:selectedendmonth">  
  19. <td class='td-group1'>{{table.description}}</td>  
  20. <td>{{table.money}}</td>  
  21. <td>{{table.test}}</td>  
  22. <td>{{table.bbb}}</td>  
  23. </tr>  
component.ts
  1. getFilteredDate(startmonth, endmonth){  
  2. //what can be the logic  
  3. }  

Answers (8)