Why my filter is not working in v2.ODataModel “read”?

半世苍凉 提交于 2019-12-29 02:00:10

问题


I am using the OData model to read data. But it doesn't work. Check the code below:

getGuid: function(pernr) {
  var self = this;
  var url = "/PersonalDetailSet?$filter=Pernr eq '00000001'";
  self.setBusy(true);
  this.oModel.read(url, {
    success: function(res) {
      // ...
    },
    error: function() {
      // ...
    }
  });
}

I don't know why the filter in url is not working now?


回答1:


myODataModel.read("/PersonalDetailSet"/*no queries here!*/, {
  filters:[
    new Filter({ // required from "sap/ui/model/Filter"
      path: "myField",
      operator: FilterOperator.EQ, // required from "sap/ui/model/FilterOperator"
      value1: "..."
    })
  ],
  // ...
});
  • API reference: sap.ui.model.odata.v2.ODataModel#read
  • API reference: sap.ui.model.Filter



回答2:


First you check whether you are getting model in the scope or not. As i can see this.oModel which is not proper way of getting model. Better use this.getModel() or this.getView().getModel() and then check the call. Passing filter is not the right way but still it should work.




回答3:


If you want to apply additional URL Parameters in the read function you have to do this via the "urlParameters" parameter:

getGuid: function(pernr){
    var self = this;
    var url = "/PersonalDetailSet";
    self.setBusy(true);
    this.oModel.read(url, {
        urlParameters: {
            "$filter" : "Pernr eq '00000001'"
        },
        success: function(res){
            self.setBusy(false);
            self.guid = res.results[0].Guid;
        },
        error: function() {
            self.setBusy(false);
        }
    });
}


来源:https://stackoverflow.com/questions/45024087/why-my-filter-is-not-working-in-v2-odatamodel-read

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!