【问题标题】:filter kendo ui grid with filed type object使用字段类型对象过滤剑道 ui 网格
【发布时间】:2013-06-24 13:10:19
【问题描述】:

我有这个网格

$("#address-grid").kendoGrid({
    dataSource: {
        transport: {
            read: {
                url: "operations/get_sales_reps_addresses.php?salesRepsId=" + salesRepsId,
                type: "GET"
            },
            update: {
                url: "operations/edit_address.php?salesRepsId=" + salesRepsId,
                type: "POST",
                complete: function (e) {
                    $("#address-grid").data("kendoGrid").dataSource.read();
                }
            },
            destroy: {
                url: "operations/delete_address.php",
                type: "POST",
                complete: function (e) {
                    $("address-grid").data("kendoGrid").dataSource.read();
                }
            },
            create: {
                url: "operations/add_address.php?salesRepsId=" + salesRepsId,
                type: "POST",
                complete: function (e) {
                    $("#address-grid").data("kendoGrid").dataSource.read();
                }
            },
        },
        schema: {
            data: "data",
            total: "data.length", //total amount of records
            model: {
                id: "SalesRepId",
                fields: {
                    AddressType: {
                        defaultValue: {
                            AddressTypeid: 1,
                            AddressTypeName: "Work"
                        }
                    },
                    Country: {
                        defaultValue: {
                            CountryId: 38,
                            CountryName: "Canada"
                        }
                    },
                    State: {
                        defaultValue: {
                            StateId: 4223,
                            StateName: "British Colombia"
                        }
                    },
                    City: {
                        defaultValue: {
                            CityId: 59450,
                            CityName: "Vancouver"
                        }
                    },
                    PostalCode: {
                        type: "string"
                    },
                    AddressText: {
                        type: "string"
                    },
                    IsMainAddress: {
                        type: "boolean"
                    },
                    AddressId: {
                        type: "integer"
                    }
                }
            }

        },
        pageSize: 3,
    },
    ignoreCase: true,
    height: 250,
    filterable: true,
    sortable: true,
    pageable: true,
    reorderable: false,
    groupable: false,
    batch: true,
    navigatable: true,
    toolbar: ["create", "save", "cancel"],
    editable: true,
    columns: [{
        field: "AddressType",
        title: "Type",
        editor: AddressTypeDropDownEditor,
        template: "#=AddressType.AddressTypeName#",
    }, {
        field: "Country",
        title: "Country",
        editor: CountryDropDownEditor,
        template: "#=Country.CountryName#",
    }, {
        field: "State",
        title: "State",
        editor: StateDropDownEditor,
        template: "#=State.StateName#",
    }, {
        field: "City",
        title: "City",
        editor: CityTypeDropDownEditor,
        template: "#=City.CityName#",
    }, {
        field: "PostalCode",
        title: "Postal Code",
    }, {
        field: "AddressText",
        title: "Address",
    }, {
        field: "IsMainAddress",
        title: "Main?",
        width: 65,
        template: function (e) {
            if (e.IsMainAddress == true) {
                return '<img align="center" src ="images/check-icon.png" />';
            } else {
                return '';
            }
        }
        // hidden: true

    }, {
        command: "destroy",
        title: "&nbsp;",
        width: 90
    },

    ]
});

问题是当我尝试按国家或州或城市过滤时出现错误

TypeError: "".toLowerCase 不是函数

我尝试将 Country 的类型更改为字符串,我使用的是组合框,因此值未定义。我还尝试将类型更改为对象,值显示正确但我无法过滤。我得到了同样的错误(toLowerCase)

我该如何解决这个问题?

我的网格很像this example

这里是 jsFiddle 。我刚刚添加了过滤器。我仍然得到以前的错误

我想过滤类别,有什么帮助吗??

【问题讨论】:

    标签: kendo-ui kendo-grid


    【解决方案1】:

    这是您过滤数据源Kendo dataSource , filter的方式

    所以得到你的网格的数据源,

    var gridDatasource = $("#address-grid").data('kendoGrid').dataSource;
    

    并像这个例子一样过滤它。

    gridDatasource.filter({ ... });
    

    如果你提供一个工作的 jsFiddle,你可能会得到一个更具体的答案。

    具体答案:

    您添加了过滤器,因此对于 Category 它不起作用,因为正如我所说,它是可观察的,而不是可以过滤为字符串的字段。

    因此,您必须为此列指定更好的过滤器,如下所示:

    field: "Category",
    title: "Category",
    width: "160px",
    editor: categoryDropDownEditor,
    template: "#=Category.CategoryName#",
    filterable: {
        extra: false,
        field:"Category.CategoryName",
        operators: {
            string: {
                startswith: "Starts with",
                eq: "Is equal to",
                neq: "Is not equal to"
            }
        }
    }
    

    看到这个 jsFiddle --> http://jsfiddle.net/blackjim/Sbb5Z/463/

    【讨论】:

    • @Kamal 我添加了一个答案。看看吧。
    • 搜索有效,但您破坏了类别的选择。选择一个类别后,将显示类别 ID 而不是类别名称。如果您尝试在此之后进行搜索,它将因为整数值而失败。请检查它的增益!
    • @Kamal 你的过滤器实现是错误的,原因和我之前说的一样。
    • jsFiddle 链接已失效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多