Skip to content

Commit

Permalink
refactor(theme-shared): add trackByFn
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmet-erim committed Jan 6, 2020
1 parent ea2eeb3 commit a9df4c7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './breadcrumb/breadcrumb.component';
export * from './button/button.component';
export * from './chart/chart.component';
export * from './confirmation/confirmation.component';
export * from './loading/loading.component';
export * from './loader-bar/loader-bar.component';
export * from './modal/modal.component';
export * from './paginator/paginator.component';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
><span class="ui-paginator-icon pi pi-caret-left"></span></a
><span class="ui-paginator-pages"
><a
*ngFor="let page of pageArray"
*ngFor="let page of pageArray; trackBy: trackByFn"
(click)="changePage(page)"
class="ui-paginator-page ui-paginator-element ui-state-default ui-corner-all"
[class.ui-state-active]="page === value"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';
import { Component, Input, OnInit, Output, EventEmitter, TrackByFunction } from '@angular/core';

@Component({
selector: 'abp-paginator',
Expand All @@ -11,8 +11,7 @@ export class PaginatorComponent implements OnInit {
return this._value;
}
set value(newValue: number) {
if (newValue < 1) return;
else if (newValue > this.totalPages) return;
if (newValue < 1 || newValue > this.totalPages || newValue === this._value) return;

this._value = newValue;
this.valueChange.emit(newValue);
Expand All @@ -36,6 +35,8 @@ export class PaginatorComponent implements OnInit {
}
}

trackByFn: TrackByFunction<number> = (_, page) => page;

ngOnInit() {
if (!this.value || this.value < 1 || this.value > this.totalPages) {
this.value = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
</ng-template>

<ng-template #colGroup>
<colgroup>
<ng-container *ngTemplateOutlet="colgroupTemplate"></ng-container>
</colgroup>
<ng-container *ngTemplateOutlet="colgroupTemplate"></ng-container>
</ng-template>

<ng-template #head>
Expand All @@ -60,7 +58,7 @@
<ng-template #body>
<tbody class="ui-table-tbody">
<ng-container
*ngFor="let val of slicedValue"
*ngFor="let val of slicedValue; trackBy: trackByFn"
[ngTemplateOutlet]="bodyTemplate"
[ngTemplateOutletContext]="{ $implicit: val }"
></ng-container>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Component, OnInit, Input, TemplateRef, Output, EventEmitter } from '@angular/core';
import {
Component,
OnInit,
Input,
TemplateRef,
Output,
EventEmitter,
TrackByFunction,
} from '@angular/core';

@Component({
selector: 'abp-table',
Expand Down Expand Up @@ -28,13 +36,20 @@ export class TableComponent implements OnInit {
@Input()
rows: number;

@Input()
trackingProp = 'id';

@Output()
readonly pageChange = new EventEmitter<number>();

page = 1;

bodyScrollLeft = 0;

trackByFn: TrackByFunction<any> = (_, value) => {
return typeof value === 'object' ? value[this.trackingProp] || value : value;
};

@Input()
get totalRecords(): number {
return this._totalRecords || this.value.length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './loading.directive';
export * from './table-sort.directive';
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { httpErrorConfigFactory, HTTP_ERROR_CONFIG } from './tokens/http-error.t
import { DateParserFormatter } from './utils/date-parser-formatter';
import { chartJsLoaded$ } from './utils/widget-utils';
import { PaginatorComponent } from './components/paginator/paginator.component';
import { LoadingComponent } from './components/loading/loading.component';
import { LoadingDirective } from './directives/loading.directive';

export function appendScript(injector: Injector) {
const fn = () => {
Expand All @@ -45,12 +47,14 @@ export function appendScript(injector: Injector) {
ConfirmationComponent,
HttpErrorWrapperComponent,
LoaderBarComponent,
LoadingComponent,
ModalComponent,
PaginatorComponent,
TableComponent,
TableEmptyMessageComponent,
ToastComponent,
SortOrderIconComponent,
LoadingDirective,
TableSortDirective,
],
exports: [
Expand All @@ -59,16 +63,18 @@ export function appendScript(injector: Injector) {
ChartComponent,
ConfirmationComponent,
LoaderBarComponent,
LoadingComponent,
ModalComponent,
PaginatorComponent,
TableComponent,
TableEmptyMessageComponent,
ToastComponent,
SortOrderIconComponent,
LoadingDirective,
TableSortDirective,
],
providers: [DatePipe],
entryComponents: [HttpErrorWrapperComponent],
entryComponents: [HttpErrorWrapperComponent, LoadingComponent],
})
export class ThemeSharedModule {
constructor(private errorHandler: ErrorHandler) {}
Expand Down

0 comments on commit a9df4c7

Please sign in to comment.