UNPKG

angular2-data-table

Version:

angular2-data-table is a Angular2 component for presenting large and complex data.

106 lines (87 loc) 2.26 kB
import { Component } from '@angular/core'; @Component({ selector: 'live-data-demo', template: ` <div> <h3> Live Data Demo <small> <a href="#" (click)="start()">Start</a> | <a href="#" (click)="stop()">Stop</a> | <a href="#" (click)="add()">Add</a> | <a href="#" (click)="remove()">Remove</a> </small> </h3> <swui-datatable #mydatatable class="material" [headerHeight]="50" [limit]="5" [columnMode]="'force'" [footerHeight]="50" [rowHeight]="'auto'" [trackByProp]="'updated'" [rows]="rows"> <swui-datatable-column name="Name"></swui-datatable-column> <swui-datatable-column name="Gender"></swui-datatable-column> <swui-datatable-column name="Company"></swui-datatable-column> </swui-datatable> </div> ` }) export class LiveDataComponent { count: number = 50; rows: any[] = []; active: boolean = true; temp: any[] = []; cols: any = [ 'name', 'gender', 'company' ]; constructor() { this.fetch((data) => { this.rows = data.map(d => { d.updated = Date.now().toString(); return d; }); this.temp = [...this.rows]; }); this.start(); } randomNum(start: number, end: number): number { return Math.floor(Math.random() * end) + start; } start(): void { if(!this.active) return; setTimeout(this.updateRandom.bind(this), 50); } stop(): void { this.active = false; } add() { this.rows.splice(0, 0, this.temp[this.count++]); } remove() { this.rows.splice(0, this.rows.length); } updateRandom() { const rowNum = this.randomNum(0, 5); const cellNum = this.randomNum(0, 3); const newRow = this.randomNum(0, 100); const prop = this.cols[cellNum]; let rows = this.rows; if(rows.length) { let row = rows[rowNum]; row[prop] = rows[newRow][prop]; row.updated = Date.now().toString(); } this.start(); } fetch(cb: any): void { const req = new XMLHttpRequest(); req.open('GET', `assets/data/company.json`); req.onload = () => { cb(JSON.parse(req.response)); }; req.send(); } }