start of list filtering

This commit is contained in:
Jesse Lucas 2020-03-30 17:02:59 -04:00
parent dae1f990a5
commit 42fa03aa4a
No known key found for this signature in database
GPG Key ID: 9810010C7FDCD3BB
8 changed files with 53 additions and 149 deletions

View File

@ -5,6 +5,7 @@ import { HttpClientModule, HttpClientXsrfModule } from '@angular/common/http';
import { MatTableModule } from '@angular/material/table'; import { MatTableModule } from '@angular/material/table';
import { MatPaginatorModule } from '@angular/material/paginator'; import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort'; import { MatSortModule } from '@angular/material/sort';
import { MatInputModule } from '@angular/material/input';
import { MatButtonToggleModule } from '@angular/material/button-toggle'; import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatCardModule } from '@angular/material/card'; import { MatCardModule } from '@angular/material/card';
import { FlexLayoutModule } from '@angular/flex-layout'; import { FlexLayoutModule } from '@angular/flex-layout';
@ -15,7 +16,6 @@ import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { StatusListComponent } from './lists/status-list/status-list.component'; import { StatusListComponent } from './lists/status-list/status-list.component';
import { FolderListComponent } from './lists/folder-list/folder-list.component';
import { DeviceListComponent } from './lists/device-list/device-list.component'; import { DeviceListComponent } from './lists/device-list/device-list.component';
import { DonutChartComponent } from './charts/donut-chart/donut-chart.component'; import { DonutChartComponent } from './charts/donut-chart/donut-chart.component';
import { DashboardComponent } from './dashboard/dashboard.component'; import { DashboardComponent } from './dashboard/dashboard.component';
@ -28,23 +28,25 @@ import { deviceID } from './api-utils';
import { environment } from '../environments/environment'; import { environment } from '../environments/environment';
import { ChartItemComponent } from './charts/chart-item/chart-item.component'; import { ChartItemComponent } from './charts/chart-item/chart-item.component';
import { ChartComponent } from './charts/chart/chart.component'; import { ChartComponent } from './charts/chart/chart.component';
import { FolderListComponent } from './lists/folder-list/folder-list.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
StatusListComponent, StatusListComponent,
FolderListComponent,
DeviceListComponent, DeviceListComponent,
ListToggleComponent, ListToggleComponent,
DashboardComponent, DashboardComponent,
DonutChartComponent, DonutChartComponent,
ChartComponent, ChartComponent,
ChartItemComponent, ChartItemComponent,
FolderListComponent,
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
AppRoutingModule, AppRoutingModule,
BrowserAnimationsModule, BrowserAnimationsModule,
MatInputModule,
MatTableModule, MatTableModule,
MatPaginatorModule, MatPaginatorModule,
MatSortModule, MatSortModule,

View File

@ -1,22 +1,20 @@
<div class="{{elevation}}"> <table mat-table class="full-width-table" matSort aria-label="Elements">
<table mat-table class="full-width-table" matSort aria-label="Elements"> <!-- Id Column -->
<!-- Id Column --> <ng-container matColumnDef="id">
<ng-container matColumnDef="id"> <th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th> <td mat-cell *matCellDef="let row">{{row.deviceID}}</td>
<td mat-cell *matCellDef="let row">{{row.deviceID}}</td> </ng-container>
</ng-container>
<!-- Name Column --> <!-- Name Column -->
<ng-container matColumnDef="name"> <ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th> <th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td> <td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container> </ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table> </table>
<mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50" <mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50"
[pageSizeOptions]="[25, 50, 100, 250]"> [pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator> </mat-paginator>
</div>

View File

@ -1,13 +1,10 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'; import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator'; import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort'; import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table'; import { MatTable, MatTableDataSource } from '@angular/material/table';
import { DeviceListDataSource } from './device-list-datasource';
import Device from '../../device'; import Device from '../../device';
import { SystemConfigService } from '../../services/system-config.service'; import { SystemConfigService } from '../../services/system-config.service';
import { dataTableElevation } from '../../style';
import { Subject } from 'rxjs';
@Component({ @Component({
selector: 'app-device-list', selector: 'app-device-list',
@ -18,8 +15,7 @@ export class DeviceListComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort; @ViewChild(MatSort) sort: MatSort;
@ViewChild(MatTable) table: MatTable<Device>; @ViewChild(MatTable) table: MatTable<Device>;
dataSource: DeviceListDataSource; dataSource: MatTableDataSource<Device>;
elevation: string = dataTableElevation;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['id', 'name']; displayedColumns = ['id', 'name'];
@ -27,14 +23,12 @@ export class DeviceListComponent implements AfterViewInit, OnInit {
constructor(private systemConfigService: SystemConfigService) { }; constructor(private systemConfigService: SystemConfigService) { };
ngOnInit() { ngOnInit() {
this.dataSource = new DeviceListDataSource(this.systemConfigService); this.dataSource = new MatTableDataSource();
this.dataSource.dataSubject = new Subject<Device[]>()
this.dataSource.data = []; this.dataSource.data = [];
this.systemConfigService.getDevices().subscribe( this.systemConfigService.getDevices().subscribe(
data => { data => {
this.dataSource.data = data; this.dataSource.data = data;
this.dataSource.dataSubject.next(data);
} }
); );
} }

View File

@ -1,82 +0,0 @@
import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge, Subject } from 'rxjs';
import Folder from '../../folder';
import { SystemConfigService } from '../../services/system-config.service';
/**
* Data source for the FolderList view. This class should
* encapsulate all logic for fetching and manipulating the displayed data
* (including sorting, pagination, and filtering).
*/
export class FolderListDataSource extends DataSource<Folder> {
data: Folder[];
dataSubject: Subject<Folder[]>;
paginator: MatPaginator;
sort: MatSort;
constructor(private systemConfigService: SystemConfigService) {
super();
}
/**
* Connect this data source to the table. The table will only update when
* the returned stream emits new items.
* @returns A stream of the items to be rendered.
*/
connect(): Observable<Folder[]> {
// Combine everything that affects the rendered data into one update
// st
const dataMutations = [
this.dataSubject,
observableOf(this.data),
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(map(() => {
return this.getPagedData(this.getSortedData([...this.data]));
}));
}
/**
* Called when the table is being destroyed. Use this function, to clean up
* any open connections or free any held resources that were set up during connect.
*/
disconnect() { }
/**
* Paginate the data (client-side). If you're using server-side pagination,
* this would be replaced by requesting the appropriate data from the server.
*/
private getPagedData(data: Folder[]) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
/**
* Sort the data (client-side). If you're using server-side sorting,
* this would be replaced by requesting the appropriate data from the server.
*/
private getSortedData(data: Folder[]) {
if (!this.sort.active || this.sort.direction === '') {
return data;
}
return data.sort((a, b) => {
const isAsc = this.sort.direction === 'asc';
switch (this.sort.active) {
case 'label': return compare(a.label, b.label, isAsc);
case 'id': return compare(+a.id, +b.id, isAsc);
default: return 0;
}
});
}
}
function compare(a: string | number, b: string | number, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

View File

@ -1,22 +1,24 @@
<div class="{{elevation}}"> <mat-form-field>
<table mat-table class="full-width-table" matSort aria-label="Elements"> <mat-label>Filter</mat-label>
<input matInput (keyup)="applyFilter($event)" placeholder="Ex. ium">
</mat-form-field>
<table mat-table class="full-width-table" matSort aria-label="Elements">
<!-- Id Column --> <!-- Id Column -->
<ng-container matColumnDef="id"> <ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th> <th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
<td mat-cell *matCellDef="let row">{{row.id}}</td> <td mat-cell *matCellDef="let row">{{row.id}}</td>
</ng-container> </ng-container>
<!-- Label Column --> <!-- Name Column -->
<ng-container matColumnDef="label"> <ng-container matColumnDef="label">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Label</th> <th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.label}}</td> <td mat-cell *matCellDef="let row">{{row.label}}</td>
</ng-container> </ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table> </table>
<mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50" <mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50"
[pageSizeOptions]="[25, 50, 100, 250]"> [pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator> </mat-paginator>
</div>

View File

@ -1,3 +1,3 @@
.full-width-table { .full-width-table {
width: 100%; width: 100%;
} }

View File

@ -1,8 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { FolderListComponent } from './folder-list.component'; import { FolderListComponent } from './folder-list.component';
@ -12,14 +8,9 @@ describe('FolderListComponent', () => {
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [FolderListComponent], declarations: [ FolderListComponent ]
imports: [ })
NoopAnimationsModule, .compileComponents();
MatPaginatorModule,
MatSortModule,
MatTableModule,
]
}).compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
@ -28,7 +19,7 @@ describe('FolderListComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should compile', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
}); });

View File

@ -1,13 +1,10 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'; import { AfterViewInit, Component, OnInit, ViewChild, Input } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator'; import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort'; import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table'; import { MatTable, MatTableDataSource } from '@angular/material/table';
import { FolderListDataSource } from './folder-list-datasource';
import Folder from '../../folder'; import Folder from '../../folder';
import { SystemConfigService } from '../../services/system-config.service'; import { SystemConfigService } from '../../services/system-config.service';
import { dataTableElevation } from '../../style';
import { Subject } from 'rxjs';
@Component({ @Component({
selector: 'app-folder-list', selector: 'app-folder-list',
@ -18,8 +15,12 @@ export class FolderListComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort; @ViewChild(MatSort) sort: MatSort;
@ViewChild(MatTable) table: MatTable<Folder>; @ViewChild(MatTable) table: MatTable<Folder>;
dataSource: FolderListDataSource; dataSource: MatTableDataSource<Folder>;
elevation: string = dataTableElevation;
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = filterValue.trim().toLowerCase();
}
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['id', 'label']; displayedColumns = ['id', 'label'];
@ -27,14 +28,12 @@ export class FolderListComponent implements AfterViewInit, OnInit {
constructor(private systemConfigService: SystemConfigService) { }; constructor(private systemConfigService: SystemConfigService) { };
ngOnInit() { ngOnInit() {
this.dataSource = new FolderListDataSource(this.systemConfigService); this.dataSource = new MatTableDataSource();
this.dataSource.dataSubject = new Subject<Folder[]>();
this.dataSource.data = []; this.dataSource.data = [];
this.systemConfigService.getFolders().subscribe( this.systemConfigService.getFolders().subscribe(
data => { data => {
this.dataSource.data = data; this.dataSource.data = data;
this.dataSource.dataSubject.next(data);
} }
); );
} }
@ -44,4 +43,4 @@ export class FolderListComponent implements AfterViewInit, OnInit {
this.dataSource.paginator = this.paginator; this.dataSource.paginator = this.paginator;
this.table.dataSource = this.dataSource; this.table.dataSource = this.dataSource;
} }
} }