【问题标题】:PrimeNG TreeTable EmptyPrimeNG 树表空
【发布时间】:2017-06-23 13:44:13
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你能用答案升级你的问题吗?

标签: angular primeng primeng-treetable


【解决方案1】:

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

对象TreeNode实现了这个接口

export interface TreeNode {
 label?: string;
 data?: any;
 icon?: any;
 expandedIcon?: any;
 collapsedIcon?: any;
 children?: TreeNode[];
 leaf?: boolean;
 expanded?: boolean;
 type?: string;
 parent?: TreeNode;
 partialSelected?: boolean;
 styleClass?: string;
 draggable?: boolean;
 droppable?: boolean;
 selectable?: boolean;
}

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

这个数组

[{"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}]

不是 TreeNode[]

【讨论】:

  • 这似乎在桌子之外没有任何区别,所有这些都一起出现。最初,它首先加载标题行,一旦承诺响应,内容就会弹出。
  • 您可以将服务 CustomerTypeService 从组件移动到 @NgModule.providers 中的模块
  • 我把它移到提供的 ngModules 中,但仍然没有成功。我用更改更新了原始帖子。
  • 服务正常吗?,你可以使用订阅 this.customerTypesService.getCustomerTypes().subscribe((data)=>{this.treeNode=data});
  • 我觉得服务还可以。我得到了我预期的回应。如果您想看一下,我将其添加到原始帖子中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 2021-05-12
相关资源
最近更新 更多