如何在OpenUI5中使用聚合绑定时获取对象密钥

问题描述 投票:0回答:1

例如,我有一个用以下数据初始化的sap.ui.model.json.JSONModel

{
    "data": {
        "something": {"a": "blah", "b": "blah", "c": "blah"},
        "another": {"a": "blah", "b": "blah", "c": "blah"}
    }
}

我在XMLView中有一个sap.m.List,如下所示:

<List items="{/data}">
    <items>
        <ObjectListItem title="{a}" intro="">
            <firstStatus>
                <ObjectStatus title="{b}" text="{c}"/>
            </firstStatus>
        </ObjectListItem>
    </items>
</List>

现在我希望这两个introObjectListItem属性是“某事”和“另一个”,但我找不到办法去做,除了将数据更改为

{
    "data": {
        "something": {"key": "something", "a": "blah", "b": "blah", "c": "blah"},
        "another": {"key": "another", "a": "blah", "b": "blah", "c": "blah"}
    }
}

然后绑定"{key}",这看起来很愚蠢(它引入了冗余并浪费了服务器带宽)。

那么,任何想法?

sapui5
1个回答
1
投票

使用自定义工厂函数,从上下文路径获取所需的键:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv='X-UA-Compatible' content='IE=edge'>
		<meta charset="utf-8">

		<title>MVC with XmlView</title>

		<!-- Load UI5, select "blue crystal" theme and the "sap.m" control library -->
		<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-libs='sap.m'
			data-sap-ui-xx-bindingSyntax='complex'></script>

		<script id="view1" type="sapui5/xmlview">
		    <mvc:View
				controllerName="my.own.controller"
				xmlns:l="sap.ui.layout"
				xmlns:core="sap.ui.core"
				xmlns:mvc="sap.ui.core.mvc"
				xmlns:f="sap.ui.layout.form"
				xmlns="sap.m">
			    <List items="{path:'/data', factory: '.myFactory'}">
          </List>
			</mvc:View>
	</script>


		<script>
			// define a new (simple) Controller type
			sap.ui.controller("my.own.controller", {
			  myFactory: function(sId, oContext){
			    var keyPath = oContext.getPath();
			    console.log(oContext);
			    
			    var oObjectListItem = new sap.m.ObjectListItem({
			      title: {path: keyPath + '/a'},
			      intro: keyPath.split('/')[keyPath.split('/').length - 1],
			      firstStatus: [
			        new sap.m.ObjectStatus({
			          title: {path: keyPath + '/b'},
			          text: {path: keyPath + '/c'}
			        })
			      ]
			    });
			    
			    return oObjectListItem;
			  }
			});

			// instantiate the View
			var myView = sap.ui.xmlview({viewContent:jQuery('#view1').html()}); // accessing the HTML inside the script tag above

			// create some dummy JSON data
			var data = {
          "data": {
              "something": {"a": "blah", "b": "blah", "c": "blah"},
              "another": {"a": "blah", "b": "blah", "c": "blah"}
          }
      }
			
			// create a Model and assign it to the View
			var oModel = new sap.ui.model.json.JSONModel();
			oModel.setData(data);
			myView.setModel(oModel);
			
			
			// put the View onto the screen
			myView.placeAt('content');

		</script>
	
	</head>
	<body id='content' class='sapUiBody'>
	</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.