Vas*_*ira 5 javascript json ag-grid angular
我对 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
},
];
}
}
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
数据.json
{
"data" :[
{
"id": "red",
"value": "#f00"
},
{
"id": "green",
"value": "#0f0"
}
]
}
Run Code Online (Sandbox Code Playgroud)
my-grid-application.component.html
<div style="width: 200px;">
<ag-grid-angular #agGrid style="width: 100%; height: 200px;" class="ag-
theme-fresh"
[gridOptions]="gridOptions">
Run Code Online (Sandbox Code Playgroud)
我不是 Ag-Grid 专家,但为什么要在构造函数中使用var gridOptions重新声明 gridOptions 。这是一个明显的错误,应该纠正:
this.gridOptions = {
onGridReady: function() {
this.gridOptions.api.setRowData(this.things);
}
}
Run Code Online (Sandbox Code Playgroud)
因为这是您在模板中访问的属性。
从我的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
},
];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5157 次 |
| 最近记录: |