【问题标题】:How to create a custom (reusable) feature for filter menus如何为过滤器菜单创建自定义(可重用)功能
【发布时间】:2018-05-15 18:25:05
【问题描述】:

我有一个与 Kendo 文档中的过滤器匹配的自定义过滤器:

https://www.telerik.com/kendo-angular-ui/components/grid/data-operations/filtering/reusable-filter/

@Component({
    selector: 'my-dropdown-filter',
    template: `
    <kendo-dropdownlist 
      [data]="data"
      (valueChange)="onChange($event)" 
      [defaultItem]="defaultItem"
      [value]="selectedValue" 
      [valuePrimitive]="true"
      [textField]="textField"
      [valueField]="valueField">
    </kendo-dropdownlist>
  `
})
export class DropDownListFilterComponent extends BaseFilterCellComponent {

    public get selectedValue(): any {
        const filter = this.filterByField(this.valueField);
        return filter ? filter.value : null;
    }

    @Input() public filter: CompositeFilterDescriptor;
    @Input() public data: any[];
    @Input() public textField: string;
    @Input() public valueField: string;

    public get defaultItem(): any {
        return {
            [this.textField]: "Select item...",
            [this.valueField]: null
        };
    }

    constructor(filterService: FilterService) {
        super(filterService);
    }

    public onChange(value: any): void {
        this.applyFilter(
            value === null ? // value of the default item
                this.removeFilter(this.valueField) : // remove the filter 
                this.updateFilter({ // add a filter for the field with the value
                    field: this.valueField,
                    operator: "eq",
                    value: value
                })
        ); // update the root filter
    }
}

它适用于“行”样式的网格过滤器。但是,当我尝试将其更改为“菜单”样式时,只要更改下拉菜单,就会应用过滤器。该样式的所有其他过滤器都需要您单击“过滤器”按钮。如果我将更改处理程序中的代码更改为:

public onChange(value: any): void {
    if (value) {
        this.updateFilter({ // add a filter for the field with the value
            field: this.valueField,
            operator: 'eq',
            value: value
        })
    }
    else {
        this.removeFilter(this.valueField);
    }
}

然后过滤器按钮功能按预期工作;但它实际上并没有过滤网格。我期望有一些方法可以在单击“过滤器”时调用,但没有找到它。我也尝试过设置过滤器属性(相同的效果)或使用applyFilter,它会立即再次过滤。

如何在 Kendo Angular Grids 中使用带有“菜单”样式过滤器的自定义过滤器组件?

【问题讨论】:

    标签: angular filter kendo-ui-angular2


    【解决方案1】:

    自定义过滤器菜单时,您需要使用 kendoGridFilterMenuTemplate 指令(而不是 kendoGridFilterCellTemplate 指令)。

    Step-by-step explanation of the process and a runnable example are available in the docs.

    【讨论】:

    • 我明白;但是我仍然遇到上面列出的问题(除非我按照您提到的步骤操作,否则它根本不起作用)。然而,我没有找到那个样本。我会将它与我的代码进行比较,看看有什么不同。谢谢!
    • 看起来您必须手动处理过滤器更改...必须有比这更好的选择。
    猜你喜欢
    • 2021-10-25
    • 2019-10-15
    • 2022-08-14
    • 2013-09-18
    • 2019-04-04
    • 2018-08-04
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多