如何设置ODATA绑定的kendo-grid的列过滤器?

时间:2017-10-27 10:22:33

标签: kendo-grid odata kendo-ui-angular2 inline-editing

我有一个ODATA源,一列是来自SQL的位字段0/1,用作布尔类型。将该列的过滤器类型设置为“boolean”,过滤器查询将不起作用,因为它查找值true / false,而不是0/1。我想替换(覆盖)该过滤器查询。我该怎么做?

如果不可能,我预计网格中存在一个允许转换类型的管道:它在哪里?

但是,如果我使用的是数字滤镜,则用户必须输入0,1,并且他可能会错误地输入任何其他数字。所以,那是不可行的。

插入自定义Checkbox模板无效,因为该复选框上的Click事件不会触发编辑单元格事件。

1 个答案:

答案 0 :(得分:0)

我希望Telerik的回答是徒劳的。感谢Angular 2的良好架构结构,这是一个直截了当的黑客:

我正在使用自定义绑定指令来访问我从Telerik site复制的ODATA服务。

在那里,您将找到过滤器的访问点:

    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元素中设置为boolean

    <kendo-grid-column field="Dropped" title="Dropped" editor="boolean" filter="boolean" (valueChange)="setBoolFilter(filter, $event)" [editable]="false" width="200px" >
    <ng-template kendoGridCellTemplate let-dataItem>
相关问题