Search code examples
asp.net-web-apikendo-gridodatakendo-asp.net-mvc

Odata v4 working with Kendoui grid contains filter


What is easiest way to get the contains filter working with a asp.net web api web service that uses odata v4?

It seems that web services using odata v4 no longer acknowledge the 'substringof' function and want the ' contains' function.

Example: GET using the Contains filter on the WorkUnitCode column in the grid and entering 'xYz'. substringof(fails)

http://localhost:1486/odata/BillOfMaterials(2)/BillOfMaterialsItems?$format=json&$top=10&$filter=substringof('xYz',WorkUnitCode)&$count=true

What the GET needs to be for the contains function to work:

http://localhost:1486/odata/BillOfMaterials(2)/BillOfMaterialsItems?$format=json&$top=10&$filter=contains(WorkUnitCode,'xYz')&$count=true

I believe there are two ways to approach this problem and not sure which is better or how given either solution is reusable.

Approach 1: Intercept the request and change it to use the contains function with reversed parameters. Approach 2: Add the substringof functionality to the web api.


Solution

  • Went with the simpler JS solution where the parameterMap is intercepted and changed to accommodate the new ODATA v4 function.

    var ds = new kendo.data.DataSource({
            type: 'odata',
            serverFiltering: true,
            serverPaging: true,
            pageSize: 10,
            transport: {
                read: {
                    url: function () { return '{0}{1}'.format(_appRoot,_serviceUrl); },
                    dataType: "json"
                }
                , parameterMap: function (data) {
                    var d = kendo.data.transports.odata.parameterMap(data);
                    delete d.$inlinecount;
                    d.$count = true;
    
                    if (d.$filter) {
                        // substringof('xYz',WorkUnitCode)  needs to 
                        // change to contains(WorkUnitCode,'06')
                        if (d.$filter.substring(0, 12) == 'substringof(') {
                            var parms = d.$filter.substring(12, d.$filter.length - 1).split(',');
                            d.$filter = 'contains({0},{1})'.format(parms[1],parms[0]);
                        }
                    }
    
                    return d;
                }
            },
            schema: {
                data: function (data) { return data.value; },
                total: function (data) { return data['@odata.count']; },
                model: _schemaModel
            }
            });