我有这个简单的 XML 视图:
<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"
controllerName="listicons.list" xmlns:html="http://www.w3.org/1999/xhtml">
<Page title="Title">
<content>
<List id="test-list"></List>
</content>
</Page>
</core:View>
在我的控制器中,我调用一个方法来构建 onInit 列表项。首先设置一些数据:
var data = {
"products": [
{
"prodName": "Apple",
"prodCountry": "Netherlands",
"price": "normal"
},
{
"prodName": "Orange",
"prodCountry": "Spain",
"price": "extra"
},
{
"prodName": "Strawberry",
"prodCountry": "Poland",
"price": "normal"
}
]
};
// create a Model with this data and attach it to the view
var model = new sap.ui.model.json.JSONModel();
model.setData(data);
this.getView().setModel(model);
var list = this.getView().byId("test-list");
然后我构建列表并将项目绑定到它:
// bind the List items to the data collection
list.bindItems({
path : "/products",
sorter : new sap.ui.model.Sorter("prodName"),
//template : listTmpl
template : new sap.m.StandardListItem({
title: "{prodName}",
description: "{prodCountry}"
})
});
在构建列表并已呈现后,我会查看哪些项目有额外价格并为它们设置一个图标:
jQuery.each(list.getItems(), function(i, obj) {
if(obj.mProperties.price == "extra") {
obj.setIcon("sap-icon://flag");
}
});
所以。一切正常。但我对我的解决方案并不满意,因为我宁愿在渲染列表之前操作数据。我尝试在将项目绑定到列表之前直接构建一个列表模板,然后使用此模板,如下所示:
var listTmpl = jQuery.each(data.products, function(i, a) {
var lI = new sap.m.StandardListItem({
title: "{prodName}",
description: "{prodCountry}"
});
if(a.price == "extra") {
lI.setIcon("sap-icon://flag");
}
return lI;
});
但后来我的列表没有显示,我在控制台中收到一个错误,说
元素 sap.m.List 聚合项缺少模板或工厂函数 ...
有谁知道如何改进我的 sol.?
多谢..