自定义 Ag-Grid 主题的问题

Issues Customizing the Ag-Grid Themes

如何在现有 Angular 应用程序中自定义 Ag-Grid 主题(例如 ag-theme-material.css)?

缺少他们提供的documentation,因为它没有解释如何执行这些changes/configurations。

如有任何帮助,我们将不胜感激。

添加ag-grid-overrides.scss文件,把你要为ag-grid主题修改的saas变量。 sass 个可用变量的完整列表可以在 link - https://github.com/ag-grid/ag-grid/tree/master/src/styles 中找到。在 component.ts 文件中导入 ag-grid-overrides.scss。您仍然可以为每个组件拥有自己的 .css 文件,如下面的 app.component.css 文件所示。

app.component.ts

import '../ag-grid-overrides.scss';

app.component.html

<ag-grid-angular class="data-grid ag-theme-fresh" [gridOptions]="gridOptions">
</ag-grid-angular>

ag-grid-overrides.scss

// Customize the look and feel of the grid with Sass variables
// Up-to-date list of variables is available in the source code: https://github.com/ag-grid/ag-grid/blob/latest/src/styles/theme-fresh.scss 
$icons-path: "~ag-grid/src/styles/icons/";

// changes the border color
$border-color: #FF0000;

// Changes the font size
$font-size: 14px;

// Changes the font family
//$font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;

// Reverts the cell horizontal padding change between ag-fresh and ag-theme-fresh
//$cell-horizontal-padding: 2px;

// changes the icon color
// $icon-color: red;
//$primary-color: green;

@import '~ag-grid/src/styles/ag-grid.scss';
@import '~ag-grid/src/styles/ag-theme-fresh.scss';

app.component.css(不需要,因为这是 ag-grid-angular 上的自定义 class)

.data-grid {
  width: 500px; height: 300px; margin-bottom: 1rem;
}

angular-cli.json

"styles": [
        "../node_modules/ag-grid/dist/styles/ag-grid.css",
        "../node_modules/ag-grid/dist/styles/ag-theme-fresh.css",
        "styles.scss",
        "ag-grid-overrides.scss"
      ]

在我的例子中,它被使用 "ag-grid-enterprise": "^23.1.0""ag-grid-community": "^23.1. 0" 并为 angular2+ 应用程序创建自定义主题你应该导入到全局样式文件(scss 用于我的情况)来自 ag-grid-community 包的几个 mixins 文件和它将如下所示:

@import '~ag-grid-community/src/styles/ag-grid';
@import '~ag-grid-community/src/styles/ag-theme-base/sass/ag-theme-base';

那么你应该包括基本主题和你可以为这个默认主题覆盖的参数集:

.ag-theme-base {//should have specific name, since sizes doesn't work with custom name
  @include ag-theme-base(
    (
      foreground-color: black,
      background-color: yellow,
    )// this is parameters set object where you can ovveride ag-grid properties 
  );
}

您可以找到的农业网格参数列表link

此外,您可以提取此参数并应用于其他元素(不确定它是否有用,但 ag-grid 允许这样做)

.ag-header-cell {
  background-color: ag-param(foreground-color);
  color: ag-param(background-color);
}

Link 到有关 ag-param 函数的文档 link。 Link 有关主题自定义的文档 link

angular中的用法示例:

<ag-grid-angular
  style="width: 100%; height: 400px;"
  class="ag-theme-base"
  ...
>
</ag-grid-angular>