【问题标题】:How to change placeholder text in Ag-grid filter?如何更改 Ag-grid 过滤器中的占位符文本?
【发布时间】:2019-02-12 15:30:39
【问题描述】:

我正在使用 Ag-grid,我只想更改过滤器的占位符文本。我需要使用自定义过滤器还是可以通过其他方式完成。请帮助

下面是图片链接。我试图在其中显示我需要更改的占位符的“过滤器”文本。

【问题讨论】:

  • 你能发布一个你的问题的代码示例吗?
  • @Juan 请查看我上面提供的图片链接。
  • 不幸的是,您不能部分修改此内置功能,但您可以为此创建自己的filterComponent。请参阅文档:ag-grid.com/javascript-grid-filter-component
  • @un.spike 所以只是为了改变一个占位符,我们需要创建一个自定义过滤器吗?
  • @gauravpatni 是的

标签: angular ag-grid


【解决方案1】:

很遗憾,默认文本过滤器的 ag-Grid 自定义选项很差。

但是您可以通过简单的 DOM 操作来实现您的目标:

Array.from(document.getElementsByClassName('ag-floating-filter-input')).forEach((obj) => {
  if (obj.attributes['disabled']) { // skip columns with disabled filter
    return;
  }
  obj.setAttribute('placeholder', 'Filter...');
});

这将抓取 ag-Grid(通过 'ag-floating-filter-input')类的所有过滤器文本输入,并将占位符值设置为您的值。

【讨论】:

  • 这个函数应该放在哪里?我能够使用 jquery 获取过滤器值,但我不知道该代码的确切位置。
  • 我使用了 onGridReady() 回调函数。 :)
【解决方案2】:

查看此 StackBlitz:Custom placeholder with ag-grid。 Make 列有一个自定义过滤器。

在你的app.module.ts:

imports:[ BrowserModule, FormsModule, AgGridModule.withComponents([CustomFilterComponent]) ]

在您的 HTML 文件中:

<ag-grid-angular 
    style="width: 500px; height: 500px;" 
    class="ag-theme-balham"
    [enableSorting]="true"
    [enableFilter]="true"
    [rowData]="rowData" 
    [columnDefs]="columnDefs"
    [frameworkComponents]="frameworkComponents"
    >
</ag-grid-angular>

在您的 TS 文件中:

export class AppComponent  {
  name = 'Angular';

  private frameworkComponents;

  constructor() {
    this.frameworkComponents = { customFilter: CustomFilterComponent };
  }

  columnDefs = [
    {headerName: 'Make', field: 'make', filter: "customFilter" },
    {headerName: 'Model', field: 'model' },
    {headerName: 'Price', field: 'price'}
  ];

  rowData = [
    { make: 'Toyota', model: 'Celica', price: 35000 },
    { make: 'Ford', model: 'Mondeo', price: 32000 },
    { make: 'Porsche', model: 'Boxter', price: 72000 }
  ];
}

在您的custom-filter.component.ts 文件中:

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

import {IAfterGuiAttachedParams, IDoesFilterPassParams, IFilterParams, RowNode} from "ag-grid-community";
import {IFilterAngularComp} from "ag-grid-angular";

@Component({
    selector: 'filter-cell',
    template: `
        <div class="container">
            Custom Filter: <input #input (ngModelChange)="onChange($event)" [ngModel]="text" class="form-control" placeholder="Custom placeholder">
        </div>
    `, styles: [
        `
            .container {
                border: 2px solid #22ff22;
                border-radius: 5px;
                background-color: #bbffbb;
                width: 200px;
                height: 50px
            }

            input {
                height: 20px
            }
        `
    ]
})
export class CustomFilterComponent implements IFilterAngularComp {
  private params: IFilterParams;
    private valueGetter: (rowNode: RowNode) => any;
    public text: string = '';

    @ViewChild('input', {read: ViewContainerRef}) public input;

    agInit(params: IFilterParams): void {
        this.params = params;
        this.valueGetter = params.valueGetter;
    }

    isFilterActive(): boolean {
        return this.text !== null && this.text !== undefined && this.text !== '';
    }

    doesFilterPass(params: IDoesFilterPassParams): boolean {
        return this.text.toLowerCase()
            .split(" ")
            .every((filterWord) => {
                return this.valueGetter(params.node).toString().toLowerCase().indexOf(filterWord) >= 0;
            });
    }

    getModel(): any {
        return {value: this.text};
    }

    setModel(model: any): void {
        this.text = model ? model.value : '';
    }

    ngAfterViewInit(params: IAfterGuiAttachedParams): void {
        setTimeout(() => {
            this.input.element.nativeElement.focus();
        })
    }

    onChange(newValue): void {
        if (this.text !== newValue) {
            this.text = newValue;
            this.params.filterChangedCallback();
        }
    }
}

【讨论】:

猜你喜欢
  • 2020-01-17
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2019-04-30
  • 2020-11-04
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
相关资源
最近更新 更多