简体   繁体   English

向 Angular Material 表中的列添加投影

[英]Add a drop shadow to a column in an Angular Material table

Is it possible to add a drop shadow to the 1st column of an Angular Material table?是否可以向 Angular Material 表的第一列添加投影?

I have tried a couple approaches with ::ng-deep or adding classes, but without much success.我尝试了几种使用 ::ng-deep 或添加类的方法,但没有取得多大成功。

Below is my basic code.下面是我的基本代码。 The right border via the CSS shows in the table in the 1st column, but not the shadow.通过 CSS 的右边框显示在第一列的表格中,但不是阴影。 And the moment I start scrolling to the right, the border disappears (my 1st column is sticky).当我开始向右滚动时,边框消失了(我的第一列是粘性的)。

I would like to add a drop shadow to the 1st column, that will not be removed on scrolling.我想在第一列添加阴影,滚动时不会删除。

CSS CSS

.column-shadow {
  border-right: 1px solid $af-pinkish-grey !important;
  box-shadow: 3px 3px 5px rgba(68, 68, 68, 0.6) !important;
}

HTML HTML

<!-- action buttons -->
<ng-container matColumnDef="select" sticky>
  <th mat-header-cell *matHeaderCellDef>
    Action
  </th>
  <td
    [ngClass]="'column-shadow'"
    mat-cell
    *matCellDef="let row; let index = index"
  >
    <!-- Select record -->
    <mat-checkbox
      color="primary"
      (click)="$event.stopPropagation()"
      (change)="selectRecord($event.checked, index)"
      [checked]="selection.isSelected(row)"
      [aria-label]="checkboxLabel(row)"
    >
    </mat-checkbox>

    <span class="delete-edit-row">
      <!-- Delete single record -->
      <a
        class="action-button"
        mat-button
        (click)="deleteSingleRecord(index)"
        ><img
          class="close-button-image"
          src="../../../assets/images/trashgrid.svg"
      /></a>
    </span>
  </td>
</ng-container>

Edit编辑

Your way of applying css seems correct.您应用 css 的方式似乎是正确的。 For me the only reason it doesn't apply the css could be对我来说,它不适用 css 的唯一原因可能是

  1. My old answer我的旧答案
  2. Your css not being applied at all because of the shadow-dom由于shadow-dom,您的 css 根本没有被应用

Shadow dom fix阴影 dom 修复

Please, try adding your css in a global scope, for example in a styles.scss file that is imported in the root of your project.请尝试在全局范围内添加您的 css,例如在项目根目录中导入的styles.scss文件中。

If it works, you shall try the remove the shadow dome of your component by adding encapsulation: ViewEncapsulation.None to your @component directive.如果它有效,您应该尝试通过将encapsulation: ViewEncapsulation.None添加到您的@component指令来移除组件的阴影圆顶。

Old老的

It seems like you aren't using [ngClass] properly.您似乎没有正确使用[ngClass] If you're trying to add a class, then do [class] and not [ngClass] .如果你想添加一个类,那么做[class]而不是[ngClass]

this answer did explained the difference between the both but basically.这个答案确实解释了两者之间的区别,但基本上。

But you should change your code to this one.但是你应该把你的代码改成这个。

 <td
    [class.column-shadow]="true"
    mat-cell
    *matCellDef="let row; let index = index"
  >
  <!-- Or -->
  <td
    class="column-shadow"
    mat-cell
    *matCellDef="let row; let index = index"
  >
  <!-- Or -->
  <td
    [ngClass]="{'column-shadow': true}"
    mat-cell
    *matCellDef="let row; let index = index"
  >

[ngClass] [ngClass]

NgClass should receive an object with class names as keys and expressions that evaluate to true or false. NgClass应该接收一个以类名作为键和计算结果为真或假的表达式的对象。 extra-sparkle is the key and isDelightful is the value (boolean). extra-sparkle 是关键,isDelightful 是值(布尔值)。

<div [ngClass]="{'column-shadow': true}">

[class] [班级]

on the right that should evaluate to true or false to determine if the class should be applied.应评估为 true 或 false 以确定是否应应用该类的右侧。

<div [class.column-shadow]="true">

Additional tip附加提示

Do avoid using !important in your css since it is considering being a bad practice.请避免在您的 css 中使用!important ,因为它被认为是一种不好的做法。 If you have to, then please do add a comment ;)如果必须,请添加评论;)

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

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