Skip to content

Commit 38b4265

Browse files
andrewseguinmmalerba
authored andcommitted
docs(sort): add initial sorting docs (#5513)
* docs(sort): add initial sorting docs * add newlines * update * minor * rebase, add table example
1 parent d65087b commit 38b4265

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

src/lib/sort/sort.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
The `mdSort` and `md-sort-header` are used, respectively, to add sorting state and display
2+
to tabular data.
3+
4+
<!-- example(sort-overview) -->
5+
6+
### Adding sort to table headers
7+
8+
To add sorting behavior and styling to a set of table headers, add the `<md-sort-header>` component
9+
to each header and provide an `id` that will identify it. These headers should be contained within a
10+
parent element with the `mdSort` directive, which will emit an `mdSortChange` event when the user
11+
triggers sorting on the header.
12+
13+
Users can trigger the sort header through a mouse click or keyboard action. When this happens, the
14+
`mdSort` will emit an `mdSortChange` event that contains the ID of the header triggered and the
15+
direction to sort (`asc` or `desc`).
16+
17+
#### Changing the sort order
18+
19+
By default, a sort header starts its sorting at `asc` and then `desc`. Triggering the sort header
20+
after `desc` will remove sorting.
21+
22+
To reverse the sort order for all headers, set the `mdSortStart` to `desc` on the `mdSort`
23+
directive. To reverse the order only for a specific header, set the `start` input only on the header
24+
instead.
25+
26+
To prevent the user from clearing the sort sort state from an already sorted column, set
27+
`mdSortDisableClear` to `true` on the `mdSort` to affect all headers, or set `disableClear` to
28+
`true` on a specific header.
29+
30+
#### Using sort with the md-table
31+
32+
When used on an `md-table` header, it is not required to set an `md-sort-header` id on because
33+
by default it will use the id of the column.
34+
35+
<!-- example(table-sorting) -->

src/material-examples/example-module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import {TableBasicExample} from './table-basic/table-basic-example';
9898
import {TableSortingExample} from './table-sorting/table-sorting-example';
9999
import {TableFilteringExample} from './table-filtering/table-filtering-example';
100100
import {CdkTableBasicExample} from './cdk-table-basic/cdk-table-basic-example';
101+
import {SortOverviewExample} from './sort-overview/sort-overview-example';
101102

102103
export interface LiveExample {
103104
title: string;
@@ -197,6 +198,7 @@ export const EXAMPLE_COMPONENTS = {
197198
},
198199
'slide-toggle-forms': {title: 'Slide-toggle with forms', component: SlideToggleFormsExample},
199200
'slide-toggle-overview': {title: 'Basic slide-toggles', component: SlideToggleOverviewExample},
201+
'sort-overview': {title: 'Sorting overview', component: SortOverviewExample},
200202
'snack-bar-component': {
201203
title: 'Snack-bar with a custom component',
202204
component: SnackBarComponentExample
@@ -312,6 +314,7 @@ export const EXAMPLE_LIST = [
312314
SlideToggleConfigurableExample,
313315
SlideToggleOverviewExample,
314316
SlideToggleFormsExample,
317+
SortOverviewExample,
315318
SnackBarComponentExample,
316319
PizzaPartyComponent,
317320
SnackBarOverviewExample,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mat-sort-header-container {
2+
align-items: center;
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<table mdSort (mdSortChange)="sortData($event)">
2+
<tr>
3+
<th md-sort-header="name">Dessert (100g)</th>
4+
<th md-sort-header="calories">Calories</th>
5+
<th md-sort-header="fat">Fat (g)</th>
6+
<th md-sort-header="carbs">Carbs (g)</th>
7+
<th md-sort-header="protein">Protein (g)</th>
8+
</tr>
9+
10+
<tr *ngFor="let dessert of sortedData">
11+
<td>{{dessert.name}}</td>
12+
<td>{{dessert.calories}}</td>
13+
<td>{{dessert.fat}}</td>
14+
<td>{{dessert.carbs}}</td>
15+
<td>{{dessert.protein}}</td>
16+
</tr>
17+
</table>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {Component} from '@angular/core';
2+
import {Sort} from '@angular/material';
3+
4+
5+
@Component({
6+
selector: 'sort-overview-example',
7+
templateUrl: 'sort-overview-example.html',
8+
styleUrls: ['sort-overview-example.css'],
9+
})
10+
export class SortOverviewExample {
11+
desserts = [
12+
{name: 'Frozen yogurt', calories: '159', fat: '6', carbs: '24', protein: '4'},
13+
{name: 'Ice cream sandwich', calories: '237', fat: '9', carbs: '37', protein: '4'},
14+
{name: 'Eclair', calories: '262', fat: '16', carbs: '24', protein: '6'},
15+
{name: 'Cupcake', calories: '305', fat: '4', carbs: '67', protein: '4'},
16+
{name: 'Gingerbread', calories: '356', fat: '16', carbs: '49', protein: '4'},
17+
];
18+
19+
sortedData;
20+
21+
constructor() {
22+
this.sortedData = this.desserts.slice();
23+
}
24+
25+
sortData(sort: Sort) {
26+
const data = this.desserts.slice();
27+
if (!sort.active || sort.direction == '') {
28+
this.sortedData = data;
29+
return;
30+
}
31+
32+
this.sortedData = data.sort((a, b) => {
33+
let isAsc = sort.direction == 'asc';
34+
switch (sort.active) {
35+
case 'name': return compare(a.name, b.name, isAsc);
36+
case 'calories': return compare(+a.calories, +b.calories, isAsc);
37+
case 'fat': return compare(+a.fat, +b.fat, isAsc);
38+
case 'carbs': return compare(+a.carbs, +b.carbs, isAsc);
39+
case 'protein': return compare(+a.protein, +b.protein, isAsc);
40+
default: return 0;
41+
}
42+
});
43+
}
44+
}
45+
46+
function compare(a, b, isAsc) {
47+
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
48+
}

0 commit comments

Comments
 (0)