angular2-data-table
Version:
angular2-data-table is a Angular2 component for presenting large and complex data.
106 lines (87 loc) • 2.26 kB
text/typescript
import { Component } from '@angular/core';
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();
}
}