【问题标题】:ag-grid-angular not displaying data in a tabular formatag-grid-angular 不以表格格式显示数据
【发布时间】:2018-06-04 07:39:33
【问题描述】:

我正在使用 ag-grid-angular 以表格格式显示数据,但所有数据都被杂乱无章地放在一列中。

我正在使用原始数据来填充 ag-grid。

我的 component.ts 文件

import { Component, OnInit } from '@angular/core';
import {AgGridModule} from 'ag-grid-angular/main';
import {GridOptions} from 'ag-grid';


@Component({
 selector: 'app-main',
 templateUrl: './main.component.html',
 styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
gridOptions: GridOptions;
columnDefs= [
   {headerName: 'Pokemon', field: 'name'},
   {headerName: 'Type', field: 'type'},
   {headerName: 'Price', field: 'price'},

];
rowData = [
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000},
{name : 'Bulbasaur', type: 'plant', price: 1000}
];
constructor() {
this.gridOptions = <GridOptions>{
  rowCount: 10
};
}

ngOnInit() {
}

}

上面的代码中声明了rowData和colDefs

我的component.html代码:

<h1>Grid example</h1>

<ag-grid-angular class = "ag-fresh"
            [rowData]="rowData"
            [columnDefs]="columnDefs"
             [gridOptions]="gridOptions"
            >
 </ag-grid-angular>

我的 app.module.ts 文件:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { MainComponent } from './main/main.component';
import {AgGridModule} from 'ag-grid-angular';


@NgModule({
 declarations: [
 AppComponent,
 MainComponent
],
imports: [
BrowserModule,
AgGridModule.withComponents(MainComponent)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

【问题讨论】:

  • 在我看来您缺少一些与 ag-grid 相关的样式,因为那看起来根本不像 ag-grid?
  • 这是解决方案吗?

标签: angular typescript ag-grid


【解决方案1】:

将以下内容添加到 .angular-cli.json

的样式部分
"../node_modules/ag-grid/dist/styles/ag-grid.css",
"../node_modules/ag-grid/dist/styles/theme-fresh.css",

【讨论】:

    【解决方案2】:

    在底部使用 Angular CLI (https://www.ag-grid.com/ag-grid-angular-angularcli/#creating-a-angular-project-with-ag-grid) 构建 ag-Grid 的教程中,他们告诉您将 ag-Grid 的样式表放入 angular-cli.json 文件中。就像 ishabi 所描述的那样。

    要完成答案而不仅仅是发布链接: 将以下两行添加到您的 .angular-cli.json 文件中:

     "../node_modules/ag-grid/dist/styles/ag-grid.css",
     "../node_modules/ag-grid/dist/styles/ag-theme-balham.css"
    

    根据您使用的主题更改第二行。

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 2018-10-09
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2022-07-11
      • 2014-11-02
      相关资源
      最近更新 更多