This commit is contained in:
Amritanshu
2019-06-15 23:09:43 +05:30
parent 3dcf1c4c42
commit 459ab244ff
70 changed files with 2140 additions and 103 deletions

View File

@ -0,0 +1,55 @@
import {DataSource} from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import {map, tap} from 'rxjs/operators';
import {merge, Observable, of as observableOf} from 'rxjs';
import {Product} from '../../core/product';
export class ProductListDataSource extends DataSource<Product> {
private dataObservable: Observable<Product[]>;
private filterValue: string;
constructor(private paginator: MatPaginator, private filter: Observable<string>, public data: Product[]) {
super();
this.filter = filter.pipe(
tap(x => this.filterValue = x)
);
}
connect(): Observable<Product[]> {
this.dataObservable = observableOf(this.data);
const dataMutations = [
this.dataObservable,
this.filter,
this.paginator.page
];
return merge(...dataMutations).pipe(
map((x: any) => {
return this.getPagedData(this.getFilteredData([...this.data]));
}),
tap((x: Product[]) => this.paginator.length = x.length)
);
}
disconnect() {
}
private getFilteredData(data: Product[]): Product[] {
const filter = (this.filterValue === undefined) ? '' : this.filterValue;
return filter.split(' ').reduce((p: Product[], c: string) => {
return p.filter(x => {
const productString = (
x.code + ' ' + x.name + ' ' + x.units + ' ' + x.productGroup + (x.isActive ? 'active' : 'deactive')
).toLowerCase();
return productString.indexOf(c) !== -1;
}
);
}, Object.assign([], data));
}
private getPagedData(data: Product[]) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
}

View File

@ -0,0 +1,97 @@
<mat-card>
<mat-card-title-group>
<mat-card-title>Products</mat-card-title>
<button mat-button mat-icon-button *ngIf="dataSource.data.length" (click)="exportCsv()">
<mat-icon>save_alt</mat-icon>
</button>
<a mat-button [routerLink]="['/products', 'new']">
<mat-icon>add_box</mat-icon>
Add
</a>
</mat-card-title-group>
<mat-card-content>
<form [formGroup]="form" fxLayout="column">
<div fxLayout="row" fxLayoutAlign="space-around start" fxLayout.lt-md="column" fxLayoutGap="20px"
fxLayoutGap.lt-md="0px">
<mat-form-field fxFlex>
<mat-label>Product Type</mat-label>
<mat-select placeholder="Product Group" formControlName="productGroup" (selectionChange)="filterOn(pg.id)">
<mat-option>--</mat-option>
<mat-option *ngFor="let pg of productGroups" [value]="pg.id">
{{ pg.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</form>
<mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let row"><a [routerLink]="['/products', row.id]">{{row.name}} ({{row.units}})</a></mat-cell>
</ng-container>
<!-- Price Column -->
<ng-container matColumnDef="price">
<mat-header-cell *matHeaderCellDef>Price</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.price | currency:'INR'}}</mat-cell>
</ng-container>
<!-- Product Group Column -->
<ng-container matColumnDef="productGroup">
<mat-header-cell *matHeaderCellDef>Product Group</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.productGroup}}</mat-cell>
</ng-container>
<!-- Tax Column -->
<ng-container matColumnDef="tax">
<mat-header-cell *matHeaderCellDef>Tax</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.tax}}</mat-cell>
</ng-container>
<!-- Info Column -->
<ng-container matColumnDef="info">
<mat-header-cell *matHeaderCellDef>Details</mat-header-cell>
<mat-cell *matCellDef="let row">
<div layout="row">
<div flex>
<mat-icon>
{{ row.hasHappyHour ? "sentiment_satisfied_alt" : "sentiment_dissatisfied" }}
</mat-icon>
<b> {{ row.hasHappyHour ? "Happy Hour" : "Regular" }}</b>
</div>
<div flex>
<mat-icon>
{{ row.isNotAvailable ? "pause" : "play_arrow" }}
</mat-icon>
<b> {{ row.isNotAvailable ? "Not Available" : "Available" }}</b>
</div>
<div flex>
<mat-icon>
{{ row.isActive ? "visibility" : "visibility_off" }}
</mat-icon>
<b> {{ row.isActive ? "Active" : "Deactivated" }}</b>
</div>
</div>
</mat-cell>
</ng-container>
<!-- Yield Column -->
<ng-container matColumnDef="quantity">
<mat-header-cell *matHeaderCellDef>Quantity</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.quantity | currency:'INR'}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator #paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="5000"
[pageSizeOptions]="[25, 50, 100, 250, 5000]">
</mat-paginator>
</mat-card-content>
</mat-card>

View File

@ -0,0 +1,23 @@
import {ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing';
import {ProductListComponent} from './product-list.component';
describe('ProductListComponent', () => {
let component: ProductListComponent;
let fixture: ComponentFixture<ProductListComponent>;
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [ProductListComponent]
})
.compileComponents();
fixture = TestBed.createComponent(ProductListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should compile', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,59 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { ProductListDataSource } from './product-list-datasource';
import { Product } from '../../core/product';
import { ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Observable } from 'rxjs';
import { ToCsvService } from "../../shared/to-csv.service";
@Component({
selector: 'app-product-list',
templateUrl: './product-list.component.html',
styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
dataSource: ProductListDataSource;
filter: Observable<string> = new Observable<string>();
form: FormGroup;
list: Product[];
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns: string[] = ['name', 'price', 'productGroup', 'tax', 'info', 'quantity'];
constructor(private route: ActivatedRoute, private fb: FormBuilder, private toCsv: ToCsvService) {
this.form = this.fb.group({
productGroup: ''
});
}
filterOn(val: string) {
console.log(val);
}
ngOnInit() {
this.route.data
.subscribe((data: { list: Product[] }) => {
this.list = data.list;
});
this.dataSource = new ProductListDataSource(this.paginator, this.filter, this.list);
}
exportCsv() {
const headers = {
Code: 'code',
Name: 'name',
Units: 'units',
Price: 'price',
ProductGroup: 'productGroup',
Tax: 'tax'
};
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement('a');
link.href = window.URL.createObjectURL(csvData);
link.setAttribute('download', 'products.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}