【问题标题】:My ag-grid doesnt show up any Data我的 ag-grid 没有显示任何数据
【发布时间】:2018-08-07 07:23:58
【问题描述】:

我对 Angular/Typescript 相当陌生。我有点学习和发展的同时。我试图用从 Json 文件加载的数据构建一个网格,并且在显示我的数据时遇到了一些问题。如果你们能指出我的错误,我会很高兴,因为我的代码编译时没有错误,我现在有点无助。

我将在下面提供我的代码。 提前致谢。

my-grid-application.component.ts

@Component({
  selector: 'app-my-grid-application',
  templateUrl: './my-grid-application.component.html'
})
export class MyGridApplicationComponent {
  private gridOptions: GridOptions;
  things:Things[];

  getThings(){
    this.myGridApplicationService.getThings().subscribe( things => 
this.things = things)
  }

constructor( private myGridApplicationService: MyGridApplicationService) {
    this.gridOptions = <GridOptions>{};
    var gridOptions = {
        onGridReady: function() {
    this.gridOptions.api.setRowData(this.things);
        }
    }
    this.gridOptions.columnDefs = [
        {
            headerName: "ID",
            field: "id",
            width: 100
        },
        {
            headerName: "Value",
            field: "value",
            cellRendererFramework: RedComponentComponent,
            width: 100
        },

    ]; 
  } 
}

my-grid-application.service.ts

export class Things{

}

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

  getThings(){
    return this.http.get('src/assets/data.json')
        .map((response:Response)=> <Things[]>response.json().data)
  }
}

数据.json

{
"data" :[
    {
        "id": "red",
        "value": "#f00"
    },
    {
        "id": "green",
        "value": "#0f0"
    }
]
}

my-grid-application.component.html

<div style="width: 200px;">
  <ag-grid-angular #agGrid style="width: 100%; height: 200px;" class="ag-
theme-fresh"
           [gridOptions]="gridOptions">

【问题讨论】:

    标签: javascript json angular ag-grid


    【解决方案1】:

    我不是 Ag-Grid 专家,但为什么要在构造函数中使用 var gridOptions 重新声明 gridOptions。这是明显的错误,应该纠正:

    this.gridOptions = {
        onGridReady: function() {
            this.gridOptions.api.setRowData(this.things);
        }
    }
    

    因为这是您在模板中访问的属性。

    在我的Github 中查看这个StackBlitz

    //MyGridApplication
    constructor( private myGridApplicationService: MyGridApplicationService) {
        myGridApplicationService.getThings()
            .subscribe( things => this.things = things);
    
        this.gridOptions = <GridOptions>{};
        this.gridOptions = {
            onGridReady: () => {
                this.gridOptions.api.setRowData(this.things);
            }
        };
    
        this.gridOptions.columnDefs = [
            {
                headerName: "ID",
                field: "id",
                width: 100
            },
            {
                headerName: "Value",
                field: "value",
                cellRendererFramework: RedComponentComponent,
                width: 100
            },
    
        ]; 
    } 
    

    【讨论】:

    • 需要定义 var gridOptions 否则他将无法识别 .api 方法来设置数据。谢谢你的回答
    • 问题是我不想将原始数据编码在组件上,我只想从服务器/json 文件中加载它,因为我有数千行要添加到我的网格中。跨度>
    • 我现在了解您的问题,所以当您注入服务 myGridApplicationService 时,如果您想获取信息,您可以将 getThings() 方法中的代码复制到构造函数中,或者调用 get things from在你开始使用它之前你的构造函数。查看 Answer 中的代码块
    • @VascoOliveira 不客气!很高兴我在尝试学习这个产品时也有用。
    【解决方案2】:

    当 onGridReady() 被触发时,“this.things”还没有被填充。 试试这个:

    var gridOptions = {
        rowData: this.things
    }
    

    【讨论】:

    • 这对我不起作用。它一直在我的网格上显示“正在加载”。谢谢你
    • 你在哪里调用 getThings()?尝试在构造函数中调用它。
    • 你能把“my-grid-application.component.html”的内容贴出来吗?
    猜你喜欢
    • 2017-06-28
    • 2020-07-12
    • 2016-08-02
    • 2017-09-16
    • 1970-01-01
    • 2021-10-09
    • 2019-07-03
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多