aboutsummaryrefslogtreecommitdiffstats
path: root/demo/angular-grid.ts
blob: e6488fb1c21a1f91997924419da16e55a2a6b6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {Component} from 'angular2/core';

@Component({
  selector: 'angular-grid',
  templateUrl: 'angular-grid.html'
})
export class AngularGrid {
  selected: Object;
  grid = document.querySelector('angular-grid vaadin-grid');
  columns = [
    {name: "user.picture.thumbnail", width: 100, renderer: this.pictureRenderer},
    {name: "user.gender"},
    {name: "user.name.first"},
    {name: "user.name.last"},
    {name: "user.email"},
  ];

  constructor() {
    // Once grid is ready, add a new header row with the gender select in it
    this.grid.then(() =>
      this.grid.header.addRow(1, ['', document.querySelector('angular-grid select')])
    );
  }

  pictureRenderer(cell) {
    cell.element.innerHTML = '<img style="width: 30px" src="' + cell.data + '" />';
  }

  items(params, callback) {
    var gender = document.querySelector('angular-grid select');
    var url = 'https://randomuser.me/api?nat=us&gender='
      + gender.value + '&results=' + params.count;
    getJSON(url, data =>
      callback(data ? data.results : [], gender.value ? 50 : 100)
  }

  onSelect() {
    this.selected = undefined;
    const selectedIndex = this.grid.selection.selected()[0];
    this.grid.getItem(selectedIndex, (err, data) => this.selected = data);
  }

  onFilterChange() {
    this.grid.async(() => this.grid.refreshItems());
    this.grid.scrollToStart();
  }

}