question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Table] Add a full-featured but limited table component and array-based data source

See original GitHub issue

Introduce a new table component that would use <mat-table> and come with built-in features including sorting, pagination, selection, etc. Would accept an array-based data input so users do not need to create a data source to use the table.

This would allow people to quickly use a table but would limit their ability to customize behavior and would be very opinionated on user experience.

Example concept:

<mat-simple-table #simpleTable [data]="_peopleDatabase.data" pageSize="25">
  <mat-simple-column headerText="User ID" property="id"></mat-simple-column>
  <mat-simple-column headerText="Name" property="name"></mat-simple-column>
  <mat-simple-column headerText="Progress" property="progress"></mat-simple-column>
  <mat-simple-column headerText="Color" property="color"></mat-simple-column>
</mat-simple-table>

Notes:

  • Long column names should be truncated and display a tooltip on hover

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:25
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
b-johnsecommented, Jun 29, 2022

@mykolav here’s an updated version as a angular 14 standalone component that I have working:

import { CommonModule } from '@angular/common';
import { Component, HostBinding, Input, OnDestroy, OnInit, Optional, ViewChild } from '@angular/core';
import { MatSortModule } from '@angular/material/sort';
import {
  MatCellDef,
  MatColumnDef,
  MatFooterCellDef,
  MatHeaderCellDef,
  MatTable,
  MatTableModule,
} from '@angular/material/table';

@Component({
  standalone: true,
  imports: [MatTableModule, CommonModule, MatSortModule],
  selector: 'evt-table-column-currency',
  template: `
    <ng-container matColumnDef>
      <th mat-header-cell *matHeaderCellDef>
        <span mat-sort-header> {{ label ?? capitalize(prop) }}</span>
      </th>
      <td mat-cell *matCellDef="let row">{{ row[prop] | currency }}</td>
    </ng-container>
  `,
})
export class TableCurrencyColumnComponent implements OnDestroy, OnInit {
  @Input() prop = '';
  @Input() label: string | undefined;

  constructor(@Optional() public table: MatTable<unknown>) {}

  @HostBinding('attr.ariaHidden') ariaHidden!: true;
  @HostBinding('class') classes!: 'column-template cdk-visually-hidden';

  @ViewChild(MatColumnDef, { static: true }) columnDef!: MatColumnDef;
  @ViewChild(MatCellDef, { static: true }) cellDef!: MatCellDef;
  @ViewChild(MatHeaderCellDef, { static: true }) headerCellDef!: MatHeaderCellDef;
  @ViewChild(MatFooterCellDef, { static: true }) footerCellDef!: MatFooterCellDef;

  ngOnInit(): void {
    if (this.table && this.columnDef) {
      this.columnDef.name = this.prop;
      this.columnDef.cell = this.cellDef;
      this.columnDef.headerCell = this.headerCellDef;
      this.columnDef.footerCell = this.footerCellDef;
      this.table.addColumnDef(this.columnDef);
    }
  }

  ngOnDestroy(): void {
    if (this.table) {
      this.table.removeColumnDef(this.columnDef);
    }
  }

  capitalize(value: string): string {
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}

1reaction
kussmaulcommented, Oct 21, 2020

FYI, I finally got this to work, by explicitly connecting the columnDef to its children. I don’t know if this happens later, or if it should happen sooner but doesn’t for some reason.

  @ViewChild(    MatColumnDef, { static : true })     columnDef ! :     MatColumnDef;
  @ViewChild(      MatCellDef, { static : true })       cellDef ! :       MatCellDef;
  @ViewChild(MatHeaderCellDef, { static : true }) headerCellDef ! : MatHeaderCellDef;
  @ViewChild(MatFooterCellDef, { static : true }) footerCellDef ! : MatFooterCellDef;

  ngOnInit() {
    if (this.table && this.columnDef) {
      this.columnDef.cell       = this.cellDef;
      this.columnDef.headerCell = this.headerCellDef;
      this.columnDef.footerCell = this.footerCellDef;
      this.table.addColumnDef(this.columnDef);
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add data dynamically to mat-table dataSource?
I have found a solution for this problem, basically if you do: this.dataSource.data.push(newElement); //Doesn't work. But if you replace the ...
Read more >
Tables - CKEditor 5 Documentation
The table feature offers table creation and editing tools that help content authors bring tabular data into their documents. Tables help organize the...
Read more >
DataTable UI widget documentation - Webix
It is a powerful yet easy-to-use component that supports different data sources (XML, JSON, CSV, JSArray, HTML tables). Datatable has various possibilities ...
Read more >
Do not include some Object data source items - DataTables
I have data coming with a field to Ignore the entry. ... I may be counting, adding, or doing other things with the...
Read more >
Full-featured Angular Data Grid (Data Table) | Smart UI for ...
The Angular Grid component for Web development. Supporting editing by cell or entire row, grouping, paging, sorting by single or multiple columns, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found