Converting ODataModel into JSON Model

2019-09-25 09:41发布

问题:

Because of problems with reading all lines of a UI5 common table and the getModel() method from table offers a model.

I thought I could use a JSONModel instead of my ODataModel, the problem now is how to get the ODataModel to the JSONModel.
Because JSON offers some Two Way Binding options which should be helpful.

I tried to read some set and bind it to the JSONModel, the problem is that I couldn't bind the new model two the view because it doesn't offer some set.

Here is a snippet of my code. Hopefully this should be easier too fix as the reading all table rows:

//Erstellung des Modells
var oModel2 = new sap.ui.model.odata.ODataModel(url, true, username, password);
var oModel = new JSONModel(jQuery.sap.getModulePath("QuickStartApplication.model", "/Search.json"));

oModel2.setSizeLimit(8000000);
var pspJSONModel = new sap.ui.model.json.JSONModel();
var oODataJSONModelDLSet = new sap.ui.model.json.JSONModel();
var oODataJSONModelProjectSet = new sap.ui.model.json.JSONModel();
var oODataJSONModelPSPset = new sap.ui.model.json.JSONModel();
var debitorJSONModel = new sap.ui.model.json.JSONModel();
this.getView().setModel(pspJSONModel, "jsonmodel2");
this.getView().setModel(oODataJSONModelDLSet, "jsonmodel");
this.getView().setModel(oODataJSONModelProjectSet, "jsonmodel3");
this.getView().setModel(oODataJSONModelPSPset, "jsonmodel4");
this.getView().setModel(debitorJSONModel, "jsonmodel5");
this.getView().setModel(oModel, "saves");
// etc

oModel2.read("/SEARCH_DLSet", null, null, false, function (oData, oResponse) {
    oODataJSONModelDLSet.setData({ DLSet: oData });
});
oModel2.read("/Search_ProjectsSet", null, null, false, function (oData, oResponse) {
    oODataJSONModelProjectSet.setData({ ProjectSet: oData });
});
oModel2.read("/SEARCH_PSPSet", null, null, false, function (oData, oResponse) {
    oODataJSONModelPSPset.setData({ PSPset: oData });
});
oModel2.read("/DebitorSet", null, null, false, function (oData, oResponse) {
    debitorJSONModel.setData({ DebitorSet: oData });
});
oModel2.read("/PSPSet", null, null, false, function (oData, oResponse) {
    pspJSONModel.setData({ Pspset2: oData });
});
var json1 = pspJSONModel.getJSON();
var json2 = oODataJSONModelDLSet.getJSON();

I saw that the table row binding has found the correct length of the data that it should get but the column binding won't work. Here is a code snippet of row binding and then the column

                            <Text text="PSP(A-Nr.)" class="sapUiResponsiveMargin" ></Text>
                            <ComboBox id="combo1"  items="{path: 'jsonmodel2>/Pspset2/results'}">
                                <core:Item key="{jsonmodel2>Psp}" text="{jsonmodel2>Psp}" />
                            </ComboBox>         

                            <Text text="Abrechnungsstatus" class="sapUiResponsiveMargin" ></Text>
                            <ComboBox id="Abrechnungsstatus"></ComboBox>                    
                        </l:VerticalLayout>
                    </l:HorizontalLayout>
                </l:VerticalLayout> 
             </l:HorizontalLayout> 
             </ScrollContainer> 
         </Panel>   
     <Panel visible="false" height="80%" width="98%" id="__layout15" class="__layout5">
<Button text="Edit" id="edit" type="Transparent" class="buttons" press="onEditRow"> </Button>
        <t:Table class="table0" id="table1" selectionMode="MultiToggle" rows="{jsonmodel>/DLSet/results}">
            <t:columns>
                <t:Column class="columns"  width="105px">
                    <Label text="Kontrakt Nr." id="lKontrakt" >
                    </Label>    
                    <t:template>
                        <commons:TextField id="Kontrakt" editable="false" value="{jsonmodel>KontraktNr}" ></commons:TextField>
                    </t:template>                   
                </t:Column>

Also my two OData Models got on first binding property the set name under sPath but my new one won't get this property.

Here are some Screens from debugging:

OModel 1 with first property of binding

New Model with first property of binding but no right path

Update examples of how the json Result looks like:
PSPSet:

{
  "Pspset2": {
    "results": [
      {
        "__metadata": {
          "id": "http://hvw-sapdev01.oms-sap.omnisys.de:8000/sap/opu/odata/SAP/Z_TBRKNPRO_SEARCH_DL_SRV/PSPSet('00014158001')",
          "uri": "http://hvw-sapdev01.oms-sap.omnisys.de:8000/sap/opu/odata/SAP/Z_TBRKNPRO_SEARCH_DL_SRV/PSPSet('00014158001')",
          "type": "Z_TBRKNPRO_SEARCH_DL_SRV.PSP"
        },
        "Psp": "00014158001"
      },
      {
        "__metadata": {
          "id": "http://hvw-sapdev01.oms-sap.omnisys.de:8000/sap/opu/odata/SAP/Z_TBRKNPRO_SEARCH_DL_SRV/PSPSet('00030173001')",
          "uri": "http://hvw-sapdev01.oms-sap.omnisys.de:8000/sap/opu/odata/SAP/Z_TBRKNPRO_SEARCH_DL_SRV/PSPSet('00030173001')",
          "type": "Z_TBRKNPRO_SEARCH_DL_SRV.PSP"
        },
        "Psp": "00030173001"
      }
    ]
  }
}

And DLSet:

{
  "DLSet": {
    "results": [
      {
        "__metadata": {
          "id": "http://hvw-sapdev01.oms-sap.omnisys.de:8000/sap/opu/odata/SAP/Z_TBRKNPRO_SEARCH_DL_SRV/SEARCH_DLSet(9168)",
          "uri": "http://hvw-sapdev01.oms-sap.omnisys.de:8000/sap/opu/odata/SAP/Z_TBRKNPRO_SEARCH_DL_SRV/SEARCH_DLSet(9168)",
          "type": "Z_TBRKNPRO_SEARCH_DL_SRV.SEARCH_DL"
        },
        "Abrechungsstatus": "",
        "Contractnr": "",
        "DatumHigh": "",
        "DatumLow": "",
        "Login": "",
        "Mandant": "",
        "Mitarbeiter": "",
        "Name": "",
        "Psp": "",
        "Client": "001",
        "Id": 9168,
        "Impdate": "",
        "Ldatum": "20130705",
        "Lperiode": "",
        "PersNr": "49056462",
        "PersName": "J",
        "PersGrade": "",
        "LocalGradeKey": "",
        "LocalGradeDesc": "",
        "PersKstN": "",
        "PersKstB": "",
        "AufKstNr": "",
        "AufKstBe": "",
        "AufNr": "",
        "AufBez": "",
        "Bemerkung": "",
        "DebitorId": "0000037503",
        "DebitorBez": "G",
        "ProdCodeNr": "T130",
        "ProdCodeBez": "Allg. Steuerberatung von Unternehmen",
        "IndustCodeNr": "99999",
        "IndustCodeBez": "H",
        "ArbZ": "5.0000000000000000E-01",
        "ScaleRate": "280.0000",
        "MarketRate": "280.0000",
        "ScaleValue": "0.0000",
        "CostRate": "0.0000",
        "CukyCostRt": "",
        "Fakturiert": "",
        "FakeLdate": "",
        "Confidential": "",
        "KontraktNr": "5000059735",
        "PrctrHead": "",
        "Honorar": "140.0000",
        "Kosten": "140.0000",
        "SatzNeu": "280.0000",
        "HonorarNeu": "140.0000"
      },
      {
        "__metadata": {}
      }
    ]
  }
}

回答1:

As your code seems really unclear, here's a pointer on how you could implement it:

You read the OData response into a JSONModel:

var oModel2 = new sap.ui.odata.ODataModel();
var oODataJSONModelDLSet = new sap.ui.json.JSONModel();

this.getView().setModel(oODataJSONModelDLSet, "jsonmodel");

// etc

oModel2.read("/SEARCH_DLSet" + filterString, null, null, false, function (oData, oResponse) {
    oODataJSONModelDLSet.setData({ DLSet: oData });
});

...you then bind to your view:

<t:Table rows="{jsonmodel>/DLSet/results}">
    <t:columns>
        <t:Column>
            <Label text="Kontrakt Nr." /> 
            <t:template >
                <commons:TextField value="{jsonmodel>KontraktNr}" />
            </t:template>                   
        </t:Column>

EDIT: Updated answer based on updated question



回答2:

First: did you properly attached your JSONModel to your view, using the 'jsonmodel' name (because you refer to it in your table's binding)

Then, have you checked the structure of the oData object ? you are asking something like:

{ oData : { results : [... ] } }

Finally in the text field you do not reference 'jsonmodel' again, you should put {jsonmodel>KontraktNr} instead of just {KontraktNr}