Skip to content

Commit 1fca780

Browse files
Refactor data source (#65)
* refactor data source
1 parent 0722bbf commit 1fca780

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

projects/table-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mx-table-builder",
3-
"version": "0.3.10",
3+
"version": "0.3.11",
44
"peerDependencies": {
55
"@angular/common": "~11.0.0",
66
"@angular/core": "~11.0.0",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Observable } from 'rxjs';
2+
import { orderBy } from 'lodash';
3+
import { direc } from '../components/generic-table/generic-table.component';
4+
import { MultiSortDirective } from '../directives/multi-sort.directive';
5+
import {MatTableObservableDataSource} from './MatTableObservableDataSource'
6+
7+
export class GenericTableDataSource<T> extends MatTableObservableDataSource<T>
8+
{
9+
constructor(dataSrc: Observable<T[]>)
10+
{
11+
super(dataSrc);
12+
this.sortData = (data: {}[], sort: MultiSortDirective) =>
13+
orderBy(data, sort.rules.map(r => r.active), sort.rules.map(r => r.direction as direc ));
14+
}
15+
}

projects/table-builder/src/lib/classes/MatTableObservableDataSource.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { MatTableDataSource } from '@angular/material/table';
22
import { Observable, Subscription } from 'rxjs';
3-
import { direc } from '../components/generic-table/generic-table.component';
4-
import { MultiSortDirective } from '../directives/multi-sort.directive';
5-
import { orderBy } from 'lodash';
63

74
export class MatTableObservableDataSource<T> extends MatTableDataSource<T> {
85
subscription: Subscription;
96
constructor(private dataSrc: Observable<T[]>) {
107
super([]);
11-
this.sortData = (data: T[], sort: MultiSortDirective) => !sort.rules ?
12-
super.sortData(data,sort)
13-
: orderBy(data, sort.rules.map(r => r.active), sort.rules.map(r => r.direction as direc ));
148
}
159

1610
connect() {

projects/table-builder/src/lib/components/generic-table/generic-table.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import { MatSort } from '@angular/material/sort';
1818
import { MatRowDef, MatTable } from '@angular/material/table';
1919
import { Observable } from 'rxjs';
2020
import * as _ from 'lodash';
21-
import { MatTableObservableDataSource } from '../../classes/MatTableObservableDataSource';
2221
import { SelectionModel } from '@angular/cdk/collections';
2322
import { TableStore } from '../../classes/table-store';
2423
import { tap, map, distinct } from 'rxjs/operators';
2524
import { ColumnBuilderComponent } from '../column-builder/column-builder.component';
2625
import { ColumnInfo } from '../table-container/table-container';
2726
import { Dictionary } from '../../interfaces/dictionary';
27+
import { GenericTableDataSource } from '../../classes/GenericTableDataSource';
2828

2929
@Component({
3030
selector: 'tb-generic-table',
@@ -51,7 +51,7 @@ export class GenericTableComponent implements OnInit {
5151

5252
currentColumns: string[];
5353
selection: SelectionModel<any>;
54-
dataSource: MatTableObservableDataSource<any>;
54+
dataSource: GenericTableDataSource<any>;
5555
keys: string [] = [];
5656
factory: ComponentFactory<ColumnBuilderComponent> ;
5757
injector: Injector;
@@ -107,7 +107,7 @@ export class GenericTableComponent implements OnInit {
107107
}
108108

109109
createDataSource() {
110-
this.dataSource = new MatTableObservableDataSource(
110+
this.dataSource = new GenericTableDataSource(
111111
this.data$.pipe(tap((d) => this.selection.clear() ))
112112
);
113113
this.dataSource.sort = this.sort;

0 commit comments

Comments
 (0)