Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit dd10513

Browse files
authored
Changed: container should be a part of component template. #85 (#132)
1 parent 704cdc7 commit dd10513

2 files changed

Lines changed: 24 additions & 48 deletions

File tree

lib/ce/src/hot-table.component.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import {
2+
AfterContentInit,
23
Component,
3-
ElementRef,
4+
Input,
45
NgZone,
5-
AfterContentInit,
66
OnChanges,
77
OnDestroy,
8-
OnInit,
98
SimpleChanges,
9+
ViewChild,
1010
ViewEncapsulation,
11-
Input,
1211
} from '@angular/core';
1312
import * as Handsontable from 'handsontable';
1413
import { HotTableRegisterer } from './hot-table-registerer.service';
@@ -18,13 +17,14 @@ import { HotColumnComponent } from './hot-column.component';
1817
@Component({
1918
// tslint:disable-next-line
2019
selector: 'hot-table',
21-
template: '',
20+
template: '<div #container [id]="hotId"></div>',
2221
encapsulation: ViewEncapsulation.None,
2322
providers: [ HotTableRegisterer, HotSettingsResolver ],
2423
})
25-
export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy, OnInit {
24+
export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy {
25+
@ViewChild('container') public container;
26+
2627
private hotInstance: Handsontable;
27-
private container: HTMLDivElement;
2828
private columnsComponents: HotColumnComponent[] = [];
2929
// component inputs
3030
@Input() settings: Handsontable.GridSettings;
@@ -275,22 +275,11 @@ export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy
275275
@Input() unmodifyRow: (row: number) => void;
276276

277277
constructor(
278-
private el: ElementRef,
279278
private _ngZone: NgZone,
280279
private _hotTableRegisterer: HotTableRegisterer,
281-
private _hotSettingsResolver: HotSettingsResolver
280+
private _hotSettingsResolver: HotSettingsResolver,
282281
) {}
283282

284-
ngOnInit(): void {
285-
this.container = document.createElement('div');
286-
287-
if (this.hotId) {
288-
this.container.id = this.hotId;
289-
}
290-
291-
this.el.nativeElement.appendChild(this.container);
292-
}
293-
294283
ngAfterContentInit(): void {
295284
const options: Handsontable.GridSettings = this._hotSettingsResolver.mergeSettings(this);
296285

@@ -305,7 +294,7 @@ export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy
305294
}
306295

307296
this._ngZone.runOutsideAngular(() => {
308-
this.hotInstance = new Handsontable(this.container, options);
297+
this.hotInstance = new Handsontable(this.container.nativeElement, options);
309298
});
310299

311300
if (this.hotId) {
@@ -324,14 +313,13 @@ export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy
324313
}
325314

326315
ngOnDestroy(): void {
327-
this.hotInstance.destroy();
316+
this._ngZone.runOutsideAngular(() => {
317+
this.hotInstance.destroy();
318+
});
328319

329320
if (this.hotId) {
330321
this._hotTableRegisterer.removeInstance(this.hotId);
331322
}
332-
333-
this.container.parentElement.removeChild(this.container);
334-
this.container = void 0;
335323
}
336324

337325
updateHotTable(newSettings: Handsontable.GridSettings): void {

lib/pro/src/hot-table.component.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import {
2+
AfterContentInit,
23
Component,
3-
ElementRef,
4+
Input,
45
NgZone,
5-
AfterContentInit,
66
OnChanges,
77
OnDestroy,
8-
OnInit,
98
SimpleChanges,
109
ViewEncapsulation,
11-
Input,
10+
ViewChild,
1211
} from '@angular/core';
1312
import * as Handsontable from 'handsontable-pro';
1413
import { HotTableRegisterer } from './hot-table-registerer.service';
@@ -18,14 +17,15 @@ import { HotColumnComponent } from './hot-column.component';
1817
@Component({
1918
// tslint:disable-next-line
2019
selector: 'hot-table',
21-
template: '',
20+
template: '<div #container [id]="hotId"></div>',
2221
encapsulation: ViewEncapsulation.None,
2322
providers: [ HotTableRegisterer, HotSettingsResolver ],
2423
})
2524

26-
export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy, OnInit {
25+
export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy {
26+
@ViewChild('container') public container;
27+
2728
private hotInstance: Handsontable;
28-
private container: HTMLDivElement;
2929
private columnsComponents: HotColumnComponent[] = [];
3030
// component inputs
3131
@Input() settings: Handsontable.GridSettings;
@@ -308,22 +308,11 @@ export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy
308308
@Input() hiddenRow: (row: number) => void;
309309

310310
constructor(
311-
private el: ElementRef,
312311
private _ngZone: NgZone,
313312
private _hotTableRegisterer: HotTableRegisterer,
314-
private _hotSettingResolver: HotSettingsResolver
313+
private _hotSettingResolver: HotSettingsResolver,
315314
) {}
316315

317-
ngOnInit(): void {
318-
this.container = document.createElement('div');
319-
320-
if (this.hotId) {
321-
this.container.id = this.hotId;
322-
}
323-
324-
this.el.nativeElement.appendChild(this.container);
325-
}
326-
327316
ngAfterContentInit(): void {
328317
const options: Handsontable.GridSettings = this._hotSettingResolver.mergeSettings(this);
329318

@@ -338,7 +327,7 @@ export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy
338327
}
339328

340329
this._ngZone.runOutsideAngular(() => {
341-
this.hotInstance = new Handsontable(this.container, options);
330+
this.hotInstance = new Handsontable(this.container.nativeElement, options);
342331

343332
if (this.hotId) {
344333
this._hotTableRegisterer.registerInstance(this.hotId, this.hotInstance);
@@ -358,14 +347,13 @@ export class HotTableComponent implements AfterContentInit, OnChanges, OnDestroy
358347
}
359348

360349
ngOnDestroy(): void {
361-
this.hotInstance.destroy();
350+
this._ngZone.runOutsideAngular(() => {
351+
this.hotInstance.destroy();
352+
});
362353

363354
if (this.hotId) {
364355
this._hotTableRegisterer.removeInstance(this.hotId);
365356
}
366-
367-
this.el.nativeElement.removeChild(this.container);
368-
this.container = void 0;
369357
}
370358

371359
updateHotTable(newSettings: Handsontable.GridSettings): void {

0 commit comments

Comments
 (0)