以角度2导入数据透视js

时间:2017-05-18 19:31:42

标签: javascript angular typescript pivottable.js

我正在学习Angular 2并尝试在角度2应用程序中导入nicolas kruchten的数据透视js

当我试图在角度2中包装pivottable js时它没有给出任何错误但是当我尝试在浏览器中检查它时它没有显示数据透视表。

我可以检查组件是否已创建,但始终为空。

我认为AfterViewInit应该让我显示表格。

\\ 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 { }

3 个答案:

答案 0 :(得分:1)

请参阅下面的解决方案: 我正在使用角度cli

<强> - 的package.json

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

<强> - 角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-枢轴包装

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"]
     });
  }
 }

<强> - 枢轴wrapper.component.html

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

答案 1 :(得分:0)

这是使用与pivottable https://github.com/kurtzace/pivottableandreact的反应的一个示例(读作在typescipt生态系统中工作的pivottobale) - 可能无法回答您的问题 - 但可能会给您提示

答案 2 :(得分:0)

我认为这对处理没有CSS的数据透视表的人会有所帮助。 我没有意识到我忘记在pivot table css中加入angular-cli.json

enter image description here