Skip to content
Closed
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
13 changes: 12 additions & 1 deletion goldens/material/sort/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { OnChanges } from '@angular/core';
import { OnDestroy } from '@angular/core';
import { OnInit } from '@angular/core';
import { Subject } from 'rxjs';
import { TemplateRef } from '@angular/core';

// @public @deprecated
export type ArrowViewState = SortDirection | 'hint' | 'active';
Expand Down Expand Up @@ -73,6 +74,7 @@ export interface MatSortable {
export interface MatSortDefaultOptions {
arrowPosition?: SortHeaderArrowPosition;
disableClear?: boolean;
icons?: SortHeaderIcons;
}

// @public
Expand All @@ -83,6 +85,7 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
arrowPosition: SortHeaderArrowPosition;
// (undocumented)
_columnDef: MatSortHeaderColumnDef | null;
get defaultIcons(): SortHeaderIcons | undefined;
disableClear: boolean;
disabled: boolean;
_getAriaSortAttribute(): "none" | "ascending" | "descending";
Expand Down Expand Up @@ -110,10 +113,11 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
_sort: MatSort;
get sortActionDescription(): string;
set sortActionDescription(value: string);
readonly sortIconsTemplate: i0.InputSignal<TemplateRef<any> | null>;
start: SortDirection;
_toggleOnInteraction(): void;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatSortHeader, "[mat-sort-header]", ["matSortHeader"], { "id": { "alias": "mat-sort-header"; "required": false; }; "arrowPosition": { "alias": "arrowPosition"; "required": false; }; "start": { "alias": "start"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "sortActionDescription": { "alias": "sortActionDescription"; "required": false; }; "disableClear": { "alias": "disableClear"; "required": false; }; }, {}, never, ["*"], true, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatSortHeader, "[mat-sort-header]", ["matSortHeader"], { "id": { "alias": "mat-sort-header"; "required": false; }; "arrowPosition": { "alias": "arrowPosition"; "required": false; }; "start": { "alias": "start"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "sortActionDescription": { "alias": "sortActionDescription"; "required": false; }; "disableClear": { "alias": "disableClear"; "required": false; }; "sortIconsTemplate": { "alias": "matSortIconsTemplate"; "required": false; "isSignal": true; }; }, {}, never, ["*"], true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatSortHeader, never>;
}
Expand Down Expand Up @@ -149,6 +153,13 @@ export type SortDirection = 'asc' | 'desc' | '';
// @public
export type SortHeaderArrowPosition = 'before' | 'after';

// @public
export interface SortHeaderIcons {
ascending: string;
default?: string;
descending: string;
}

// (No @packageDocumentation comment for this package)

```
1 change: 1 addition & 0 deletions src/components-examples/material/sort/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ng_project(
"//:node_modules/@types/jasmine",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/icon",
"//src/material/sort",
"//src/material/sort/testing",
],
Expand Down
4 changes: 4 additions & 0 deletions src/components-examples/material/sort/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export {SortOverviewExample} from './sort-overview/sort-overview-example';
export {SortHeaderIconsExample} from './sort-header-icons/sort-header-icons-example';
export {SortHeaderIconsWithDefaultExample} from './sort-header-icons-with-default/sort-header-icons-with-default-example';
export {SortHeaderCustomExample} from './sort-header-custom/sort-header-custom-example';
export {SortHeaderCustomWithDefaultExample} from './sort-header-custom-with-default/sort-header-custom-with-default-example';
export {SortHarnessExample} from './sort-harness/sort-harness-example';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.mat-sort-header-container {
align-items: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<ng-template #sortIconsTemplate let-sortHeader>
@if (!sortHeader.isDisabled && sortHeader.isSorted) {
@if (sortHeader.direction === 'desc') {
<mat-icon focusable="false" aria-hidden="true" style="color: red;">keyboard_arrow_down</mat-icon>
}
@else {
<mat-icon focusable="false" aria-hidden="true" style="color: green;">keyboard_arrow_up</mat-icon>
}
}
@else {
<mat-icon focusable="false" aria-hidden="true">unfold_more</mat-icon>
}
</ng-template>

<table matSort (matSortChange)="sortData($event)">
<tr>
<th mat-sort-header="name" [matSortIconsTemplate]="sortIconsTemplate">Dessert (100g)</th>
<th mat-sort-header="calories" [matSortIconsTemplate]="sortIconsTemplate">Calories</th>
<th mat-sort-header="fat" [matSortIconsTemplate]="sortIconsTemplate">Fat (g)</th>
<th mat-sort-header="carbs" [matSortIconsTemplate]="sortIconsTemplate">Carbs (g)</th>
<th mat-sort-header="protein" [matSortIconsTemplate]="sortIconsTemplate">Protein (g)</th>
</tr>

@for (dessert of sortedData; track dessert) {
<tr>
<td>{{dessert.name}}</td>
<td>{{dessert.calories}}</td>
<td>{{dessert.fat}}</td>
<td>{{dessert.carbs}}</td>
<td>{{dessert.protein}}</td>
</tr>
}
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {Component} from '@angular/core';
import {MatIcon} from '@angular/material/icon';
import {Sort, MatSortModule} from '@angular/material/sort';

export interface Dessert {
calories: number;
carbs: number;
fat: number;
name: string;
protein: number;
}

/**
* @title Sorting header with custom template (including default).
*/
@Component({
selector: 'sort-header-custom-with-default-example',
templateUrl: 'sort-header-custom-with-default-example.html',
styleUrl: 'sort-header-custom-with-default-example.css',
imports: [MatSortModule, MatIcon],
})
export class SortHeaderCustomWithDefaultExample {
desserts: Dessert[] = [
{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: Dessert[];

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) => {
const 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: number | string, b: number | string, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.mat-sort-header-container {
align-items: center;
}

sup {
font-size: 7px;
top: -3px;
position: absolute;
display: block;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<ng-template #sortIconsTemplate let-sortHeader>
@if (!sortHeader.isDisabled && sortHeader.isSorted) {
<sup>{{ sortHeader.direction === 'desc' ? 'Z-A' : 'A-Z' }}</sup>
}
</ng-template>

<table matSort (matSortChange)="sortData($event)">
<tr>
<th mat-sort-header="name" [matSortIconsTemplate]="sortIconsTemplate">Dessert (100g)</th>
<th mat-sort-header="calories" [matSortIconsTemplate]="sortIconsTemplate">Calories</th>
<th mat-sort-header="fat" [matSortIconsTemplate]="sortIconsTemplate">Fat (g)</th>
<th mat-sort-header="carbs" [matSortIconsTemplate]="sortIconsTemplate">Carbs (g)</th>
<th mat-sort-header="protein" [matSortIconsTemplate]="sortIconsTemplate">Protein (g)</th>
</tr>

@for (dessert of sortedData; track dessert) {
<tr>
<td>{{dessert.name}}</td>
<td>{{dessert.calories}}</td>
<td>{{dessert.fat}}</td>
<td>{{dessert.carbs}}</td>
<td>{{dessert.protein}}</td>
</tr>
}
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {Component} from '@angular/core';
import {Sort, MatSortModule} from '@angular/material/sort';

export interface Dessert {
calories: number;
carbs: number;
fat: number;
name: string;
protein: number;
}

/**
* @title Sorting header with custom template.
*/
@Component({
selector: 'sort-header-custom-example',
templateUrl: 'sort-header-custom-example.html',
styleUrl: 'sort-header-custom-example.css',
imports: [MatSortModule],
})
export class SortHeaderCustomExample {
desserts: Dessert[] = [
{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: Dessert[];

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) => {
const 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: number | string, b: number | string, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.mat-sort-header-container {
align-items: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table matSort (matSortChange)="sortData($event)">
<tr>
<th mat-sort-header="name">Dessert (100g)</th>
<th mat-sort-header="calories">Calories</th>
<th mat-sort-header="fat">Fat (g)</th>
<th mat-sort-header="carbs">Carbs (g)</th>
<th mat-sort-header="protein">Protein (g)</th>
</tr>

@for (dessert of sortedData; track dessert) {
<tr>
<td>{{dessert.name}}</td>
<td>{{dessert.calories}}</td>
<td>{{dessert.fat}}</td>
<td>{{dessert.carbs}}</td>
<td>{{dessert.protein}}</td>
</tr>
}
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {Component} from '@angular/core';
import {Sort, MatSortModule, MAT_SORT_DEFAULT_OPTIONS} from '@angular/material/sort';

export interface Dessert {
calories: number;
carbs: number;
fat: number;
name: string;
protein: number;
}

/**
* @title Sorting header with configured default icons.
*/
@Component({
selector: 'sort-header-icons-with-default-example',
templateUrl: 'sort-header-icons-with-default-example.html',
styleUrl: 'sort-header-icons-with-default-example.css',
imports: [MatSortModule],
providers: [
{
provide: MAT_SORT_DEFAULT_OPTIONS,
useValue: {
icons: {
ascending: 'keyboard_arrow_up',
descending: 'keyboard_arrow_down',
default: 'unfold_more', // if you'd like to have always visible default sorting icon
},
},
},
],
})
export class SortHeaderIconsWithDefaultExample {
desserts: Dessert[] = [
{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: Dessert[];

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) => {
const 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: number | string, b: number | string, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.mat-sort-header-container {
align-items: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table matSort (matSortChange)="sortData($event)">
<tr>
<th mat-sort-header="name">Dessert (100g)</th>
<th mat-sort-header="calories">Calories</th>
<th mat-sort-header="fat">Fat (g)</th>
<th mat-sort-header="carbs">Carbs (g)</th>
<th mat-sort-header="protein">Protein (g)</th>
</tr>

@for (dessert of sortedData; track dessert) {
<tr>
<td>{{dessert.name}}</td>
<td>{{dessert.calories}}</td>
<td>{{dessert.fat}}</td>
<td>{{dessert.carbs}}</td>
<td>{{dessert.protein}}</td>
</tr>
}
</table>
Loading
Loading