javascriptsapui5jsonmodel

Date/DateTime Filters on client side JSON Model


I have a JSON model that is bound to a sap.m.Table. I am trying to filter data based on a column "Date" (bound to property[CreatedOn] of the model) which the service returns in the JSON Object Format ("/Date(timeStamp)"). The table is as below: enter image description here

sample Date from server:

enter image description here

I am trying to filter the table on the client side but I am not sure on how to implement date filters on the client side. The date displayed are formatted based on

sap.ui.model.type.Date({pattern: 'dd/MM/YYYY'})

The filtering code looks as below:

var fromD = this.getView().byId("idKMFilterPaneDocDateF").getValue() ? new Date(this.getView().byId("idKMFilterPaneDocDateF").getValue()) :
  undefined;

var dtFilter = new sap.ui.model.Filter({
  path: "CreatedOn",
  operator: "EQ",
  value1: "dateTime'" + fromD.toJSON() + "'"
});

var binding = oTable.getBinding("items");
binding.filter([filter], "Application");
binding.refresh();

When I execute the above code, I always get "NO Data". I need to implement the "BT" filters as well based on user selection criteria but can't get it to work with "EQ" itself.


Solution

  • Create Custom Filter

    var fromD = this.getView().byId("idKMFilterPaneDocDateF").getValue() ? new Date(this.getView().byId("idKMFilterPaneDocDateF").getValue()) :undefined;
    var dtFilter = new sap.ui.model.Filter("CreatedOn");
    dtFilter.fnTest = function(value) {
        //check what value are you getting here. If its string, get only time out of it
        //Compare the date values(in milliseconds)
        fromD.setHours(0); //get rid of time and concern about date
        fromD.setMinutes(0);
        fromD.setSeconds(0);
        fromD.setMilliseconds(0);
        //value should be date object, else change accordingly..
       if(fromD.getTime() === value.getTime()){
           return true;
       }
       return false;
    };
    

    PS:If you can get working JSFiddle with your coding context, I could debug.