【问题标题】:Ngx-datatable cellClass not workingNgx-datatable cellClass 不起作用
【发布时间】:2022-04-06 16:34:32
【问题描述】:

我尝试将我的自定义 css 附加到 ngx-datatable 单元格。

<ngx-datatable-column 
  *ngFor="let column of tableOptions.columnDefs"
  [cellClass]="'my-custom-cell'"
  ...

my-custom-cell 的 CSS:

.my-custom-cell{
  padding: 0;
}

在开发工具中我可以看到 datatable-body-cell 有类

class="datatable-body-cell my-custom-cell sort-active"

问题是没有覆盖css

【问题讨论】:

    标签: angular ngx-datatable


    【解决方案1】:

    您可能遇到了关于视图封装的问题。

    ngx-datatable 在其样式上使用 ViewEncapsulation.None。您可以将您的 .my-custom-cell 样式定义添加到 ./src/styles.(s)css 或者您也可以set your component's encapsulation to None

    如果您还不知道view encapsulation from angular guide的详细信息,您可以查看它。


    编辑 (回复Tom's comment

    我也不想要这样的解决方案,但不幸的是,这似乎就是这样...... 我已经准备好了a demo app,它显示了我想要表达的内容。

    .my-custom-cell 分配给名称列。

    .my-custom-cell-global 分配给性别列。

    ./app/demo-component.ts

    <ngx-datatable-column name="name" [cellClass]="'my-custom-cell'">
      <ng-template ngx-datatable-cell-template let-value="value">
        {{value}}
      </ng-template>
    </ngx-datatable-column>
    <ngx-datatable-column name="gender" [cellClass]="'my-custom-cell-global'">
      <ng-template ngx-datatable-cell-template let-value="value">
        {{value}}
      </ng-template>
    </ngx-datatable-column>
    

    ./app/demo-component.scss

    .my-custom-cell {
      background: red;
    }
    

    ./styles.scss

    .my-custom-cell-global {
      background: #e6f2ff;
    }
    

    如您所见,红色背景未分配给名称列。但性别列的背景颜色为蓝色。

    这是因为视图封装告诉 cli 编译 .my-custom-cell 类,如下所示:

    .my-custom-cell[_ngcontent-c176]{background:red}
    

    但是DatatableComponent is using ViewEncapsulation.None。如果您检查名称列中的一个单元格,那么您可以看到具有 .my-custom-cell 类的 datatable-body-cell 没有 [_ngcontent-c176] 属性。所以这个类没有被应用。

    如果您取消注释encapsulation line:46,那么您会看到 [_ngcontent-c176] 属性已从已编译的 .my-custom-cell 中消失,并且 name 列成功获取样式。

    如果您找到其他合适的方式,请告诉我。

    【讨论】:

    • [cellClass] 或 [rowClass] 应避免将 ViewEncapsulation 设置为 None 之类的事情。这不是我想要的。
    • 编辑是一个不太合适的解决方案,因为它意味着修改库。不过,之前的反响还不错。谢谢!
    • 我使用函数隐藏模板文件中的列,&lt;ngx-datatable-column [cellClass]="showColumn" [width]="20" [flexGrow]="0.5"&gt; 在代码文件中,showColumn({ row, column, value }) { return (row.status === 'Open') ? ' hideColumn' : ''; } 在styles.scss 中,.hideColumn { display: none !important; }
    【解决方案2】:

    以下作品适合我:

    :host /deep/ .my-custom-cell {
      background: red;
    }
    

    【讨论】:

    • 它已经解决了,而这些 deepng-deep 看起来很糟糕
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2018-10-05
    • 2021-07-27
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多