list-invoices.component.ts 2.31 KB
Newer Older
Ooh-Ao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
import { Component } from '@angular/core';
import { InvoiceList } from '../invoice';
import { InvoiceService } from '../invoice.service';

@Component({
  selector: 'app-list-invoices',
  templateUrl: './list-invoices.component.html',
  styleUrls: ['./list-invoices.component.css']
})
export class ListInvoicesComponent {

  compInvoice: InvoiceList[];


  // checkbox
  isMasterSel: boolean;
  checkedCategoryList: any;

  ///////////////////////////


  // pagination
  page = 1;
  pageSize = 5;
  totalLengthOfCollection: number;


  constructor(public invoiceService: InvoiceService) {


    this.compInvoice = this.invoiceService.getInvoiceList();

    // pagination
    this.totalLengthOfCollection = this.invoiceService.getInvoiceList().length;

    // checkbox
    this.isMasterSel = false;
    this.getCheckedItemList();
  }




  deleteInvoice(id: number): void {

    if (confirm('Are you sure to delete this ! ')) {

      this.compInvoice = this.compInvoice.filter(invoice => invoice.id !== id);
      this.totalLengthOfCollection = this.compInvoice.length;
      this.invoiceService.deleteInvoice(id);
    }
  }


  // check-box.

  checkUncheckAll() {
    for (var i = 0; i < this.compInvoice.length; i++) {
      this.compInvoice[i].isSelected = this.isMasterSel;
    }
    this.getCheckedItemList();
  }

  isAllSelected() {
    this.isMasterSel = this.compInvoice.every(function (item: any) {
      return item.isSelected == true;
    })
    this.getCheckedItemList();
  }

  getCheckedItemList() {
    this.checkedCategoryList = [];
    for (var i = 0; i < this.compInvoice.length; i++) {
      if (this.compInvoice[i].isSelected)
        this.checkedCategoryList.push(this.compInvoice[i]);
    }
    this.checkedCategoryList = JSON.stringify(this.checkedCategoryList);
  }


  /////////////////

  _searchTerm: string = '';
  get searchTerm(): string {
    return this._searchTerm;
  }
  set searchTerm(val: string) {
    this._searchTerm = val;

    this.compInvoice = this.filter(val);
    this.totalLengthOfCollection = this.compInvoice.length;
  }

  filter(v: string) {
    return this.invoiceService.getInvoiceList().filter(x => x.billTo.toLowerCase().indexOf(v.toLowerCase()) !== -1 ||
      x.billFrom.toLowerCase().indexOf(v.toLowerCase()) !== -1 || x.status.toLowerCase().indexOf(v.toLowerCase()) !== -1 || x.totalCost !== -1);
  }
}