Skip to content

Commit

Permalink
feat(entity): added column templates and sticky column options
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Dec 8, 2018
1 parent e525738 commit 46abb0f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ export class AccountsTableComponent extends EntitiesComponent<Account, AccountSe
**/
openPopUp(entity: Account) {
let isNew = false;
let id;
if (!entity) {
isNew = true;
entity = this.getNewEntity();
} else {
id = entity.id;
}
const title = isNew ? 'Add Member' : 'Update Member';

Expand All @@ -122,13 +125,7 @@ export class AccountsTableComponent extends EntitiesComponent<Account, AccountSe
.pipe(
filter(res => res !== false),
// tap(res => console.log(res)),
map((res: Account) => {
if (!isNew) {
res.id = entity.id;
}
return res;
}),
concatMap((res: Account) => super.updateOrCreate(res, isNew)),
concatMap((res: Account) => super.updateOrCreate(res, id)),
)
.subscribe(
_ => {
Expand Down
20 changes: 14 additions & 6 deletions libs/shared/src/lib/containers/entity/entity.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<!-- FIXME Only used for GRID Demo -->
<ngx-breadcrumbs title="Grid" [crumbs]="crumbs"></ngx-breadcrumbs>
<!-- FIXME End -->

<div fxLayout="column">
<mat-card class="mat-elevation-z8">
<mat-toolbar *ngIf="showToolbar">
Expand Down Expand Up @@ -87,9 +83,21 @@
</mat-cell>
</ng-container>

<ng-container *ngFor="let column of columns" [matColumnDef]="column.property">
<ng-container
*ngFor="let column of columns"
[matColumnDef]="column.property"
[sticky]="column.sticky === 'start'"
[stickyEnd]="column.sticky === 'end'"
>
<mat-header-cell *matHeaderCellDef mat-sort-header> {{ column.header }} </mat-header-cell>
<mat-cell *matCellDef="let row"> {{ column.displayFn(row) }} </mat-cell>
<mat-cell *matCellDef="let row">
<ng-container *ngIf="column.template; else noTemplate">
<ng-container
*ngTemplateOutlet="column.template; context: { $implicit: row, column: row[column.property] }"
></ng-container>
</ng-container>
<ng-template #noTemplate> {{ column.displayFn(row) }} </ng-template>
</mat-cell>
</ng-container>

<ng-container *ngIf="showActionColumn" [matColumnDef]="actionColumn" sticky>
Expand Down
6 changes: 0 additions & 6 deletions libs/shared/src/lib/containers/entity/entity.component.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
:host {
display: block;
padding: 1.5%;
position: relative;
}

mat-toolbar {
padding: 8px 24px 0;
}
Expand Down
8 changes: 4 additions & 4 deletions libs/shared/src/lib/containers/entity/entity.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export abstract class EntitiesComponent<TEntity extends Entity, TService extends
return this.entityService.delete(item.id).pipe(concatMap(_ => this.update()));
}

updateOrCreate(entity: TEntity, isNew: boolean) {
if (isNew) {
return this.entityService.post(entity).pipe(concatMap(_ => this.update()));
updateOrCreate(entity: TEntity, id: number) {
if (id) {
return this.entityService.put(id, entity).pipe(concatMap(_ => this.update()));
} else {
return this.entityService.put(entity).pipe(concatMap(_ => this.update()));
return this.entityService.post(entity).pipe(concatMap(_ => this.update()));
}
}

Expand Down
4 changes: 4 additions & 0 deletions libs/shared/src/lib/containers/entity/entity.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TemplateRef } from '@angular/core';

export abstract class Entity {
abstract get id(): number | string;
[key: string]: any;
Expand All @@ -10,6 +12,8 @@ export class EntityColumnDef<T> {
readonly property: string;
readonly header = this.property;
public visible = true;
readonly sticky?: 'start' | 'end';
readonly template?: TemplateRef<any>;
readonly displayFn = (entity: T) => entity[this.property];

public constructor(init?: Partial<EntityColumnDef<T>>) {
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/src/lib/containers/entity/entity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export abstract class EntityService<T extends Entity> {
);
}

put(entity: T) {
put(id: number | string, entity: T) {
console.log(entity);
this.loadingSubject.next(true);
return this.httpClient.put(`${this.baseUrl}/${this.entityPath}`, entity).pipe(
return this.httpClient.put(`${this.baseUrl}/${this.entityPath}/${id}`, entity).pipe(
catchError(this.handleError),
finalize(() => this.loadingSubject.next(false)),
);
Expand Down

0 comments on commit 46abb0f

Please sign in to comment.