【问题标题】:Import Pivottable js in angular 2以角度 2 导入 Pivottable js
【发布时间】:2017-10-18 18:37:49
【问题描述】:

我正在学习 Angular 2 并尝试在 Angular 2 应用程序中导入 nicolas kruchten 的 Pivottable js

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

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

我认为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 { }

【问题讨论】:

  • 我看不到 AfterViewInit 是如何使用的。我认为它应该被实现并调用 buildPivot() ,对吗?
  • @cmonkey 这是一个愚蠢的错误,因为构造函数的拼写不正确。但是现在当我尝试使用pivotUI 函数时,它会出现渲染错误
  • 这是一个将 react 与 pivottable github.com/kurtzace/pivottableandreact 一起使用的示例(读作在 typecipt 生态系统中工作的 pivottobale)- 可能无法回答您的问题 - 但可能会给您提示

标签: javascript angular typescript pivottable.js


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请看下面的解决方案: 我正在使用角度剪辑

    --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>
    

    【讨论】:

    • 我认为这里有一个错字,您想在“样式”中包含“pivot.css”而不是 .js 文件:[“../node_modules/pivottable/dist/pivot. min.js”,对吧?
    • 也在“node_modules/jquery-ui/dist/jquery-ui.min.js”中,
    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 2016-12-19
    • 2017-10-19
    • 1970-01-01
    • 2019-03-05
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多