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()),我注意到更新下拉菜单时过滤器确实存在。但是,网格没有根据外部过滤器进行更新。
我很确定&#34; isExternalFilterPresent()&#34;和&#34; doExternalFilterPass(节点)&#34;贯穿并提供正确的返回值。我的理解是,ag-grid将负责其余部分,但它并没有这样做。有什么想法吗?
答案 0 :(得分:6)
这个问题有一个解决方案。
声明两个函数:isExternalFilterPresent,doExternalFilterPass 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
答案 1 :(得分:2)
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;
}
答案 2 :(得分:0)
此问题的更新:
对我来说问题是角度2变量的范围。 &#34; this.portType&#34;未定义在&#34; isExternalFilterPresent()&#34;和&#34; doExternalFilterPass(节点)&#34;甚至我在构造函数中正确初始化。我的修复方法是每次调用这两个函数时从html中检索portType。
这不是一个好的解决方案,希望有人能想出更好的东西。如果有人能解释为什么portType变量未定义?