I was working on an application and there was a requirement in which I had to apply filter while fetching data from the server using ODATA. I had two choice to achieve this
- Construct ODATA URL with filter applied
- Apply filter in Kendo UI Data Source
In this post we will discuss how we could apply filter while creating Kendo UI DataSource reading OData feed.
We can create data source reading ODATA feed as following. Below we are creating data source reading ODATA feed of Netflix
var movieTitleData; movieTitleData = new kendo.data.DataSource( { type: "odata", endlessScroll: true, batch: false, transport: { read: { url: "http://odata.netflix.com/Catalog/Titles?$select=Id,ShortName,BoxArt&$top=10", dataType: "jsonp", data: { Accept: "application/json" } } } });
On inspecting request URL you will find that URL to fetch data is getting constructed as following. We are fetching top 10 movies title from Netflix
Now assume there is a requirement to fetch information of a particular movie with given Id. We can do that by applying filter while creating the KendoUI DataSource. We can apply filter to fetch movie of particular id as given below.
While applying filter we need to make sure that serverFiltering is set to true.
In Filter,
- Id is the column on which filter is being applied
- eq is operator to apply filter
- value is filter value
Kendo UI data source with filter applied can be created as following.
var moviedetailData = new kendo.data.DataSource( { type: "odata", endlessScroll: true, serverFiltering: true, transport: { read: { url: "http://odata.netflix.com/Catalog/Titles?$select=Id,ShortName,BoxArt,AverageRating,ReleaseYear,Synopsis", dataType: "jsonp", data: { Accept: "application/json" } } }, filter: { filters: [{ field: "Id", operator: "eq", value: query }] } });
While creating above KendoUI data source
- Data is fetched from Odata feed of Netflix
- Filter is applied on Id column of Netflix data source
- To apply filter at server side serverFiltering attribute is set to true
On inspecting request URL you will find that URL to fetch selected movie is getting constructed as following. We are fetching a particular movie on given id of the movie.
You will find that URL being constructed to fetch Odata feed is containing the filter query. In this way we can apply filter while creating Kendo UI DataSource reading an OData feed. I hope you find this post useful. Thanks for reading.
In your example I believe Id is a string. What would I do in the case of an Id that is an integer or perhaps a guid? In the both cases, using a debugger, I’ve been able to strip the single quotes or cast as a guid, respectively, but I haven’t been able to figure out how to do this in code.