import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import { DatePipe } from '@angular/common';
import { JtGridCellDirective, JtGridColumn, JtGridComponent } from '@jambotron/aria-grid';
import { makeServers, Server } from '../data/servers';
import { text } from '../i18n/labels';
import { CodeComponent } from './code';
/** Example 1: columns, cell templates, sorting, paging, filtering and the empty state. */
@Component({
selector: 'demo-basic-example',
imports: [JtGridComponent, JtGridCellDirective, DatePipe, CodeComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
{{ statusText(row.status) }}
{{ row.cpu }}%
{{ row.memoryGb }} GB
{{ row.created | date: 'mediumDate' }}
`
})
export class BasicExampleComponent {
protected readonly t = text;
protected readonly statusText = (s: Server['status']) => this.t()[s];
private readonly all = makeServers();
protected readonly filter = signal('');
// rows is a plain input: filter the array yourself, the grid keeps the page and the sort
protected readonly rows = computed(() => {
const q = this.filter().trim().toLowerCase();
return q ? this.all.filter(s => [s.name, s.region, s.owner].some(v => v.includes(q))) : this.all;
});
// computed — so the column titles follow the language switch
protected readonly columns = computed[]>(() => {
const t = this.t();
return [
{ key: 'name', label: t.name, sort: s => s.name },
{ key: 'region', label: t.region, sort: s => s.region },
{ key: 'status', label: t.status, sort: s => s.status },
{ key: 'cpu', label: t.cpu, sort: s => s.cpu, align: 'end' },
{ key: 'memoryGb', label: t.memory, sort: s => s.memoryGb, align: 'end' },
{ key: 'created', label: t.created, sort: s => s.created },
{ key: 'owner', label: t.owner } // no sort — the header is not a button
];
});
protected readonly byId = (s: Server) => s.id;
protected readonly code = `columns: JtGridColumn[] = [
{ key: 'name', label: 'Name', sort: s => s.name },
{ key: 'status', label: 'Status', sort: s => s.status },
{ key: 'cpu', label: 'CPU', sort: s => s.cpu, align: 'end' },
{ key: 'created', label: 'Created', sort: s => s.created },
{ key: 'owner', label: 'Owner' } // not sortable
];
byId = (s: Server) => s.id;
{{ row.status }}
{{ row.created | date }}
`;
}