首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SAPUI5视图中DropdownBox的复杂数据绑定示例

SAPUI5视图中DropdownBox的复杂数据绑定示例
EN

Stack Overflow用户
提问于 2015-02-11 12:56:36
回答 1查看 24.6K关注 0票数 0

我构建了这个JSON (测试为有效) (.不要介意男人和女人在这里的名字是一样的:-))

代码语言:javascript
复制
{
    "staff": {
        "berlin": [{
            "male": [
                {"firstName": "Lasse", "lastName": "Larsson"},  
                {"firstName": "Gerrit", "lastName": "Gartner"}
            ],
            "female": [
                {"firstName": "Lasse", "lastName": "Larsson"},  
                {"firstName": "Gerrit", "lastName": "Gartner"}
            ]
        }],
        "paris": [{
            "male": [
                {"firstName": "Lasse", "lastName": "Larsson"},  
                {"firstName": "Gerrit", "lastName": "Gartner"}
            ],
            "female": [
                {"firstName": "Lasse", "lastName": "Larsson"},  
                {"firstName": "Gerrit", "lastName": "Gartner"}
            ]
        }],
        "oslo": [{
            "male": [
                {"firstName": "Lasse", "lastName": "Larsson"},  
                {"firstName": "Gerrit", "lastName": "Gartner"}
            ],
            "female": [
                {"firstName": "Lasse", "lastName": "Larsson"},  
                {"firstName": "Gerrit", "lastName": "Gartner"}
            ]
        }]
    }
}

在我的控制器中,我将JSON模型设置为整个视图,如下所示:

代码语言:javascript
复制
// init instance of JSON model
this.staffData = new sap.ui.model.json.JSONModel();
// load JSON file into model
this.staffData.loadData("ajax-dummy/staff.json");
// bind model to whole view 
this.getView().setModel(this.staffData);

在我的XML视图中,我现在想动态地构建一个(嵌套的) DropdownBox,它应该让我选择例如柏林->男性->lastName,所以我需要3个级别的ListItems。

第一个问题是:我可以用JS (为staff对象中的每个键构建一个Listitem,等等)来生成它。但是我不知道如何在XML视图中处理这个问题。现在看起来是这样的:

代码语言:javascript
复制
<content>
<DropdownBox id="dd-locations" editable="true">
<core:ListItem text="{/staff/berlin}"></core:ListItem>
</DropdownBox>
</content> 

当然,它只在den DropdownBox中显示"{ object . .}“,因为它是一个对象。

这甚至可以在XML视图中使用数据绑定构建吗?还是有更好的方法来构造JSON?我知道ListItems需要一个数组。最后:我如何嵌套ListItems?是否有比我应该使用的DropdownBox更好的控制?

编辑:我想要的是像在HTML中那样“只是”嵌套Listitems。我试过:

代码语言:javascript
复制
<ComboBox>
                            <items>
                                <core:ListItem key="key2" text="text2"/>
                                <core:ListItem key="key3" text="text2">
                                    <ComboBox>
                                        <items>
                                            <core:ListItem key="key4" text="text3"/>
                                            <core:ListItem key="key5" text="text3"/>
                                            <core:ListItem key="key6" text="text3"/>
                                         </items>
                                    </ComboBox>
                                </core:ListItem>
                                <core:ListItem key="key4" text="text2"/>
                             </items>
                        </ComboBox>

但一旦出现错误,就会说:

未正确的错误:在没有为控件sap.ui.core.ListItem定义的默认聚合的情况下不能添加直接子级

如何为ListItem定义项聚合?那能行吗?

非常感谢,呵呵

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-11 13:10:21

不确定在您的情况下这是否是一种理想的方法,但是考虑到模型的分层性质,也许下面这个示例"Table - Breadcrumb“就是您要寻找的:https://sapui5.hana.ondemand.com/sdk/explored.html#/sample/sap.m.sample.TableBreadcrumb/preview

它允许您“深入研究”您的模型层次结构,如果需要的话也可以后退一步。

但我不确定它是否能适应“多层下拉框”。

编辑:我更深入地研究了您的JSON,它的结构似乎是不正确的。若要向多个下拉列表提供数据,数据应采用数组格式,而不是对象格式。现在,JSON包含一个属性staff,具有多个属性berlinparis等,而它应该是一个城市数组。我修改了JSON,因此staff属性包含一个对象数组,其中包含一个包含对象数组的gender属性,该属性包含一个包含对象数组的person属性。

此外,为了向“子”下拉列表提供正确的绑定,可以在从下拉列表中选择条目时将绑定设置为正确的路径。

请参阅以下重构模型的工作片段(城市数组、性别数组和人员数组),以及下拉列表的重新绑定:

代码语言:javascript
复制
sap.ui.controller("view1.initial", {
    onInit : function(oEvent) {
        var oModel = new sap.ui.model.json.JSONModel();
        oModel.setData({
            "staff": [
                {
                    "city" : ""
                },
                {
                    "city" : "berlin",
                    "genders" : [
                        { 
                            "gender" : "male", 
                            "people" : [
                                {"firstName": "Lasse", "lastName": "Larsson"},  
                                {"firstName": "Gerrit", "lastName": "Gartner"}
                            ]
                        },
                        { 
                            "gender" : "female", 
                            "people" : [
                                {"firstName": "Paris", "lastName": "Hilton"},  
                                {"firstName": "Kate", "lastName": "Upton"}
                            ]
                        },
                    ]
                },
                {
                    "city" : "paris",
                    "genders" : [
                        { 
                            "gender" : "male", 
                            "people" : [
                                {"firstName": "Lasse", "lastName": "Larsson"},  
                                {"firstName": "Gerrit", "lastName": "Gartner"}
                            ]
                        },
                        { 
                            "gender" : "female", 
                            "people" : [
                                {"firstName": "Lasse", "lastName": "Larsson"},  
                                {"firstName": "Gerrit", "lastName": "Gartner"}
                            ]
                        },
                    ]
                },
                {
                    "city" : "oslo",
                    "genders" : [
                        { 
                            "gender" : "male", 
                            "people" : [
                                {"firstName": "Lasse", "lastName": "Larsson"},  
                                {"firstName": "Gerrit", "lastName": "Gartner"}
                            ]
                        },
                        { 
                            "gender" : "female", 
                            "people" : [
                                {"firstName": "Lasse", "lastName": "Larsson"},  
                                {"firstName": "Gerrit", "lastName": "Gartner"}
                            ]
                        },
                    ]
                },
            ]
        });
        this.getView().setModel(oModel);

    },

    handleStaffSelect : function(oEvent) {
        var oGender = this.byId("selGender");
        var oTemplate = new sap.ui.core.ListItem({ key : "{gender}", text : "{gender}"})
        oGender.bindItems(oEvent.getParameter("selectedItem").getBindingContext().getPath() + "/genders", oTemplate);
    },

    handleGenderSelect : function(oEvent) {
        var oGender = this.byId("selPerson");
        var oTemplate = new sap.ui.core.ListItem({ key : "{lastName}", text : "{lastName}"})
        oGender.bindItems(oEvent.getParameter("selectedItem").getBindingContext().getPath() + "/people", oTemplate);
    }
});

sap.ui.xmlview("main", {
    viewContent: jQuery("#view1").html()
})
.placeAt("uiArea");
代码语言:javascript
复制
<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">
        <Select id="selStaff" items="{/staff}" change="handleStaffSelect">
            <core:ListItem key="{city}" text="{city}" />
        </Select>
        <Select id="selGender" change="handleGenderSelect" />
        <Select id="selPerson" />
    </mvc:View>
</script>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28454944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档