问题描述
我正在尝试从SAPUI5应用程序中的嵌套JSON文件绑定数据.该视图为XML格式.
I am trying to bind data from a nested JSON file in my SAPUI5 application. The view is in the XML format.
这是我的JSON文件中的代码段:
Here is the snippet from my JSON file:
{
"Departments": [
{
"ID": "1",
"Name": "Транспортный цех 1",
"Count": 35,
"Address": "Корпус 1, Этаж 7",
"Logo": "image/manager1.jpg",
"Employees": [
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
},
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
}
]
},
{
"ID": "2",
"Name": "Транспортный цех 2",
"Count": 35,
"Address": "Корпус 1, Этаж 7",
"Logo": "image/manager1.jpg",
"Employees": [
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
},
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
}
]
}
]
}
我正在将JSON文件加载到控制器中,然后按如下所示在XML视图中绑定数据地址"和名称":
I am loading the JSON file in my controller and then binding the data "Address" and "Name" in my XML view as follows:
<List id="list1" items="{path:'/Departments'}">
<items>
<ObjectListItem icon="{Logo}" type="Active" press="onListItemPress" number="{Count}" title="{Name}">
<attributes>
<ObjectAttribute text="{Address}" />
</attributes>
</ObjectListItem>
</items>
</List>
但是,当我尝试像这样绑定嵌套数据名字"或姓氏"时,我无法对其进行绑定.
However when I tried binding the nested data "FirstName" or "LastName" like this I am not able to bind it.
text="{Employees/LastName}"
推荐答案
Employees是一个数组.
Employees is an array.
-
您可以选择一个条目并根据需要使用
{Employees/0/LastName}
.
您还可以使用格式化程序功能将员工合并到字符串:
You can also use a formatter function to merge the employees to a string:
查看:
<ObjectAttribute text="{path: 'Employees', formatter: '.formatEmployees'}"/>
控制器:
formatEmployees: function(aEmployees){
return aEmployees.map(function(employee){ return employee.LastName + ", " + employee.FirstName; }).join("; ");
}
- 您可以使用诸如sap.m.ListBox或sap.m.Tokenizer的列表控件,并将项目绑定到Employees数组.
这篇关于在SAPUI5 XML视图中绑定嵌套的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!