如何绑定实例的属性?

时间:2018-01-17 09:34:25

标签: sapui5

我创建了一个对象数组:

buildPayerModel: function() {
    return [
        new SalesPayer("000", "2333", "033.433", "CHF"),
        new SalesPayer("000", "2333", "033.433", "CHF"),
        new SalesPayer("000", "2333", "033.433", "CHF")
    ];
}

并作为模特:

this.getView().setModel(this.buildPayerModel(), "Sales")

现在我想在表中显示如下数据:

<Table id="SalesView" inset="false" items="{ path: '/Sales' }">
    <headerToolbar>
        <Toolbar>
            <Title text="Statistics" level="H2"/>
        </Toolbar>
    </headerToolbar>
    <columns>
        <Column width="12em">
            <Text text="Payer"/>
        </Column>
        <Column minScreenWidth="Tablet" demandPopin="true">
            <Text text="Name"/>
        </Column>
        <Column minScreenWidth="Tablet" demandPopin="true" hAlign="End">
            <Text text="Net Value"/>
        </Column>
        <Column hAlign="End">
            <Text text="Currency"/>
        </Column>
    </columns>
    <items>
        <ColumnListItem>
            <cells>
                <ObjectIdentifier title="{Sales.id}"/>
                <Text text="{Sales.name}"/>
                <Text text="{Sales.name}"/>
                <Text text="{Sales.name}"/>
            </cells>
        </ColumnListItem>
    </items>
</Table>

如何访问实例的属性?

2 个答案:

答案 0 :(得分:1)

setModel()获取模型作为第一个参数,而不是普通的JSON对象或数组。此外,第二个参数是型号名称,如果您将其命名为&#34;销售&#34;,则必须使用&#34;销售&gt;&#34;在你的绑定中。然后,记住: 1.使用&#34; /&#34;在从模型的根构建路径时开始。 2.避免使用&#34; /&#34;在开头,如果你绑定一个相对于另一个上下文的路径(让我们说这个表的每一行的上下文)

尝试这样的事情:

&#13;
&#13;
<!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_belize_plus'
			data-sap-ui-libs='sap.m'
			data-sap-ui-xx-bindingSyntax='complex'></script>


		<!-- DEFINE RE-USE COMPONENTS - NORMALLY DONE IN SEPARATE FILES -->

		<!-- define a new (simple) View type as an XmlView
		 - using data binding for the Button text
		 - binding a controller method to the Button's "press" event
		 - also mixing in some plain HTML
		 note: typically this would be a standalone file -->

		<script id="view1" type="sapui5/xmlview">
  		<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" controllerName="my.own.controller">
  			<Table id="SalesView" inset="false" items="{ path: 'Sales>/players' }">
          <headerToolbar>
              <Toolbar>
                  <Title text="Statistics" level="H2"/>
              </Toolbar>
          </headerToolbar>
          <columns>
              <Column width="12em">
                  <Text text="Payer"/>
              </Column>
              <Column minScreenWidth="Tablet" demandPopin="true">
                  <Text text="Name"/>
              </Column>
              <Column minScreenWidth="Tablet" demandPopin="true" hAlign="End">
                  <Text text="Net Value"/>
              </Column>
              <Column hAlign="End">
                  <Text text="Currency"/>
              </Column>
          </columns>
          <items>
              <ColumnListItem>
                  <cells>
                      <ObjectIdentifier title="{Sales>id}"/>
                      <Text text="{Sales>name}"/>
                      <Text text="{Sales>name}"/>
                      <Text text="{Sales>name}"/>
                  </cells>
              </ColumnListItem>
          </items>
      </Table>
  		</mvc:View> 
    </script>


		<script>
			// define a new (simple) Controller type
			sap.ui.controller("my.own.controller", {
				onInit: function(){
				  
					this.getView().setModel(this.buildPayerModel(), "Sales")
				},
				
				buildPayerModel: function() {
          var oModelData = {
            "players": [
              {"id": 001, "name": "Michael Jordan"},
              {"id": 002, "name": "Kobe Bryant"},
              {"id": 003, "name": "LeBron James"}
            ]
          };
          var oModel = new sap.ui.model.json.JSONModel(oModelData)
          return oModel;
        }
			});
	
			// instantiate the View
			var myView = sap.ui.xmlview({viewContent:jQuery('#view1').html()}); // accessing the HTML inside the script tag above
			
			// put the View onto the screen
			myView.placeAt('content');
		</script>
	
	</head>
	<body id='content' class='sapUiBody'>
	</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

  1. 您的buildPayerModel()未构建模型。
  2. API setModel等待&#34;子类&#34; sap.ui.model.Model(不是数组),以及相应的模型名称(在您的案例中为"Sales")。
  3. 绑定语法如下所示:"{modelName>/propertyName}"
    • 绑定在大括号内定义:{ ... }
    • 如果有型号名称,则需要>
    • 开头的/(可能在>之后)表示绝对绑定语法
    • 开头没有 / - &gt; "{modelName>childPropertyName}"表示相对绑定语法。它是相对的,因为如果没有给定的上下文,就无法解决这种绑定。
  4. 为&#34;销售&#34;提供JSONModel SalesPayer 实例包含nameid等直接属性,为你解决的问题:

    buildPayerModel: function() {
        return new /*sap.ui.model.json.*/JSONModel([
            new SalesPayer("000", "2333", "033.433", "CHF"),
            new SalesPayer("000", "2333", "033.433", "CHF"),
            new SalesPayer("000", "2333", "033.433", "CHF")
        ]);
    },
    

    在同一个控制器的某个地方:

    this.getView().setModel(this.buildPayerModel(), "Sales")
    

    xmlView中:

    <Table id="SalesView" inset="false" items="{Sales>/}">
        <headerToolbar>
            <Toolbar>
                <Title text="Statistics" level="H2"/>
            </Toolbar>
        </headerToolbar>
        <columns>
            <Column width="12em">
                <Text text="Payer"/>
            </Column>
            <Column minScreenWidth="Tablet" demandPopin="true">
                <Text text="Name"/>
            </Column>
            <Column minScreenWidth="Tablet" demandPopin="true" hAlign="End">
                <Text text="Net Value"/>
            </Column>
            <Column hAlign="End">
                <Text text="Currency"/>
            </Column>
        </columns>
        <items>
            <ColumnListItem>
                <cells>
                    <ObjectIdentifier title="{Sales>id}"/>
                    <Text text="{Sales>name}"/>
                    <Text text="{Sales>name}"/>
                    <Text text="{Sales>name}"/>
                </cells>
            </ColumnListItem>
        </items>
    </Table>