Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit c0b52b8

Browse files
Merge pull request #234 from ssuperczynski/pinn-fixes
added back ChangeDetectionStrategy
2 parents 282b026 + fe32013 commit c0b52b8

File tree

10 files changed

+54
-209
lines changed

10 files changed

+54
-209
lines changed

projects/ngx-easy-table/src/lib/components/base/base.component.html

+13-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
</th>
2525
<ng-container *ngFor="let column of columns; let colIndex = index">
2626
<th class="ngx-table__header-cell"
27+
[class.pinned-left]="column.pinned"
28+
[ngClass]="column.cssClass && column.cssClass.includeHeader ? column.cssClass.name : ''"
29+
[style.left]="pinnedWidth(column.pinned, colIndex)"
2730
#th
28-
[attr.width]="getColumnWidth(column)"
31+
[style.width]="getColumnWidth(column)"
2932
(mousedown)="onMouseDown($event, th)"
3033
(mouseup)="onMouseUp($event)"
3134
(mousemove)="onMouseMove($event)">
3235
<div (click)="orderBy(column)" [class.pointer]="isOrderEnabled(column)">
3336
<div class="ngx-table__header-title">{{ column.title }}<span>&nbsp;</span>
34-
<i class="ngx-icon ngx-icon-pin" *ngIf="styleService.pinnedColumns.has(colIndex + 1)"></i>
37+
<i class="ngx-icon ngx-icon-pin" *ngIf="column.pinned"></i>
3538
<div [style.display]="config.orderEnabled ? 'inline' : 'none' ">
3639
<span *ngIf="sortKey === column.key && this.sortState.get(sortKey) === ''"> -</span>
3740
<i *ngIf="sortKey === column.key && this.sortState.get(sortKey) === 'asc'"
@@ -66,8 +69,11 @@
6669
<tr *ngIf="config.searchEnabled && !filtersTemplate"
6770
class="ngx-table__sort-header">
6871
<th *ngIf="config.checkboxes"></th>
69-
<ng-container *ngFor="let column of columns">
70-
<th>
72+
<ng-container *ngFor="let column of columns; let colIndex = index">
73+
<th
74+
[ngClass]="column.cssClass && column.cssClass.includeHeader ? column.cssClass.name : ''"
75+
[class.pinned-left]="column.pinned"
76+
[style.left]="pinnedWidth(column.pinned, colIndex)">
7177
<table-header
7278
*ngIf="getColumnDefinition(column)"
7379
(update)="onSearch($event)"
@@ -150,6 +156,9 @@
150156
<td (click)="onClick($event, row, column.key, colIndex, rowIndex)"
151157
(contextmenu)="onRowContextMenu($event, row, column.key, colIndex, rowIndex)"
152158
(dblclick)="onDoubleClick($event, row, column.key, colIndex, rowIndex)"
159+
[class.pinned-left]="column.pinned"
160+
[ngClass]="column.cssClass ? column.cssClass.name : ''"
161+
[style.left]="pinnedWidth(column.pinned, colIndex)"
153162
[class.ngx-table__table-col--selected]="colIndex == selectedCol && !config.selectCell"
154163
[class.ngx-table__table-cell--selected]="colIndex == selectedCol && rowIndex == selectedRow && !config.selectCol && !config.selectRow"
155164
>

projects/ngx-easy-table/src/lib/components/base/base.component.ts

+9-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
22
import {
3+
ChangeDetectionStrategy,
34
ChangeDetectorRef,
45
Component,
56
ContentChild,
@@ -14,16 +15,7 @@ import {
1415
ViewChild,
1516
} from '@angular/core';
1617

17-
import {
18-
Columns,
19-
Config,
20-
Event,
21-
API,
22-
Pagination,
23-
ColumnKeyType,
24-
TableMouseEvent,
25-
ApiType,
26-
} from '../..';
18+
import { API, ApiType, ColumnKeyType, Columns, Config, Event, Pagination, TableMouseEvent } from '../..';
2719
import { DefaultConfigService } from '../../services/config-service';
2820
import { PaginationComponent, PaginationRange } from '../pagination/pagination.component';
2921
import { GroupRowsService } from '../../services/group-rows.service';
@@ -40,6 +32,7 @@ interface RowContextMenuPosition {
4032
selector: 'ngx-table',
4133
providers: [DefaultConfigService, GroupRowsService, StyleService],
4234
templateUrl: './base.component.html',
35+
changeDetection: ChangeDetectionStrategy.OnPush,
4336
})
4437
export class BaseComponent implements OnInit, OnChanges {
4538
@ViewChild('paginationComponent') private paginationComponent: PaginationComponent;
@@ -176,8 +169,6 @@ export class BaseComponent implements OnInit, OnChanges {
176169
key: this.sortKey,
177170
order: this.sortState.get(this.sortKey),
178171
};
179-
this.setColumnPinned();
180-
this.setColumnClass();
181172
this.emitEvent(Event.onOrder, value);
182173
}
183174

@@ -233,8 +224,6 @@ export class BaseComponent implements OnInit, OnChanges {
233224
if (!DefaultConfigService.config.serverPagination) {
234225
this.term = $event;
235226
}
236-
this.setColumnPinned();
237-
this.setColumnClass();
238227
this.emitEvent(Event.onSearch, $event);
239228
}
240229

@@ -248,8 +237,6 @@ export class BaseComponent implements OnInit, OnChanges {
248237
onPagination(pagination: PaginationRange): void {
249238
this.page = pagination.page;
250239
this.limit = pagination.limit;
251-
this.setColumnPinned();
252-
this.setColumnClass();
253240
this.emitEvent(Event.onPagination, pagination);
254241
}
255242

@@ -387,47 +374,22 @@ export class BaseComponent implements OnInit, OnChanges {
387374
this.emitEvent(Event.onRowContextMenu, value);
388375
}
389376

377+
pinnedWidth(pinned: boolean, column: number): string {
378+
if (pinned) {
379+
return 150 * column + 'px'; //
380+
}
381+
}
382+
390383
private doApplyData(data) {
391384
const order = this.columns.find((c) => !!c.orderBy);
392385
if (order) {
393386
this.sortState.set(this.sortKey, (order.orderBy === 'asc') ? 'desc' : 'asc');
394387
this.orderBy(order);
395388
} else {
396389
this.data = [...data.currentValue];
397-
this.setColumnClass();
398-
this.setColumnPinned();
399390
}
400391
}
401392

402-
private setColumnClass() {
403-
this.cdr.detectChanges();
404-
const colClass = this.columns.filter((c) => !!c.cssClass);
405-
colClass.forEach((col) => {
406-
this.bindApi({
407-
type: API.setColumnClass,
408-
value: {
409-
column: this.columns.indexOf(col) + 1,
410-
className: col.cssClass.name,
411-
includeHeader: col.cssClass.includeHeader,
412-
},
413-
});
414-
});
415-
}
416-
417-
private setColumnPinned() {
418-
this.cdr.detectChanges();
419-
const pinned = this.columns.filter((c) => !!c.pinned);
420-
pinned.forEach((pin) => {
421-
this.bindApi({
422-
type: API.setColumnPinned,
423-
value: {
424-
column: this.columns.indexOf(pin) + 1,
425-
pinned: pin.pinned,
426-
},
427-
});
428-
});
429-
}
430-
431393
onDrop(event: CdkDragDrop<string[]>) {
432394
this.emitEvent(Event.onRowDrop, event);
433395
moveItemInArray(this.data, event.previousIndex, event.currentIndex);
@@ -477,18 +439,6 @@ export class BaseComponent implements OnInit, OnChanges {
477439
this.styleService.setRowClass(event.value);
478440
this.cdr.detectChanges();
479441
break;
480-
case API.setColumnClass:
481-
if (Array.isArray(event.value)) {
482-
event.value.forEach((val) => this.styleService.setColumnClassStyle(val));
483-
break;
484-
}
485-
this.styleService.setColumnClassStyle(event.value);
486-
this.cdr.detectChanges();
487-
break;
488-
case API.setColumnPinned:
489-
this.styleService.setColumnPinnedStyle(event.value.column, event.value.pinned);
490-
this.cdr.detectChanges();
491-
break;
492442
case API.setCellClass:
493443
if (Array.isArray(event.value)) {
494444
event.value.forEach((val) => this.styleService.setCellClass(val));

projects/ngx-easy-table/src/lib/model/api.ts

-5
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ export enum API {
1313
setPaginationDisplayLimit = 'setPaginationDisplayLimit',
1414
setTableClass = 'setTableClass',
1515
setRowClass = 'setRowClass',
16-
setColumnClass = 'setColumnClass',
17-
setColumnPinned = 'setColumnPinned',
1816
setCellClass = 'setCellClass',
1917
setRowStyle = 'setRowStyle',
2018
setCellStyle = 'setCellStyle',
2119
sortBy = 'sortBy',
2220
}
2321

2422
export type rowClass = { row: number, className: string };
25-
export type columnClass = { column: number, className: string, includeHeader: boolean };
2623
export type cellClass = { row: number, cell: number | string, className: string };
2724
export type rowStyle = { row: number, attr: string, value: string };
2825
export type cellStyle = { row: number, cell: number, attr: string, value: string };
@@ -42,8 +39,6 @@ export type ApiType =
4239
| { type: API.setPaginationDisplayLimit; value: number; }
4340
| { type: API.setTableClass; value: string | null; }
4441
| { type: API.setRowClass; value: rowClass | rowClass[] }
45-
| { type: API.setColumnClass; value: columnClass | columnClass[] }
46-
| { type: API.setColumnPinned; value: { column: number, pinned: boolean } }
4742
| { type: API.setCellClass; value: cellClass | cellClass[] }
4843
| { type: API.setRowStyle; value: rowStyle | rowStyle[] }
4944
| { type: API.setCellStyle; value: cellStyle | cellStyle[] }
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Injectable } from '@angular/core';
2-
import { cellClass, cellStyle, columnClass, rowClass, rowStyle } from '..';
2+
import { cellClass, cellStyle, rowClass, rowStyle } from '..';
33

44
@Injectable()
55
export class StyleService {
6-
public pinnedColumns = new Set<number>();
7-
86
setRowClass(val: rowClass): void {
97
const selector = `#table > tbody > tr:nth-child(${val.row})`;
108
const row: HTMLTableRowElement = document.querySelector(selector);
@@ -13,47 +11,6 @@ export class StyleService {
1311
}
1412
}
1513

16-
setColumnClassStyle(val: columnClass): void {
17-
if (val.column < 1) {
18-
console.error('Column number need to be 1 or greater');
19-
return;
20-
}
21-
if (val.includeHeader) {
22-
const headerRows = document.querySelectorAll(`#table > thead tr > th:nth-child(${val.column})`);
23-
[].forEach.call(headerRows, (header) => {
24-
header.className = val.className;
25-
});
26-
}
27-
const columns = document.querySelectorAll(`#table > tbody tr > td:nth-child(${val.column})`);
28-
[].forEach.call(columns, (column) => {
29-
column.className = val.className;
30-
});
31-
}
32-
33-
setColumnPinnedStyle(column: number, pinned: boolean): void {
34-
if (column < 1) {
35-
console.error('Column number need to be 1 or greater');
36-
return;
37-
}
38-
this.updatePinnedColumns(column, pinned);
39-
let leftMargin = 0;
40-
this.pinnedColumns.forEach((col) => {
41-
if (col < column) {
42-
const currentColumn = document.querySelector(`#table tr:nth-child(1) > td:nth-child(${col})`);
43-
leftMargin = leftMargin + currentColumn.clientWidth;
44-
}
45-
});
46-
const cols = document.querySelectorAll(`#table tr > td:nth-child(${column}),th:nth-child(${column})`);
47-
[].forEach.call(cols, (col: HTMLTableElement) => {
48-
if (pinned) {
49-
col.className = 'ngx-table__header-cell pinned-left';
50-
col.style.left = `${leftMargin}px`;
51-
return;
52-
}
53-
col.className = 'ngx-table__header-cell';
54-
});
55-
}
56-
5714
setCellClass(val: cellClass): void {
5815
const selector = `#table > tbody > tr:nth-child(${val.row}) > td:nth-child(${val.cell})`;
5916
const cell: HTMLTableCellElement = document.querySelector(selector);
@@ -79,12 +36,4 @@ export class StyleService {
7936
cell.style[val.attr] = val.value;
8037
}
8138
}
82-
83-
private updatePinnedColumns(column: number, pinned: boolean) {
84-
if (pinned) {
85-
this.pinnedColumns.add(column);
86-
} else {
87-
this.pinnedColumns.delete(column);
88-
}
89-
}
9039
}

src/app/demo/group-rows/group-rows.component.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component } from '@angular/core';
2-
import * as faker from 'faker';
2+
import { random, company, name } from 'faker';
33
import { ConfigService } from './configuration.service';
44
import { Columns } from 'ngx-easy-table';
55

@@ -34,16 +34,16 @@ export class GroupRowsComponent {
3434

3535
private static generateData() {
3636
return Array(31).fill('').map((_, key) => ({
37-
amount: faker.random.number(300),
37+
amount: random.number(300),
3838
debit: 300,
39-
company: faker.company.companyName(),
40-
name: `${faker.name.firstName()} ${faker.name.lastName()}`,
39+
company: company.companyName(),
40+
name: `${name.firstName()} ${name.lastName()}`,
4141
isActive: key % 2 === 1,
4242
}));
4343
}
4444

45-
onChange(name): void {
46-
this.groupBy = name;
45+
onChange(groupBy: string): void {
46+
this.groupBy = groupBy;
4747
}
4848

4949
showCount(group, key: string) {

src/app/demo/horizontal-scroll/horizontal-scroll.component.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, NgZone, OnInit } from '@angular/core';
2-
import * as faker from 'faker';
2+
import { phone, company, random, name } from 'faker';
33
import { ConfigService } from './configuration.service';
44
import { Columns } from 'ngx-easy-table';
55

@@ -37,21 +37,21 @@ export class HorizontalScrollComponent implements OnInit {
3737
private static generateData() {
3838
return Array(20).fill('').map(() => {
3939
return {
40-
phone: faker.phone.phoneNumberFormat(),
41-
age: faker.random.number({ min: 15, max: 70 }).toString(),
42-
company: faker.company.companyName(),
43-
name: `${faker.name.firstName()} ${faker.name.lastName()}`,
44-
isActive: faker.random.boolean(),
45-
company2: faker.company.companyName(),
46-
company3: faker.company.companyName(),
47-
company4: faker.company.companyName(),
48-
company5: faker.company.companyName(),
49-
company6: faker.company.companyName(),
50-
company7: faker.company.companyName(),
51-
company8: faker.company.companyName(),
52-
company9: faker.company.companyName(),
53-
company10: faker.company.companyName(),
54-
company11: faker.company.companyName(),
40+
phone: phone.phoneNumberFormat(),
41+
age: random.number({ min: 15, max: 70 }).toString(),
42+
company: company.companyName(),
43+
name: `${name.firstName()} ${name.lastName()}`,
44+
isActive: random.boolean(),
45+
company2: company.companyName(),
46+
company3: company.companyName(),
47+
company4: company.companyName(),
48+
company5: company.companyName(),
49+
company6: company.companyName(),
50+
company7: company.companyName(),
51+
company8: company.companyName(),
52+
company9: company.companyName(),
53+
company10: company.companyName(),
54+
company11: company.companyName(),
5555
};
5656
});
5757
}

src/app/demo/pagination-range/pagination-range.component.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component } from '@angular/core';
2-
import * as faker from 'faker';
2+
import { phone, random, company, name } from 'faker';
33
import { ConfigService } from './configuration.service';
44
import { Columns } from 'ngx-easy-table';
55

@@ -29,11 +29,11 @@ export class PaginationRangeComponent {
2929
private static generateData() {
3030
return Array(170).fill('').map(() => {
3131
return {
32-
phone: faker.phone.phoneNumberFormat(),
33-
age: faker.random.number({ min: 15, max: 70 }).toString(),
34-
company: faker.company.companyName(),
35-
name: `${faker.name.firstName()} ${faker.name.lastName()}`,
36-
isActive: faker.random.boolean(),
32+
phone: phone.phoneNumberFormat(),
33+
age: random.number({ min: 15, max: 70 }).toString(),
34+
company: company.companyName(),
35+
name: `${name.firstName()} ${name.lastName()}`,
36+
isActive: random.boolean(),
3737
};
3838
});
3939
}

0 commit comments

Comments
 (0)