2017-05-18 10 views
0

Ich lerne Angular 2 und versuche nicolas kruchten des Pivot-Tabelle js in einem Winkel 2 AnwendungImport Pivot-Tabelle js in Winkel 2

Wenn ich versuche zu wickeln Pivot-Tabelle js in dem Winkel 2 Es ist nicht gibt jeder importieren Fehler jedoch, wenn ich versuche, das gleiche im Browser zu überprüfen Es zeigt die Pivot-Tabelle nicht an.

Ich kann überprüfen, dass die Komponente erstellt wird, aber immer leer ist.

Ich denke, AfterViewInit sollte ich Tabelle anzeigen lassen.

\\ pivot.component.ts

import {Component , Inject, ElementRef, AfterViewInit} from '@angular/core'; 

declare var jQuery:any; 
declare var $:any; 
import 'pivottable/dist/pivot.min.js'; 
import 'pivottable/dist/pivot.min.css'; 
@Component({ 
    selector: 'app-pivot', 
    template: `<div id="pivot"></div>` 
}) 

export class PivotWrapper { 

private el: ElementRef; 
//constructor spelling was incorrect now working fine 
constructor (@Inject(ElementRef)el: ElementRef){ 
    this.el = el; 
} 
ngAfterViewInit(){ 

    if (!this.el || 
     !this.el.nativeElement || 
     !this.el.nativeElement.children){ 
      console.log('cant build without element'); 
      return; 
    } 

    var container = this.el.nativeElement; 
    var inst = jQuery(container); 
    var targetElement = inst.find('#pivot'); 

    if (!targetElement){ 
     console.log('cant find the pivot element'); 
     return; 
    } 

    //this helps if you build more than once as it will wipe the dom for that element 
    while (targetElement.firstChild){ 
     targetElement.removeChild(targetElement.firstChild); 
    } 

    //here is the magic 
    targetElement.pivot([ 
      {color: "blue", shape: "circle"}, 
      {color: "red", shape: "triangle"} 
     ], 
     { 
      rows: ["color"], 
      cols: ["shape"] 
     }); 
} 

}

\\ app.component.ts

import { Component,ElementRef,Inject} from '@angular/core'; 

declare var jQuery: any; 
import {PivotWrapper} from './pivot.component'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent {} 

\\ app.module.ts

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 
import { MaterialModule } from '@angular/material'; 
import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; 
import {NoopAnimationsModule} from '@angular/platform-browser/animations'; 
import { AppComponent } from './app.component'; 
import {PivotWrapper} from './pivot.component'; 

@NgModule({ 
    declarations: [ 
    AppComponent, PivotWrapper 
    ], 
    imports: [ 
    BrowserModule, 
    BrowserAnimationsModule, 
    NoopAnimationsModule, 
    FormsModule, 
    HttpModule, 
    MaterialModule.forRoot() 
    ], 
    providers: [], 
    bootstrap: [AppComponent, PivotWrapper] 
}) 
export class AppModule { } 
+0

Ich sehe nicht, wie AfterViewInit verwendet wird. Ich nehme an, es sollte implementiert werden und buildPivot() aufrufen, ist das richtig? – cmonkey

+0

@cmonkey Es war ein dummer Fehler, da die Schreibweise für den Konstruktor falsch war. Aber jetzt, wenn ich versuche, "PivotUI" -Funktion zu verwenden, gibt es Rendering-Fehler – user2881430

Antwort

0

Siehe unten für die Lösung: Ich verwende Winkel cli

--package.json

"jquery": "^3.2.1", 
"jquery-ui-dist": "^1.12.1",  
"pivottable": "^2.13.0", 

--angular-cli.json

"styles": [ 
    "../node_modules/pivottable/dist/pivot.min.js", 
    "styles.css" 
    ], 
    "scripts": ["../node_modules/jquery/dist/jquery.min.js", 
    "../node_modules/jquery-ui-dist/jquery-ui.min.js", 
    "../node_modules/pivottable/dist/pivot.min.js" 

    ], 

--app-Pivot-wrapper

import { Component, OnInit,Inject, ElementRef, AfterViewInit } from '@angular/core'; 

import 'pivottable/dist/pivot.min.js'; 
import 'pivottable/dist/pivot.min.css'; 
declare var jQuery:any; 
declare var $:any; 

@Component({ 
selector: 'app-pivot-wrapper', 
templateUrl: './pivot-wrapper.component.html', 
styleUrls: ['./pivot-wrapper.component.css'] 
}) 

export class PivotWrapperComponent implements OnInit { 
private el: ElementRef; 
constructor(@Inject(ElementRef)el: ElementRef) { 
    this.el = el; 

} 

ngOnInit() {  
} 

ngAfterViewInit(){ 

if (!this.el || 
    !this.el.nativeElement || 
    !this.el.nativeElement.children){ 
     console.log('cant build without element'); 
     return; 
} 

    var container = this.el.nativeElement; 
    var inst = jQuery(container); 
    var targetElement = inst.find('#output'); 

    if (!targetElement){ 
    console.log('cant find the pivot element'); 
    return; 
    } 


while (targetElement.firstChild){ 
    targetElement.removeChild(targetElement.firstChild); 
    } 

    //here is the magic 
    targetElement.pivot([ 
     {color: "blue", shape: "circle"}, 
     {color: "red", shape: "triangle"} 
    ], 
    { 
     rows: ["color"], 
     cols: ["shape"] 
    }); 
    } 
} 

--pivot-wrapper.component.html

<div id="output"></div> 
0

Dies ist ein Beispiel von mit Pivot-Tabelle reagiert mit https://github.com/kurtzace/pivottableandreact (als pivottobale Arbeiten in einem typescipt Ökosystem lesen) - kann Ihre Frage nicht beantworten - kann aber geben Sie Hinweise