Skip to content
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

feat(addon-table): Sortable make dynamic #9384

Merged
merged 3 commits into from
Oct 8, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {OnInit} from '@angular/core';
import {Directive, forwardRef, inject} from '@angular/core';
import type {BooleanInput} from '@angular/cdk/coercion';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import type {OnChanges} from '@angular/core';
import {Directive, forwardRef, inject, Input} from '@angular/core';
import type {TuiComparator} from '@taiga-ui/addon-table/types';

import {TuiTableTh} from '../th/th.component';
Expand All @@ -10,20 +12,32 @@ import {TuiTableDirective} from './table.directive';
standalone: true,
selector: 'th[tuiTh][tuiSortable]',
})
export class TuiTableSortable<T extends Partial<Record<keyof T, any>>> implements OnInit {
export class TuiTableSortable<T extends Partial<Record<keyof T, any>>>
implements OnChanges
{
private readonly table = inject(TuiTableDirective<T>);
private readonly th = inject(TuiTableTh<T>);
private readonly sortBy = inject<TuiTableSortBy<T>>(forwardRef(() => TuiTableSortBy));

@Input({
alias: 'tuiSortable',
transform: coerceBooleanProperty,
})
public sortable: BooleanInput;

public get key(): keyof T {
return this.th.key;
}

public sorter: TuiComparator<T> = (): number => 0;

public ngOnInit(): void {
this.sorter = this.match ? this.table.sorter : this.sorter;
this.th.sorter = this.sorter;
public ngOnChanges(): void {
if (this.sortable) {
this.sorter = this.match ? this.table.sorter : this.sorter;
this.th.sorter = this.sorter;
} else {
this.th.sorter = null;
}
}

public check(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
Find on page
</tui-input>
<tui-input-number
decimal="never"
class="tui-space_horizontal-3"
[formControl]="minAge"
[tuiNumberFormat]="{precision: 0}"
>
Minimum age
</tui-input-number>
</p>
<p class="filters">
<label tuiLabel>
<input
tuiCheckbox
type="checkbox"
[(ngModel)]="dob"
/>
Enable sorting by DOB
</label>
<button
size="m"
tuiButton
Expand Down Expand Up @@ -60,8 +69,8 @@
</th>
<th
*tuiHead="'dob'"
tuiSortable
tuiTh
[tuiSortable]="dob"
>
Date of Birth
</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

.filters {
display: flex;
gap: 1rem;
white-space: nowrap;
align-items: center;
}

.input {
Expand Down
15 changes: 13 additions & 2 deletions projects/demo/src/modules/components/table/examples/4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import {
TuiLet,
tuiToInt,
} from '@taiga-ui/cdk';
import {TuiButton, TuiDropdown, TuiLoader} from '@taiga-ui/core';
import {TuiChevron} from '@taiga-ui/kit';
import {
TuiButton,
TuiDropdown,
TuiLabel,
TuiLoader,
TuiNumberFormat,
} from '@taiga-ui/core';
import {TuiCheckbox, TuiChevron} from '@taiga-ui/kit';
import {
TuiInputModule,
TuiInputNumberModule,
Expand Down Expand Up @@ -108,12 +114,15 @@ function getAge({dob}: User): number {
NgIf,
ReactiveFormsModule,
TuiButton,
TuiCheckbox,
TuiChevron,
TuiDropdown,
TuiInputModule,
TuiInputNumberModule,
TuiLabel,
TuiLet,
TuiLoader,
TuiNumberFormat,
TuiReorder,
TuiTable,
TuiTablePagination,
Expand Down Expand Up @@ -152,6 +161,8 @@ export default class Example {

protected columns = ['name', 'dob', 'age'];

protected dob = false;

protected search = '';

protected readonly loading$ = this.request$.pipe(map(tuiIsFalsy));
Expand Down
Loading