OData Model Not Working

后端 未结 1 716
生来不讨喜
生来不讨喜 2021-01-14 06:29

I am trying to use expand option in my XML view but it\'s resulting no data.

Data are coming from backend as I can see in debugging under \'Ne

相关标签:
1条回答
  • 2021-01-14 07:01

    TL;DR

    Here I created a running example from your code: https://plnkr.co/edit/a2pcO6.

    In the production code, remove all mock server references.


    Issues to fix

    Backend

    According to your service metadata, the foreign key inside matdetails is also the only primary key at the same time. I.e. the Matids in matdetails cannot be unique if one matlist is supposed be associated with multiple matdetails entities.

    Frontend

    Model

    • You missed the model name in setModel.
      • Either pass the model name according to your view: this.setModel(odata, "odata")
      • Or remove the model name from everywhere. E.g.: <Text text="{Matid}"/>
    • According to your screenshot, the response body of your entity set doesn't contain the property "results" which ODataModel cannot deal with. Same as this issue, the "response" property needs to be there in each collection so that UI5 can display the items correctly. Not sure about the cause though as I don't have any expertise in backend. Having all entities uniquely identifiable might resolve this problem.
    • Also worth reading: Stop Using sap.ui.model.odata.ODataModel

    UI

    • You're instantiating your root view (and its controller) twice. Please, avoid that. Read:
      • Why is my Main Controller being called twice?
      • and https://github.com/SAP/openui5/issues/1746
    • Remove the second <App> from your table view. An application should contain only one root element. If not, you might end up having this kind of problems.
    • Since you want to display matdetails entities of selected matlist entity in the table, binding items there with the absolute path /matlistSet doesn't make sense. So, replace ..

      items="{
        path: 'odata>/matlistSet',
        parameters: {
          expand: 'NP_ON_MATID'
        }
      }"
      

      .. with just items="{odata>NP_ON_MATID}". This binding is now relative. The property NP_ON_MATID will be resolved once the selected context is given which is done in the controller..

    Controller

    • On patternMatched, the selected Matid value is given. So far so good but there are two anti-patterns in the following lines:

      var params = "('" + data + "')?$expand=NP_ON_MATID";
      var path = "/matlistSet" + params + "";
      
      1. Avoid creating entity key(s) manually.
      2. Avoid appending ?$expand manually as it won't expand the entity. Pass the navigation property to the parameters/expand of bindElement instead.
    • Off-topic but if you want to navigate back: Remove window parameter from onNavBack: function(window) { ... }. Otherwise, the window in window.history.go is not the global window object but the press event from the back button.

    0 讨论(0)
提交回复
热议问题