json - UI5 : Dynamically build ListItems from JSON with different Icons

标签 json data-binding listitem sapui5

我有这个简单的 XML View :

<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>

在我的 Controller 中,我调用了一个方法来构建 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)到它:

// 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"); 
                }
            });

所以。一切正常。但我对我的解决方案不满意,因为我宁愿在呈现列表之前操作数据。我尝试在将项目绑定(bind)到列表之前直接构建一个列表模板,然后使用这个模板,例如:

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;
        });

然后我的列表没有显示,我在控制台中收到错误,说

Missing template or factory function for aggregation items of Element sap.m.List ...

有人知道如何改进我的解决方案吗? 非常感谢..

最佳答案

恕我直言,我认为您可以让 Controller 尽可能干净,并在 XMLView 中定义大部分所需的功能(绑定(bind)、模板、排序器和图标):

<List id="test-list" items="{
    path   : '/products', 
    sorter : [{
        path       : 'prodName', 
        descending : true
    }]
}">
    <StandardListItem title="{prodName}" 
                      description="{prodCountry}" 
                      icon="{path:'price', formatter:'.getIconFlag'}" />
</List>

然后您可以摆脱 Controller 中的所有模板绑定(bind)和操作内容,您只需要指定格式化函数getIconFlag:

getIconFlag : function (sPrice) {
    return sPrice === "extra" ? "sap-icon://flag" : null;
}

请参阅以下工作示例:

sap.ui.controller("view1.initial", {
    onInit : function(oEvent) {
        var oModel = new sap.ui.model.json.JSONModel();
        oModel.setData({
            "products": [
                {
                    "prodName": "Apple",
                    "prodCountry": "Netherlands",
                    "price": "normal"
                },
                {
                    "prodName": "Orange",
                    "prodCountry": "Spain",
                    "price": "extra"
                },
                {
                    "prodName": "Strawberry",
                    "prodCountry": "Poland",
                    "price": "normal"
                }
            ]
        });

        this.getView().setModel(oModel);

    },

    getIconFlag : function (sPrice) {
        return sPrice === "extra" ? "sap-icon://flag" : null;
    }

});

sap.ui.xmlview("main", {
    viewContent: jQuery("#view1").html()
})
.placeAt("uiArea");
<script id="sap-ui-bootstrap"
    src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
    data-sap-ui-theme="sap_bluecrystal"
    data-sap-ui-xx-bindingSyntax="complex"
    data-sap-ui-libs="sap.m"></script>

<div id="uiArea"></div>

<script id="view1" type="ui5/xmlview">
    <mvc:View 
      controllerName="view1.initial"
      xmlns="sap.m"
      xmlns:core="sap.ui.core"
      xmlns:mvc="sap.ui.core.mvc" >
        <List id="test-list" items="{
            path: '/products', 
            sorter: [{
                path: 'prodName', 
                descending: true
            }]
        }">
            <StandardListItem title="{prodName}" description="{prodCountry}" icon="{path:'price', formatter:'.getIconFlag'}" />
        </List>
    </mvc:View>
</script>

关于json - UI5 : Dynamically build ListItems from JSON with different Icons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28586513/

相关文章:

javascript - 如何使用 query.find() 从 parse.com 获取 json 响应;在网络应用程序中使用 javascript?

javascript - 处理从 javascript 对象获取的数据

javascript - react Hook 。单击时如何使其他项目(当前项目除外)停用

java - Android:包含标题和副标题的列表项的默认布局

ios - Swift .writeToFile 不会更新 mainBundle 中的 JSON 文件

json - Firefox:在“网络”标签上查看原始JSON

javascript - 是否可以从 javascript 函数调用 telerik radgrid.databind()?

c# - 大型静态对象列表的哪个数据绑定(bind)选项?

java - 使用数据和 JTable?

react-native - 列表项上的背景颜色不起作用?