我希望 Telerik 的回答是徒劳的。感谢 Angular 2 良好的架构结构,下面是简单的 hack:
我正在使用自定义绑定指令来访问 ODATA 服务,该服务是从 Telerik site 复制而来的。
在那里,您会找到过滤器的访问点:
public rebind(): void {
this.products.query(this.state);
}
state参数有一个filter属性,它是一个对象,这个是一个过滤树的根。 (在我的表绑定中,没有复杂的过滤器嵌套,过滤器只是一个数组:一个过滤器项对应一个唯一的列。)
直接的策略是通过事件发射器和处理程序将 state.filter 对象发布到组件,然后处理程序将 true/false 设置为 1/0 用于该列 gilter。
所以,这是我的指令:
import { Directive, OnInit, OnDestroy, Output, EventEmitter } from '@angular/core';
import { DataBindingDirective, GridComponent } from '@progress/kendo-angular-grid';
import { ProjectAssignmentService } from './project-assignment-service';
import { Subscription } from 'rxjs/Subscription';
@Directive({
selector: '[projectAssignmentBinding]'
})
export class ProjectAssignmentBindingDirective extends DataBindingDirective implements OnInit, OnDestroy {
private serviceSubscription: Subscription;
constructor(private service: ProjectAssignmentService, grid: GridComponent) {
super(grid);
}
public ngOnInit(): void {
this.serviceSubscription = this.service.subscribe((result) => {
this.grid.data = result;
});
super.ngOnInit();
this.rebind();
}
public ngOnDestroy(): void {
if (this.serviceSubscription)
this.serviceSubscription.unsubscribe();
super.ngOnDestroy();
}
@Output() setFilter: EventEmitter<any> = new EventEmitter();
public rebind(): void {
// this.state.filter is undefined during initialization,
// which I consider to be a bug!
this.setFilter.emit(this.state.filter);
this.service.query(this.state);
}
}
处理程序在组件的 html 模板中声明:
<kendo-grid projectAssignmentBinding ... (setFilter)="onSetFilter($event)" ... >
...
</kendo-grid>
组件中的 onSetFilter 处理程序会这样做:
public onSetFilter(filter: CompositeFilterDescriptor) : void
{
this.setBoolFilter(filter);
}
// Does only consider uncomposed FilterDescritors, which is given here.
private setBoolFilter(filter: CompositeFilterDescriptor): void {
if ( filter == undefined || filter.filters == undefined )
return;
let allFilters = filter.filters as FilterDescriptor[];
let droppedFilter = allFilters.filter((f) => f.field == "Dropped")[0];
if ( droppedFilter == null )
return;
// don't touch others' guts if not inevitably necessary:
if ( droppedFilter.value === 1 || droppedFilter.value === 0 )
return;
droppedFilter.value = droppedFilter.value ? 1 : 0;
}
其中“Dropped”是 0/1 位字段的列名,其相应的列过滤器在 kendo-grid-column 元素中设置为布尔值
<kendo-grid-column field="Dropped" title="Dropped" editor="boolean" filter="boolean" (valueChange)="setBoolFilter(filter, $event)" [editable]="false" width="200px" >
<ng-template kendoGridCellTemplate let-dataItem>