Skip to content

a11y(table): add basic, sort, pager demos for a11y #6498 #6653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/demo-app/a11y/a11y-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {SliderAccessibilityDemo} from './slider/slider-a11y';
import {SlideToggleAccessibilityDemo} from './slide-toggle/slide-toggle-a11y';
import {SnackBarAccessibilityDemo} from './snack-bar/snack-bar-a11y';
import {SelectAccessibilityDemo} from './select/select-a11y';
import {TableAccessibilityDemo} from './table/table-a11y';
import {
TabsAccessibilityDemo,
SunnyTabContent,
Expand All @@ -45,7 +46,6 @@ import {SidenavBasicAccessibilityDemo} from './sidenav/basic-sidenav-a11y';
import {SidenavDualAccessibilityDemo} from './sidenav/dual-sidenav-a11y';
import {SidenavMobileAccessibilityDemo} from './sidenav/mobile-sidenav-a11y';


@NgModule({
imports: [
RouterModule.forChild(ACCESSIBILITY_DEMO_ROUTES)
Expand Down Expand Up @@ -98,6 +98,7 @@ export class AccessibilityRoutingModule {}
SliderAccessibilityDemo,
SlideToggleAccessibilityDemo,
SnackBarAccessibilityDemo,
TableAccessibilityDemo,
SunnyTabContent,
TabsAccessibilityDemo,
ToolbarAccessibilityDemo,
Expand Down
1 change: 1 addition & 0 deletions src/demo-app/a11y/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class AccessibilityDemo implements OnDestroy {
{name: 'Slide toggle', route: 'slide-toggle'},
{name: 'Slider', route: 'slider'},
{name: 'Snack bar', route: 'snack-bar'},
{name: 'Table', route: 'table'},
{name: 'Tabs', route: 'tabs'},
{name: 'Toolbar', route: 'toolbar'},
{name: 'Tooltip', route: 'tooltip'},
Expand Down
2 changes: 2 additions & 0 deletions src/demo-app/a11y/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {SliderAccessibilityDemo} from './slider/slider-a11y';
import {SlideToggleAccessibilityDemo} from './slide-toggle/slide-toggle-a11y';
import {SnackBarAccessibilityDemo} from './snack-bar/snack-bar-a11y';
import {SelectAccessibilityDemo} from './select/select-a11y';
import {TableAccessibilityDemo} from './table/table-a11y';
import {TabsAccessibilityDemo} from './tabs/tabs-a11y';
import {TABS_DEMO_ROUTES} from './tabs/routes';
import {TooltipAccessibilityDemo} from './tooltip/tooltip-a11y';
Expand Down Expand Up @@ -57,5 +58,6 @@ export const ACCESSIBILITY_DEMO_ROUTES: Routes = [
{path: 'snack-bar', component: SnackBarAccessibilityDemo},
{path: 'tabs', component: TabsAccessibilityDemo, children: TABS_DEMO_ROUTES},
{path: 'toolbar', component: ToolbarAccessibilityDemo},
{path: 'table', component: TableAccessibilityDemo},
{path: 'tooltip', component: TooltipAccessibilityDemo},
];
78 changes: 78 additions & 0 deletions src/demo-app/a11y/table/table-a11y.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<div>
<section>
<h2>Basic Table</h2>
<p>Shows name, color and age data.</p>
<mat-table aria-readonly="true"
[dataSource]="basicDataSource"
aria-label="Users favorite colors and age">
<ng-container cdkColumnDef="name">
<mat-header-cell *cdkHeaderCellDef>Name</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.name}}</mat-cell>
</ng-container>
<ng-container cdkColumnDef="color">
<mat-header-cell *cdkHeaderCellDef>Color</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.color}}</mat-cell>
</ng-container>
<ng-container cdkColumnDef="age">
<mat-header-cell *cdkHeaderCellDef>Age</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.age}}</mat-cell>
</ng-container>
<mat-header-row *cdkHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *cdkRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</section>

<section>
<h2>Sortable Table</h2>
<p>Shows name, color and age data. Sorted ascending by age.</p>
<mat-table [dataSource]="sortDataSource"
aria-label="Users favorite colors and age"
aria-readonly="true"
mdSort mdSortActive="age"
mdSortDirection="asc">
<ng-container cdkColumnDef="name">
<mat-header-cell *cdkHeaderCellDef>Name</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.name}}</mat-cell>
</ng-container>
<ng-container cdkColumnDef="color">
<mat-header-cell *cdkHeaderCellDef>Color</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.color}}</mat-cell>
</ng-container>
<ng-container cdkColumnDef="age">
<mat-header-cell *cdkHeaderCellDef mat-sort-header>Age</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.age}}</mat-cell>
</ng-container>
<mat-header-row *cdkHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *cdkRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</section>

<section>
<h2>Paginated Table</h2>
<p>Shows name, color and age data. Shows only first 5 until paginated.</p>
<mat-table [dataSource]="paginatedDataSource"
aria-readonly="true"
aria-label="Users favorite colors and age">
<ng-container cdkColumnDef="name">
<mat-header-cell *cdkHeaderCellDef>Name</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.name}}</mat-cell>
</ng-container>
<ng-container cdkColumnDef="color">
<mat-header-cell *cdkHeaderCellDef>Color</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.color}}</mat-cell>
</ng-container>
<ng-container cdkColumnDef="age">
<mat-header-cell *cdkHeaderCellDef>Age</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{row.age}}</mat-cell>
</ng-container>
<mat-header-row *cdkHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *cdkRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator #paginator
[length]="6"
[pageIndex]="0"
[pageSize]="3"
[pageSizeOptions]="[1, 3, 5]">
</mat-paginator>
</section>
</div>
Empty file.
114 changes: 114 additions & 0 deletions src/demo-app/a11y/table/table-a11y.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {Component, ViewChild} from '@angular/core';
import {DataSource} from '@angular/cdk/table';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable} from 'rxjs/Observable';
import {MatSort, MatPaginator} from '@angular/material';

export interface UserData {
name: string;
color: string;
age: number;
}

const exampleData = [
{name: 'Austin', color: 'blue', age: 30},
{name: 'Jeremy', color: 'green', age: 33},
{name: 'Kara', color: 'purple', age: 29},
{name: 'Tina', color: 'yellow', age: 35},
{name: 'Brad', color: 'pink', age: 40},
{name: 'Jules', color: 'red', age: 21},
];

@Component({
moduleId: module.id,
selector: 'table-a11y',
templateUrl: 'table-a11y.html',
styleUrls: ['table-a11y.css'],
})
export class TableAccessibilityDemo {
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) pager: MatPaginator;

displayedColumns = ['name', 'color', 'age'];
basicDataSource: BasicDataSource;
sortDataSource: SortDataSource;
paginatedDataSource: PaginatedDataSource;

ngOnInit(): void {
this.basicDataSource = new BasicDataSource();
this.sortDataSource = new SortDataSource(this.sort);
this.paginatedDataSource = new PaginatedDataSource(this.pager);
}
}

export class BasicDataSource extends DataSource<UserData> {
dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]);

constructor() {
super();
this.dataChange.next(exampleData);
}

connect(): Observable<UserData[]> {
return this.dataChange;
}

disconnect() {}
}

export class SortDataSource extends DataSource<UserData> {
dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]);

constructor(private _sort: MatSort) {
super();
this.dataChange.next(exampleData);
}

connect(): Observable<UserData[]> {
const displayDataChanges = [
this.dataChange,
this._sort.sortChange,
];

return Observable.merge(...displayDataChanges).map(() => {
return this.getSortedData();
});
}

disconnect() {}

getSortedData(): UserData[] {
const data = [...exampleData];
if (!this._sort.active || this._sort.direction == '') {
return data;
}

return data.sort((a: UserData, b: UserData) => {
return (a.age < b.age ? -1 : 1) * (this._sort.direction == 'asc' ? 1 : -1);
});
}
}

export class PaginatedDataSource extends DataSource<UserData> {
dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]);

constructor(private _paginator: MatPaginator) {
super();
this.dataChange.next(exampleData);
}

connect(): Observable<UserData[]> {
const displayDataChanges = [
this.dataChange,
this._paginator.page,
];

return Observable.merge(...displayDataChanges).map(() => {
const data = [...exampleData];
const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
return data.splice(startIndex, this._paginator.pageSize);
});
}

disconnect() {}
}