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

Commit 3b6f404

Browse files
author
Sebastian Superczynski
committed
fixed sorting and filtering
1 parent 6da0929 commit 3b6f404

File tree

6 files changed

+33
-52
lines changed

6 files changed

+33
-52
lines changed

src/app/components/base/base.component.html

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
<table class="ngx-table ngx-table-striped ngx-table-hover">
1313
<thead>
1414
<tr class="ngx-table__header" *ngIf="config.headerEnabled">
15-
<ng-container *ngFor="let key of columns">
15+
<ng-container *ngFor="let column of columns">
1616
<th class="ngx-table__header-cell"
17-
(click)="orderBy(key)">
18-
<div class="ngx-d-inline">{{ key }}</div>
19-
<span *ngIf="resource.order[key]==='asc' "
17+
(click)="orderBy(column['key'])">
18+
<div class="ngx-d-inline">{{ column['title'] }}</div>
19+
<span *ngIf="resource.order[column['key']]==='asc' "
2020
[style.display]="config.orderEnabled?'':'none' "
2121
class="ngx-icon ngx-icon-arrow-up">
2222
</span>
23-
<span *ngIf="resource.order[key]==='desc' "
23+
<span *ngIf="resource.order[column['key']]==='desc' "
2424
[style.display]="config.orderEnabled?'':'none' "
2525
class="ngx-icon ngx-icon-arrow-down">
2626
</span>
@@ -43,9 +43,9 @@
4343
</tr>
4444
<tr *ngIf="config.searchEnabled"
4545
class="ngx-table__sortHeader">
46-
<ng-container *ngFor="let key of columns">
46+
<ng-container *ngFor="let column of columns">
4747
<th>
48-
<table-header (update)="onSearch($event)" [key]="key"></table-header>
48+
<table-header (update)="onSearch($event)" [column]="column"></table-header>
4949
</th>
5050
</ng-container>
5151
<th *ngIf="config.additionalActions"></th>
@@ -65,12 +65,12 @@
6565
<tr *ngFor="let row of data | search : term | global : globalSearchTerm | paginate: { itemsPerPage: limit, currentPage: page, totalItems: count };
6666
let rowIndex = index"
6767
[class.ngx-table__table-row--selected]="rowIndex == selectedRow && !config.selectCell">
68-
<ng-container *ngFor="let key of columns; let colIndex = index">
69-
<td (click)="clickedCell($event, row, key, colIndex, rowIndex)"
68+
<ng-container *ngFor="let column of columns; let colIndex = index">
69+
<td (click)="clickedCell($event, row, column['key'], colIndex, rowIndex)"
7070
[class.ngx-table__table-col--selected]="colIndex == selectedCol && !config.selectCell"
7171
[class.ngx-table__table-cell--selected]="colIndex == selectedCol && rowIndex == selectedRow && !config.selectCol && !config.selectRow"
7272
>
73-
<div>{{ row[key] }}</div>
73+
<div>{{ row[column['key']] }}</div>
7474
</td>
7575
</ng-container>
7676
<td *ngIf="config.additionalActions"></td>

src/app/components/base/base.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class BaseComponent implements OnInit, OnChanges, AfterViewInit {
7979
orderBy(key: string) {
8080
if (ConfigService.config.orderEnabled || !ConfigService.config.serverPagination) {
8181
this.data = this.resource.sortBy(key, null);
82+
this.data = [...this.data];
8283
}
8384
this.onOrder(key);
8485
}
+13-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
import {Component, Input, Output, EventEmitter, OnInit} from '@angular/core';
2-
import {FiltersService} from "../../services/filters-service";
1+
import { Component, Input, Output, EventEmitter } from '@angular/core';
32

43
@Component({
54
selector: 'table-header',
65
template: `
7-
<label for="search_{{ key }}">
8-
<input type="text"
9-
id="search_{{ key }}"
10-
aria-label="Search"
11-
placeholder="Search for {{ key }}"
12-
class="ngx-table__header-search ngx-form-input ngx-input-sm"
13-
#input
14-
(input)="update.emit({value: input.value, key: key})"
15-
>
16-
</label>`
6+
<label for="search_{{ column['key'] }}">
7+
<input type="text"
8+
id="search_{{ column['key'] }}"
9+
aria-label="Search"
10+
placeholder="Search {{ column['key'] }}"
11+
class="ngx-table__header-search ngx-form-input ngx-input-sm"
12+
#input
13+
(input)="update.emit({value: input.value, key: column['key']})"
14+
>
15+
</label>`
1716
})
1817

19-
export class HeaderComponent implements OnInit {
20-
constructor(public filtersService:FiltersService) {
21-
}
22-
23-
@Input() key;
18+
export class HeaderComponent {
19+
@Input() column;
2420
@Output() update = new EventEmitter();
25-
26-
ngOnInit() {
27-
// this.update.emit({});
28-
}
2921
}

src/app/pipes/header-pipe.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Pipe, PipeTransform } from '@angular/core';
2-
import { FiltersService } from "../services/filters-service";
3-
import { ResourceService } from "../services/resource-service";
2+
import { FiltersService } from '../services/filters-service';
3+
import { ResourceService } from '../services/resource-service';
44

55
@Pipe({
66
name: 'search'
@@ -12,7 +12,7 @@ export class SearchPipe implements PipeTransform {
1212
}
1313

1414
transform(value, filters) {
15-
if (typeof value === "undefined") {
15+
if (typeof value === 'undefined') {
1616
return;
1717
}
1818
this.resource.data = value.slice();
@@ -26,17 +26,17 @@ export class SearchPipe implements PipeTransform {
2626
value.forEach((item) => {
2727
for (const filterKey in filtersArr) {
2828
if (filtersArr.hasOwnProperty(filterKey)) {
29-
let element;
30-
if (typeof item[filterKey] === "string") {
29+
let element = '';
30+
if (typeof item[filterKey] === 'string') {
3131
element = item[filterKey].toLocaleLowerCase();
3232
}
33-
if (typeof item[filterKey] === "object") {
33+
if (typeof item[filterKey] === 'object') {
3434
element = JSON.stringify(item[filterKey]);
3535
}
36-
if (typeof item[filterKey] === "number") {
36+
if (typeof item[filterKey] === 'number') {
3737
element = item[filterKey].toString();
3838
}
39-
if (typeof item[filterKey] === "boolean") {
39+
if (typeof item[filterKey] === 'boolean') {
4040
element = item[filterKey].toString();
4141
}
4242
if (element.indexOf(filtersArr[filterKey].toLocaleLowerCase()) === -1) {

src/app/services/filters-service.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { Injectable } from '@angular/core';
2-
import { ResourceService } from './resource-service';
32
import { isNumeric } from 'rxjs/util/isNumeric';
43

54
@Injectable()
65
export class FiltersService {
76

8-
constructor(public resource: ResourceService) {
9-
}
10-
117
public filters: Array<any> = [];
128

139
update(k: string, v: string) {

src/app/services/resource-service.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import { Injectable, EventEmitter } from '@angular/core';
1+
import { Injectable } from '@angular/core';
22

33
@Injectable()
44
export class ResourceService {
5-
private _pipedDataEmitter;
65
public data: Array<any> = [];
76
public key: string;
87
public order: Array<any> = [];
98

10-
public getPipedData(): EventEmitter<any> {
11-
if (!this._pipedDataEmitter) {
12-
this._pipedDataEmitter = new EventEmitter();
13-
}
14-
return this._pipedDataEmitter;
15-
}
16-
179
public sortBy(key: string, order: string | null): Array<any> {
1810
this.key = key;
1911
if (Object.keys(this.order).length === 0) {

0 commit comments

Comments
 (0)