cancel
Showing results for 
Search instead for 
Did you mean: 

How to apply a filter to the oDataModel read function?

Former Member

Hi UI5 gurus,

Been trying to follow 's advice here: https://scn.sap.com/thread/3735171

But I'm not having much luck...

The Problem

I can't seem to get sap.ui.model.Filter working with ODataModel read function!


var la_filters = new Array(); // Don't normally do this but just for the example.

var ld_date = new Date("2015-08-20"); // Javascript Date object

var lo_pickedDateFilter = new sap.ui.model.Filter({

                     path: "FilterEndDate",

                     operator: sap.ui.model.FilterOperator.EQ,

                     value1: ld_date

              });

la_filters.push(lv_pickedDateFilter);

model.read("/TimeAccountCollection", null, la_filters);

// also tried the following but no luck...

model.read("/TimeAccountCollection", null, null, true, la_filters);

So what happens is that its seems like the read function accepts the array of Filter(s) as a urlParamter which is what I suspect I'm doing wrong.

Google Chrome Dev Tools shows that the request looks like this:

http://<host>:<port>/sap/opu/odata/sap/MY_CUSTOM_SERVICE/TimeAccountCollection?[object%20Object]

Which is not giving me my desired results...

A Solution - urlParameters


var valid_date = "2015-08-20T00:00:00" // hardcoded for example

var arrParams = ["$filter=FilterEndDate eq datetime'" + valid_date + "'"];

// Fire off a new GET request to update the model based on the date.

model.read("/TimeAccountCollection", null, arrParams);

Google Chrome Dev Tools shows that the request looks like this:

http://<host>:<port>/sap/opu/odata/sap/MY_CUSTOM_SERVICE/TimeAccountCollection?$filter=FilterEndDate...

This solution works for now...


If it helps I'm on UI5 version 1.26.1 but this shouldn't be an issue.

What am I doing wrong in my sap.ui.model.Filter example for the ODataModel read function?

If anyone has any ideas I'd love to hear from you.  I'm sure I'm missing something small.

Thanks.

Regards,

Ben

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi.


model.read("/TimeAccountCollection", {

  filters: la_filters

  }

});

Regards,

Christopher

Former Member
0 Kudos

Thanks Christopher.  I knew I was doing something wrong!

Answers (2)

Answers (2)

former_member182372
Active Contributor
0 Kudos

use paramtrized second parameter

ODataModel.prototype.read = function(sPath, mParameters) {

...

aFilters   = mParameters.filters;

...

Former Member
0 Kudos

Thank you Maksim!  I had some difficulties understanding how the mParameters worked in the documentation and you've just made it click.

santhu_gowdaz
Active Contributor
0 Kudos

filter is a array,

declare your array like this,

var la_filters = [];

else add filter var like below,

  1. model.read("/TimeAccountCollection", null, [lo_pickedDateFilter ]); 

Refer this links,

http://stackoverflow.com/questions/23128948/sapui5-how-to-filter-data-with-2-or-more-values

Former Member
0 Kudos

Hi Santhosh,

Thanks for your input and contribution.  Your example is the same as mine where I use an array and I'm afraid it does not work.   The issue here is that the model.read is not following the oModel prototype for the read function.

All the best.

Regards,

Ben