【问题标题】:Add a class to expanded row using vanilla JS使用 vanilla JS 将类添加到扩展行
【发布时间】:2018-04-29 05:59:57
【问题描述】:

在我的 Angular 应用程序中,我正在尝试一种解决方法,因为 ag-Grid api getRowClass() 没有按预期工作。我需要做的就是在展开的行中添加一个 css 类,并在该行折叠时将其删除。

使用ag-Grid api的原始方法不起作用如下:

setRowNode(params) {
    this.gridOptions.getRowStyle = (params) => {
        if(params.node.expanded) {
            return { background: 'red' };
        }
    }
}  

理想情况下,我将能够选择 DOM 并向其附加一个类。我用一些 JQuery 尝试了这个并且它有效,但出于显而易见的原因,我不想在这个应用程序中使用 JQuery。我按照这些思路写了一些东西:

$('.ag-row[row="'+params.node.id+'"]',self.api.grid.gridPanel.eBodyContainer).addClass('ag-row-focus');

我将如何使用 vanilla JS 来满足这个要求?

【问题讨论】:

  • 看看querySelectorAll()classList

标签: javascript jquery css angular ag-grid


【解决方案1】:

您可以通过创建自定义指令来做到这一点,例如:

//row-focus.directive.ts

import { Directive, HostBinding, HostListener } from '@angular/core';

@Directive({
  selector: '[appRowFocus]' // you can target classes, id etc.: '.grid-Holdings' for example
})
export class RowFocusDirective {
  @HostBinding('class.ag-row-focus') isFocused = false;

  @HostListener('click') toggleOpen() {
    this.isFocused = !this.isFocused;
  }
}

在您的模块上导入此指令,然后将该指令附加到您的元素:

// your-component.component.ts :

<div class="row" appRowFocus>

这些元素将在点击时切换ag-row-focus。您可以为其他事件添加不同的@HostListener

【讨论】:

  • 好吧,我想我理解这个的主旨,但我的 html 看起来像这样:&lt;ag-grid-angular #agGrid style="width: 100%; height:85vh" class="ag-material grid-Holdings" [gridOptions]="gridOptions" [rowData]="rowData" [getRowHeight]="getRowHeight" headerHeight="46" &lt;/ag-grid-angular&gt; 我无权访问该特定行的 DOM。我如何将该指令添加到此网格中?
  • 您可以在指令选择器上定位您想要的内容,例如类、标签等!也许试试. grid-Holdingsag-grid-angular
  • 所以在选择器中我可以这样定位:@Directive({ selector: '[.grid-Holdings]' })
  • 是的,但没有括号:.grid-Holdings
  • 对,这行得通,但是该类被添加到整个&lt;ag-grid-angular&gt;,而不是展开/折叠的单个行...遗憾的是似乎没有办法针对特定行.感谢您对此提供的所有帮助...我学到了一些东西并接近了...
猜你喜欢
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
相关资源
最近更新 更多