angular 2 中的 Ag-grid 外部过滤器,过滤器存在但网格未更新
Ag-grid external filter in angular 2, filter presents but grid not updating
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)" 运行 通过并提供了正确的 return 值。我的理解是 ag-grid 会处理剩下的事情,但它并没有这样做。有什么想法吗?
关于此问题的更新:
我的问题是 angular 2 个变量的范围。 this.portType
在 isExternalFilterPresent()
和 doesExternalFilterPass(node)
中未定义,即使我在构造函数中正确初始化。我的解决方法是每次调用这两个函数时从 HTML 检索 portType。
这不是一个很好的修复,希望有人能想出更好的办法。如果有人能解释为什么 portType
变量未定义?
这个问题有解决方案。
在类型脚本中声明两个函数:isExternalFilterPresent、doesExternalFilterPass,
获取 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
doesExternalFilterPass
和 isExternalFilterPresent
是箭头函数,因此 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;
}
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)" 运行 通过并提供了正确的 return 值。我的理解是 ag-grid 会处理剩下的事情,但它并没有这样做。有什么想法吗?
关于此问题的更新:
我的问题是 angular 2 个变量的范围。 this.portType
在 isExternalFilterPresent()
和 doesExternalFilterPass(node)
中未定义,即使我在构造函数中正确初始化。我的解决方法是每次调用这两个函数时从 HTML 检索 portType。
这不是一个很好的修复,希望有人能想出更好的办法。如果有人能解释为什么 portType
变量未定义?
这个问题有解决方案。
在类型脚本中声明两个函数:isExternalFilterPresent、doesExternalFilterPass,
获取 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
doesExternalFilterPass
和 isExternalFilterPresent
是箭头函数,因此 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;
}