【问题标题】:Ag-grid external filter in angular 2, filter presents but grid not updating角度 2 中的 Ag-grid 外部过滤器,过滤器存在但网格未更新
【发布时间】:2016-10-23 18:18:09
【问题描述】:

app.component.html

<div class="inlineBlock">
    <select [(ngModel)]="portId" id="portDropdownMenu" (change)="externalFilterChanged()">
        <option *ngFor="#portId of portIds">{{portId}}</option>
    </select>
</div>

<div class="container">
    <ag-grid-ng2 #agGrid
                 [gridOptions]="gridOptions"
                 [columnDefs]="myColumnDefs"
                 [rowData]="myRowData"
                 enableColResize
                 rowSelection="multiple"
                 enableSorting
                 enableFilter
                 [isExternalFilterPresent]="isExternalFilterPresent"
                 [doesExternalFilterPass]="doesExternalFilterPass"
                 rowHeight="30"
                 headerHeight="40"
                 enableRangeSelection
                 suppressContextMenu
                 suppressMenuColumnPanel
                 rowGroupPanelShow="always"
                 rememberGroupStateWhenNextData
                 groupDefaultExpanded="-1"
                 groupHideGroupColumns
                 groupUseEntireRow
                 (modelUpdated)="onModelUpdated()"
                 (filterChanged)="onFilterChanged()">
    </ag-grid-ng2>
</div>

app.component.ts

public isExternalFilterPresent() {
        return this.portType != "All Ports";
}

public doesExternalFilterPass(node) {
    switch (this.portType) {
        case "1": return node.data.Port === "1";
        case "2": return node.data.Port === "2";
        case "3": return node.data.Port === "3";
        default: return true;
    }

}

public externalFilterChanged() {
    var newValue = (<HTMLInputElement>document.getElementById("portDropdownMenu")).value
    this.portType = newValue;
    this.gridOptions.api.onFilterChanged();
}

public onFilterChanged() {
    if (this.gridOptions.api.isAnyFilterPresent()) {
        this.gridOptions.api.setRowData(this.gridOptions.rowData);
        this.gridOptions.api.refreshView();
    }
    console.log("filter changed ...");
}

通过 console.log(this.gridOption.isAnyFilterPresented()),我注意到更新下拉菜单时过滤器确实存在。但是,网格不会根据外部过滤器进行更新。

我很确定“isExternalFilterPresent()”和“doesExternalFilterPass(node)”会运行并提供正确的返回值。我的理解是 ag-grid 会处理剩下的事情,但它没有这样做。有什么想法吗?

【问题讨论】:

    标签: ag-grid


    【解决方案1】:

    这个问题有解决办法。

    声明两个函数:isExternalFilterPresent,doesExternalFilterPass in type script,

    获取gridOptions的一个实例,

      private gridOptions:GridOptions;
    

    在构造函数中:

      this.gridOptions = <GridOptions>{};
    

    然后

      this.gridOptions.isExternalFilterPresent  =  this.isExternalFilterPresent.bind(this);
      this.gridOptions.doesExternalFilterPass = this.doesExternalFilterPass.bind(this);
    

    现在,您将能够在这些函数中访问组件的变量:

       this.myVariable
    

    完整描述问题+解决方案: https://github.com/ceolter/ag-grid-ng2/issues/121

    【讨论】:

    • 这应该是公认的答案;重新连接上下文是比在 DOM 中搜索数据更好的解决方案
    【解决方案2】:

    doesExternalFilterPassisExternalFilterPresent 是箭头函数,所以 this 在这些函数中没有任何意义。以下是它们的使用方法-

    /**
       * This property is an arrow function, which binds `this` to the Angular Component.
       * It's necessary otherwise `this` is undefined inside the function because
       * it's not called as a method of the class by the Datagrid.
       * It's called as `doesExternalFilterPass(node)` and not as `component.doesExternalFilterPass(node)`
       */
      doesExternalFilterPass = (node: RowNode): boolean => {
        return node.data.currency >= this.currencyFilter;
      }
    

    来源 - https://github.com/ceolter/ag-grid-angular/issues/121

    【讨论】:

    • 这太棒了!良好的修复和基本原理的良好解释
    【解决方案3】:

    关于这个问题的更新:

    对我来说问题是角度 2 变量的范围。 this.portTypeisExternalFilterPresent()doesExternalFilterPass(node) 中未定义,即使我在构造函数中正确初始化。我的解决方法是每次调用这两个函数时从 HTML 中检索 portType。

    这不是一个很好的解决方法,希望有人能想出更好的方法。如果有人能解释为什么 portType 变量未定义?

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2018-11-05
      • 2018-09-14
      • 2019-04-20
      • 2021-12-06
      • 2018-10-04
      • 1970-01-01
      • 2020-02-11
      • 2022-10-17
      相关资源
      最近更新 更多