Skip to content

docs(sort): add initial sorting docs #5513

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 5 commits into from
Jul 5, 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
35 changes: 35 additions & 0 deletions src/lib/sort/sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
The `mdSort` and `md-sort-header` are used, respectively, to add sorting state and display
to tabular data.

<!-- example(sort-overview) -->

### Adding sort to table headers

To add sorting behavior and styling to a set of table headers, add the `<md-sort-header>` component
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overview should be structured like

  1. Single sentence that summarizes the components, e.g.: "The mdSort and md-sort-header are used, respectively, to add sorting state and display to tabular data."
  2. overview example
  3. Basic use
  4. Additional sections on configuration, alternate behaviors, etc.
  5. Accessibility notes for this component

to each header and provide an `id` that will identify it. These headers should be contained within a
parent element with the `mdSort` directive, which will emit an `mdSortChange` event when the user
triggers sorting on the header.

Users can trigger the sort header through a mouse click or keyboard action. When this happens, the
`mdSort` will emit an `mdSortChange` event that contains the ID of the header triggered and the
direction to sort (`asc` or `desc`).

#### Changing the sort order

By default, a sort header starts its sorting at `asc` and then `desc`. Triggering the sort header
after `desc` will remove sorting.

To reverse the sort order for all headers, set the `mdSortStart` to `desc` on the `mdSort`
directive. To reverse the order only for a specific header, set the `start` input only on the header
instead.

To prevent the user from clearing the sort sort state from an already sorted column, set
`mdSortDisableClear` to `true` on the `mdSort` to affect all headers, or set `disableClear` to
`true` on a specific header.

#### Using sort with the md-table

When used on an `md-table` header, it is not required to set an `md-sort-header` id on because
by default it will use the id of the column.

<!-- example(table-sorting) -->
3 changes: 3 additions & 0 deletions src/material-examples/example-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import {TableBasicExample} from './table-basic/table-basic-example';
import {TableSortingExample} from './table-sorting/table-sorting-example';
import {TableFilteringExample} from './table-filtering/table-filtering-example';
import {CdkTableBasicExample} from './cdk-table-basic/cdk-table-basic-example';
import {SortOverviewExample} from './sort-overview/sort-overview-example';

export interface LiveExample {
title: string;
Expand Down Expand Up @@ -197,6 +198,7 @@ export const EXAMPLE_COMPONENTS = {
},
'slide-toggle-forms': {title: 'Slide-toggle with forms', component: SlideToggleFormsExample},
'slide-toggle-overview': {title: 'Basic slide-toggles', component: SlideToggleOverviewExample},
'sort-overview': {title: 'Sorting overview', component: SortOverviewExample},
'snack-bar-component': {
title: 'Snack-bar with a custom component',
component: SnackBarComponentExample
Expand Down Expand Up @@ -312,6 +314,7 @@ export const EXAMPLE_LIST = [
SlideToggleConfigurableExample,
SlideToggleOverviewExample,
SlideToggleFormsExample,
SortOverviewExample,
SnackBarComponentExample,
PizzaPartyComponent,
SnackBarOverviewExample,
Expand Down
3 changes: 3 additions & 0 deletions src/material-examples/sort-overview/sort-overview-example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.mat-sort-header-container {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be an additional example of the sort directives being applied to an md-table.
(we can re-use the same example for both the table and sortable pages)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some message about it; can add example when table-docs PR is merged

align-items: center;
}
17 changes: 17 additions & 0 deletions src/material-examples/sort-overview/sort-overview-example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<table mdSort (mdSortChange)="sortData($event)">
<tr>
<th md-sort-header="name">Dessert (100g)</th>
<th md-sort-header="calories">Calories</th>
<th md-sort-header="fat">Fat (g)</th>
<th md-sort-header="carbs">Carbs (g)</th>
<th md-sort-header="protein">Protein (g)</th>
</tr>

<tr *ngFor="let dessert of sortedData">
<td>{{dessert.name}}</td>
<td>{{dessert.calories}}</td>
<td>{{dessert.fat}}</td>
<td>{{dessert.carbs}}</td>
<td>{{dessert.protein}}</td>
</tr>
</table>
48 changes: 48 additions & 0 deletions src/material-examples/sort-overview/sort-overview-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {Component} from '@angular/core';
import {Sort} from '@angular/material';


@Component({
selector: 'sort-overview-example',
templateUrl: 'sort-overview-example.html',
styleUrls: ['sort-overview-example.css'],
})
export class SortOverviewExample {
desserts = [
{name: 'Frozen yogurt', calories: '159', fat: '6', carbs: '24', protein: '4'},
{name: 'Ice cream sandwich', calories: '237', fat: '9', carbs: '37', protein: '4'},
{name: 'Eclair', calories: '262', fat: '16', carbs: '24', protein: '6'},
{name: 'Cupcake', calories: '305', fat: '4', carbs: '67', protein: '4'},
{name: 'Gingerbread', calories: '356', fat: '16', carbs: '49', protein: '4'},
];

sortedData;

constructor() {
this.sortedData = this.desserts.slice();
}

sortData(sort: Sort) {
const data = this.desserts.slice();
if (!sort.active || sort.direction == '') {
this.sortedData = data;
return;
}

this.sortedData = data.sort((a, b) => {
let isAsc = sort.direction == 'asc';
switch (sort.active) {
case 'name': return compare(a.name, b.name, isAsc);
case 'calories': return compare(+a.calories, +b.calories, isAsc);
case 'fat': return compare(+a.fat, +b.fat, isAsc);
case 'carbs': return compare(+a.carbs, +b.carbs, isAsc);
case 'protein': return compare(+a.protein, +b.protein, isAsc);
default: return 0;
}
});
}
}

function compare(a, b, isAsc) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}