Angular 17: Sort Material Table with Signal data source - Stack Overflow

I have as datasource in a Material table a signal.dataSource: WritableSignal<PeriodicElement[]> =

I have as datasource in a Material table a signal.

  dataSource: WritableSignal<PeriodicElement[]> = signal([]);

The data of the table is displayed properly

I add according to the description the matSort to the header.

<table mat-table matSort [dataSource]="dataSource()" class="mat-elevation-z8">
<!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell mat-sort-header *matHeaderCellDef>No.</th>
...

But somehow this won't work. I've seen and tried without Signals and this worked. Any idea how to use it properly with Signals?

Stackblitz

I have as datasource in a Material table a signal.

  dataSource: WritableSignal<PeriodicElement[]> = signal([]);

The data of the table is displayed properly

I add according to the description the matSort to the header.

<table mat-table matSort [dataSource]="dataSource()" class="mat-elevation-z8">
<!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell mat-sort-header *matHeaderCellDef>No.</th>
...

But somehow this won't work. I've seen and tried without Signals and this worked. Any idea how to use it properly with Signals?

Stackblitz

Share Improve this question asked Mar 3 at 9:44 LeOLeO 5,3166 gold badges62 silver badges110 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

A DataSource of a mat-table can be an Array OR a MatTableDataSource. If you use an Array as dataSource, you have NOT "sort" properties. So you need define some like

  dataSource:  WritableSignal<MatTableDataSource<PeriodicElement>>=
                      signal(new MatTableDataSource([] as PeriodicElement[]))

  @ViewChild(MatSort) sort: MatSort;

  ngOnInit() {
    this.dataSource.set(new MatTableDataSource(ELEMENT_DATA));
  }

  ngAfterViewInit() {
    this.dataSource().sort=this.sort
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745100574a4611247.html

相关推荐

  • Angular 17: Sort Material Table with Signal data source - Stack Overflow

    I have as datasource in a Material table a signal.dataSource: WritableSignal<PeriodicElement[]> =

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信