Search code examples
javascriptkendo-uitelerikdatasource

How to apply complex odata filters programatically with kendo data sources?


I have a kendo data source (attached to a grid in case it matters) that's an OData v4 endpoint source. I need to apply something like this filter to it ...

LINQ query:

ds.data().Where(i => i.References.Any(r => r.OfferLines.Any(l => l.OfferId == "myOfferId"))

OData query:

?$filter=References/any(r:r/OfferLines/any(l:l/OfferId eq 'myOfferId'))

how can i do this using teleriks documented function dataSource.filter "after my grid has been initialised" programatically?


Solution

  • You can use parameterMap event to inject custom filter. Here is an example of my code using to filter articles by several or conditions.

    dataSource: {
        type: "odata-v4",
        serverFiltering: true,
        transport: {
            read: "/oData/Article",
            parameterMap: function (data) {
                //Get value used to filter
                var value = data.filter.filters[0].value;
                //Generate parameters in odata v4 format
                var d = kendo.data.transports['odata-v4'].parameterMap(data);
                //Add filter by matching any reference containing value
                d.$filter = "References/any(ref: contains(tolower(ref/Reference),'" + value + "')))";
                return d;
            }
        },
    }