gpt4 book ai didi

javascript - 我的农业网格没有显示任何数据

转载 作者:太空狗 更新时间:2023-10-29 17:58:38 25 4
gpt4 key购买 nike

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

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

我的网格应用程序.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
},

];
}
}

我的网格应用程序.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"
}
]
}

我的网格应用程序.component.html

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

最佳答案

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

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

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

检查这个StackBlitz来 self 的 Github

//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
},

];
}

关于javascript - 我的农业网格没有显示任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com