5

如何在我的 agGrid 组件中设置过滤。我从 agGrid 中看到了一个示例,但它是用 javascript 编写的。 https://www.ag-grid.com/angular-grid-filtering/index.php

但似乎我无法使用下面的 ts 代码完成这项工作。

agGrid.component.ts

import {Component} from 'angular2/core';
import {AgGridNg2} from 'ag-grid-ng2/main';
import {GridOptions} from 'ag-grid/main';

import 'ag-grid-enterprise/main';

@Component({
    selector: 'app',
    templateUrl: 'app/partials/agGrid/agGrid.html',
    directives: [AgGridNg2]
})
export class AgGridComponent {

    columnDefs = [
        { headerName: "Contact Name", field: "make" },
        { headerName: "Company Name", field: "model" },
        {
            headerName: "Last Event",
            field: "price",
            cellClass: 'rightJustify',
            cellRenderer: function (params: any) {
                return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
            }
        },
        { headerName: "Contact Email", field: "model" },
        { headerName: "Work Phone", field: "model" }
    ];
    // put data directly onto the controller
    rowData = [
        { make: "Toyota", model: "Celica", price: 35000 },
        { make: "Ford", model: "Mondeo", price: 32000 },
        { make: "Porsche", model: "Boxter", price: 72000 }
    ];

    gridOptions = {
        columnDefs: this.columnDefs,
        rowData: null,
        enableFilter: true
    };



    values='';
    onKey(event:any) {
        this.values = event.target.value;
        this.gridOptions.columnDefs.setQuickFilter(this.values);
    }

    searchValue='';

}

网格.html

<input placeholder="Filter..." type="text"
	   [value]="searchValue"
	   (input)="searchValue = $event.target.value"
/>

<ag-grid-ng2
		class="ag-fresh"
		enable-sorting
		enable-filter
		style="height: 300px" 
		[gridOptions]="gridOptions" 
		(cellClicked)="onCellClicked()" 
		[columnDefs]="columnDefs"
		[rowData] = "rowData"> 
</ag-grid-ng2>

4

2 回答 2

6

将 ngModel 变量绑定到“quickFilterText”属性。“quickFilterText”应该在未来更改为“quickFilter”。

<input type="text" 
    [(ngModel)]="searchValue" placeholder="quick filter..." /> 
<ag-grid-angular  
    class="data-grid ag-theme-balham"
    [rowData]="rowData | async" 
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [quickFilterText]="searchValue"
    >
</ag-grid-angular>
于 2019-03-26T15:09:53.340 回答
3

我猜你已经解决了这个问题,但对于那些目前正在寻找的人,你可以将输入框双向绑定到代码中的变量。

您的 HTML 将如下所示:

<input type="text" (keyup)="onQuickFilterChanged()" 
    [(ngModel)]="quickSearchValue" placeholder="quick filter..." /> 

和你的 TS 代码是这样的:

quickSearchValue: string = '';

private onQuickFilterChanged() {
    this.gridOptions.api.setQuickFilter(this.quickSearchValue);
}

希望这可以帮助。

于 2017-03-13T16:43:19.860 回答