Blame view

src/app/components/editor.component.ts 1.3 KB
abf7c25f   Yarik   Dropdown
1
2
3
4
  import { Component, ViewContainerRef, ViewChild, AfterViewInit } from '@angular/core';
  
  import { AgEditorComponent } from 'ag-grid-ng2/main';
  
abf7c25f   Yarik   Dropdown
5
6
7
8
  @Component({
      selector: 'editor-cell',
      template: `
          <div #container>
18ac329f   Yarik   Editors
9
              <p *ngFor="let item of data" (click)="onClick(item)" >{{item[this.params.labelCol]}}</p>
abf7c25f   Yarik   Dropdown
10
11
12
13
14
          </div>
      `
  })
  export class EditorComponent implements AgEditorComponent, AfterViewInit {
      @ViewChild('container', {read: ViewContainerRef}) public container;
18ac329f   Yarik   Editors
15
16
      public item: Object = null;
      public data: Object[];
abf7c25f   Yarik   Dropdown
17
18
19
20
21
22
23
      private params: any;
      ngAfterViewInit() {
          this.container.element.nativeElement.focus();
      }
  
      agInit(params: any): void {
          this.params = params;
18ac329f   Yarik   Editors
24
25
26
27
28
29
          if (!this.params.valueCol) {
              this.params.valueCol = 'id';
          }
          if (!this.params.labelCol) {
              this.params.labelCol = 'name';
          }
abf7c25f   Yarik   Dropdown
30
          this.data = params.data || [];
abf7c25f   Yarik   Dropdown
31
32
33
34
35
36
37
38
39
40
      }
  
      getValue(): any {
          return this.item;
      }
  
      isPopup(): boolean {
          return true;
      }
  
18ac329f   Yarik   Editors
41
      setValue(item: Object): void {
abf7c25f   Yarik   Dropdown
42
43
44
          this.item = item;
      }
  
18ac329f   Yarik   Editors
45
      onClick(item: Object) {
abf7c25f   Yarik   Dropdown
46
47
48
49
50
51
52
53
54
          this.setValue(item);
          this.params.api.stopEditing();
      }
  
      onKeyDown(event): boolean {
          event.stopPropagation();
          return false;
      }
  }