Skip to Content
0

Multi Model support in SAP UI5

Feb 15, 2017 at 08:58 PM

63

avatar image

Hi,

I am new to SAP UI5 and would like to know what mistake I am doing when using multi Models(JSON) in my app.

I have two JSON models : Address and Payments.

The structure of my app where these files are stored is :

/webapp/model/Address.json

/webapp/model/Payments.json

Inside the Contoller I have onInit function which has following Code:

var oModel1 = new JSONModel(jQuery.sap.getModulePath("NSProject_Table.model", "/Address.json"));
this.getView().setModel(oModel1,"oModel1");

var oModel2 = new JSONModel(jQuery.sap.getModulePath("NSProject_Table.model", "/Payments.json"));
this.getView().setModel(oModel2,"oModel2");

The XML View has two controls : ComboBox(uses Address) and Table(Payments):

<ComboBox id="Combo1" showSecondaryValues="true" items="{ path: 'oModel1>/Address', sorter: { path: 'Name' }}">
<core:ListItem key="{/Name}" text="{/street}" additionalText="{/City}"/>
</ComboBox>

<Table id="idProductsTable" inset="false" items="{ path: 'oModel2>/Items', sorter: { path: 'DocNo' } }">
<headerToolbar>
<Toolbar>
<Title text="Products" level="H2"/>
</Toolbar>
</headerToolbar>
<columns>
<Column width="12em"><Text text="Doc No"/></Column>
<Column minScreenWidth="Tablet" demandPopin="true"><Text text="Main"/></Column>
<Column minScreenWidth="Tablet" demandPopin="true"><Text text="Sub"/></Column>
<Column minScreenWidth="Tablet" demandPopin="true"><Text text="Description"/></Column>
<Column hAlign="Right"><Text text="Amount"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>

<Text text="{DocNo}"/>
<Text text="{Main}" width="20%"/>
<Text text="{Sub}"/>
<Text text="{Desc}"/>
<Text text="{Amount}"/>
</cells>
</ColumnListItem>
</items>
</Table>

The comboBox and the table show blank rows but the number of blank rows is the same as the number of address and the payments items. Do you know why I am seeing blank rows?

Also If I try to access using absolute Path like {oModel1>/Address/0/Name} for a text field it works, but when using in Combox and table using oModel1>/ and oModel2>/ it does not work.

Any pointers will be really appreciated.

Thanks

Govind Parmar

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Veeraraghavan Narasimhan
Feb 16, 2017 at 01:50 AM
0

Hello,

I am going with a few assumptions (which i will state) because your json structure is not known. Your Address.json has the following structure

{Address : [
    {
     Name : "Name1",
     Street : "Street1"
     City : "City1"
    },          {
          Name : "Name1",
          Street : "Street1"
          City : "City1"
       }]
}

Then your binding in combobox needs to

<ComboBox id="Combo1" showSecondaryValues="true" items="{ path: 'oModel1>/Address', sorter: { path: 'Name' }}"><core:ListItem key="{oModel1>Name}" text="{oModel1>street}" additionalText="{oModel1>City}"/></ComboBox>

Observe that Slash(/) has been removed from binding and modelname has been prefixed. aggregation binding puts the Listitem at oModel1>/Address/0/ and /Address/1/ and so on. You need to mention only the name of the attribute but prefix with model identifier.


Similarly your table looks fine from the binding perspective just prefix the named model in the attributes as well. This should fix it.

Thanks and Regards,

Veera

Share
10 |10000 characters needed characters left characters exceeded