PrimeNG TreeTable为空

时间:2017-06-23 13:44:14

标签: angular primeng primeng-treetable

为了在Angular 4.1项目中使用PrimeNG,我遇到了一些问题。

以下是我关注的文档:https://www.primefaces.org/primeng/#/treetable

我正在测试TreeTable功能,但UI中的输出为空。它显示没有内容的单元格,并且class =“undefined”。

Empty Table

app.component.html:

<p-treeTable *ngIf="treeNode" [value]="treeNode">
  <p-column field="id" header="ID"></p-column>
  <p-column field="label" header="Label"></p-column>
</p-treeTable>

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { TreeTableModule, SharedModule } from 'primeng/primeng';

import { AppComponent } from './app.component';
import { CustomerTypeService } from '../app-configure/service/app.customer.type.service';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    CommonModule,
    BrowserModule,
    HttpModule,
    BrowserAnimationsModule,
    TreeTableModule,
    SharedModule
  ],
  providers: [CustomerTypeService],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts:

import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/primeng';

import { CustomerTypeService } from '../app-configure/service/app.customer.type.service';

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

  constructor(private customerTypesService: CustomerTypeService) {}

  ngOnInit(): void {
    this.customerTypesService.getCustomerTypes()
        .then(treeNode => {
          this.treeNode = treeNode;
          console.log(this.treeNode);
        });
  }
}

以下是我从HTTP请求中获取的JSON字符串:[{"id":1,"label":"Account","parentId":null,"parentLabel":null},{"id":2,"label":"Bank Account","parentId":1,"parentLabel":"Account"},{"id":3,"label":"Test","parentId":null,"parentLabel":null}]

app.customertype.service.ts:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

import {DefaultConfiguration} from '../../app/app.defaults';
import {CustomerType} from '../model/customer.type';
import {TreeNode} from 'primeng/primeng';

@Injectable()
export class CustomerTypeService {
    constructor(private http: Http) {}

    /**
     * Returns an array of all Customer Types.
     * @returns {Promise<CustomerType[]>}
     */
    getCustomerTypes(): Promise<TreeNode[]> {
        return this.http
            .get(`${DefaultConfiguration.BASE_API_URL}/CustomerTypes/`, DefaultConfiguration.getHeaders())
            .toPromise()
            .then(response => <TreeNode[]> response.json())
            .catch(DefaultConfiguration.handleError);
    }
}

1 个答案:

答案 0 :(得分:1)

中添加* ngIf =“treeNode”
private boolean isUIThread(){
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

public void handleUncaughtException(Thread thread, Throwable e) {
    if(isUIThread()) {
        // exception occurred from UI thread

    } else {  //handle non UI thread throw uncaught exception
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                //handle non UI thread throw uncaught exception

            }
        });
    }
}

对象TreeNode实现此接口

handleUncaughtException()

你的web服务正在返回一个没有实现接口TreeNode的元素数组,它没有属性id。

此数组

<p-treeTable [value]="treeNode" *ngIf="treeNode">...</p-treeTable>

不是TreeNode []