网格样式 - 覆盖 ag-grid 的样式

Grid Styling - Overwrite style of ag-grid

我有以下风格:

.ag-theme-fresh .ag-row-selected {
    background-color: #bde2e5; 
}`

它来自一个主题的css样式文件。但我想用这种风格覆盖它:

.ag-theme-fresh .ag-row-even .ag-row-selected {
  background-color: #1428df;
}

但是没有效果,我的组件使用的是第一种风格。如何覆盖第一个样式 1?我试过 !important 但它什么也没做。

我应该在 css 文件的开头定义自定义样式吗?

更新:

我发现我可以使用函数 gridOptions.getRowClass 来设置要使用的样式 class。但我想解决中心问题(对于我在我的应用程序中使用的所有 angular 网格)。有什么想法吗?

你应该使用ViewEncapsulation

只需添加到您的组件 encapsulation: ViewEncapsulation.None:

import { Component, ViewEncapsulation } from "@angular/core";

@Component({
    selector: '....',
    templateUrl: '....',
    styles: [`
        .ag-theme-fresh .ag-row-selected {
            background-color: #1428df !important;
        }
    `],
    encapsulation: ViewEncapsulation.None
})

要覆盖 ag-grid 使用您需要使用 ng-deep 作为 angular 组件中定义的样式不要覆盖 ag-grid css

:host ::ng-deep .ag-header-cell-label {
  justify-content: center;
}

如果您使用 sass 或 scss,您可以在样式中覆盖。scss/sass。这将适用于所有地方

@import "../node_modules/ag-grid-enterprise/dist/styles/ag-grid.scss";
@import "../node_modules/ag-grid-enterprise/dist/styles/ag-theme-alpine/sass/ag-theme-alpine-mixin";

.ag-theme-alpine {
  @include ag-theme-alpine(
    (
      // use theme parameters where possible
        alpine-active-color: #c066b2,
    )
  );
    .ag-header-cell-label {
        justify-content: center;
      }
    }

如果您需要在特定网格上进行操作,请选择自定义 class 并为 ag-grid 制作 sub-class。

您还可以全局应用样式,如果这样做,它将覆盖所有 ag-Grid 组件的样式。如果您尝试单独设置组件的样式,这可能不是一个选项,但这是提供全局基本样式覆盖的好方法。

此外,您应该尝试使用更具体的选择器,而不是使用重要的选择器。

这是一个例子:

.ag-theme-alpine > .ag-row.ag-row-selected {
  background : red;
}