Skip to content

Commit

Permalink
Added functionality to default sort in descending order
Browse files Browse the repository at this point in the history
  • Loading branch information
gbubemismith committed Nov 4, 2024
1 parent 91b0660 commit c7c31fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 11 additions & 7 deletions libs/components/src/table/sortable.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { coerceBooleanProperty } from "@angular/cdk/coercion";
import { Component, HostBinding, Input, OnInit } from "@angular/core";

import type { SortFn } from "./table-data-source";
import type { SortDirection, SortFn } from "./table-data-source";
import { TableComponent } from "./table.component";

@Component({
Expand All @@ -19,12 +19,16 @@ export class SortableComponent implements OnInit {
*/
@Input() bitSortable: string;

private _default: boolean;
private _default: SortDirection | boolean = false;
/**
* Mark the column as the default sort column
*/
@Input() set default(value: boolean | "") {
this._default = coerceBooleanProperty(value);
@Input() set default(value: SortDirection | boolean | "") {
if (value === "desc" || value === "asc") {
this._default = value;
} else {
this._default = coerceBooleanProperty(value) ? "asc" : false;
}
}

/**
Expand All @@ -44,7 +48,7 @@ export class SortableComponent implements OnInit {

ngOnInit(): void {
if (this._default && !this.isActive) {
this.setActive();
this.setActive(this._default as SortDirection);
}
}

Expand All @@ -55,9 +59,9 @@ export class SortableComponent implements OnInit {
return this.sort.direction === "asc" ? "ascending" : "descending";
}

protected setActive() {
protected setActive(defaultDirection: SortDirection = "asc") {
if (this.table.dataSource) {
const direction = this.isActive && this.direction === "asc" ? "desc" : "asc";
const direction = this.isActive && this.direction === "asc" ? "desc" : defaultDirection;
this.table.dataSource.sort = {
column: this.bitSortable,
direction: direction,
Expand Down
6 changes: 6 additions & 0 deletions libs/components/src/table/table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ indicator for which column is currently sorted. The default sorting can be speci
<th bitCell bitSortable="name" default>Name</th>
```

For default sorting in descending order, set default="desc"

```html
<th bitCell bitSortable="name" default="desc">Name</th>
```

It's also possible to define a custom sorting function by setting the `fn` input.

```ts
Expand Down

0 comments on commit c7c31fb

Please sign in to comment.