简体   繁体   English

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

[英]Grid Styling - Overwrite style of ag-grid

I have the following style:我有以下风格:

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

It comes from a css style file of a theme.它来自一个主题的 css 样式文件。 But I want to overwrite it with this style:但我想用这种风格覆盖它:

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

But it has not effect and my component uses the first style.但它没有效果,我的组件使用第一种样式。 How can I overwrite the first style 1?如何覆盖第一个样式 1? I tried with !important but it does nothing.我试过!important但它什么也没做。

Should I define my custom style at the beginning of the css file?我应该在 css 文件的开头定义我的自定义样式吗?

UPDATE:更新:

I found I can use the function gridOptions.getRowClass to set the style class to be used.我发现我可以使用函数 gridOptions.getRowClass 来设置要使用的样式类。 But I would like to solve the issue central (for all the angular grids that I use in my application).但我想解决中心问题(对于我在应用程序中使用的所有角网格)。 Any idea?任何的想法?

You should use ViewEncapsulation你应该使用ViewEncapsulation

Just add to your component encapsulation: ViewEncapsulation.None :只需添加到您的组件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
})

To override the ag-grid use you need to use ng-deep as the style defined in angular component do not overide ag-grid css要覆盖 ag-grid 使用,您需要使用 ng-deep 作为 angular 组件中定义的样式不要覆盖 ag-grid css

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

if you are using sass or scss you could override in the style.scss/sass.如果您使用 sass 或 scss,您可以在 style.scss/sass 中覆盖。 this would be applicable at all places这将适用于所有地方

@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;
      }
    }

if you have need of doing at a specific grid, prefer custom class and make sub-class for the ag-grid.如果您需要在特定网格上进行操作,请更喜欢自定义类并为 ag-grid 创建子类。

You can also apply the styles globally and if you do so it will override the styles for all your ag-Grid components.您还可以全局应用样式,如果这样做,它将覆盖所有 ag-Grid 组件的样式。 This might not be an option if you are trying to style the components individually, but it's a good way to give a global base style override.如果您尝试单独设置组件的样式,这可能不是一个选项,但这是提供全局基本样式覆盖的好方法。

Also, you should try to use more specific selectors instead of using important.此外,您应该尝试使用更具体的选择器,而不是使用重要的。

Here is an example:下面是一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM