找不到SelectedItem BindingContext UI5应用程序

时间:2018-08-13 10:27:36

标签: sapui5

我试图找到selectedItem的绑定上下文。即使在将modelName传递给bindingContext之后,我也不确定。当我执行oEvent.getSourcer()并看到oBindingContexts时,它为空白。另外,oBindingInfos具有未定义的ocontext。虽然它具有sPath。正确的sPath。在这种情况下如何获取数组索引?

oNewField = new sap.m.Select({
    enabled: "{order>/" + Type+ "/" + i + "/fieldEnabled}",
    forceSelection: false,
    width: "90%",
    // Add dropdoen Items
    items: [
        new sap.ui.core.ListItem({
            key: " ",
            text: " "
        }),
        new sap.ui.core.ListItem({
            key: "{order>/" + Type+ "/" + i + "/DefaultValue}",
            text: "{order>/" + Type+ "/" + i + "/DefaultValue}"
        })
    ],
    change : function(evt) {
        that.onChange(evt); 
    },
});

var selectedKey = this.getView().getModel('order').getProperty(
  "/" + Type+ "/" + i + "/DefaultValue");
oNewField.setSelectedKey(selectedKey); 

**关于更改功能**

onChange: function(oEvent) {
    debugger;
    var key = oEvent.getSource().getSelectedItem().getKey();
    //need to get BindingContext here.
    var oContext = 
    oEvent.getSource().getSelectedItem().getBindingContext('order')
    //gives undefined
},

2 个答案:

答案 0 :(得分:2)

您根本没有进行任何聚合绑定。因此,没有上下文可检索。您正在对项目汇总中的2个项目进行硬编码。

检查此代码段。它向您展示了您可以做的许多事情。我希望他们中的一个是您正在寻找的东西。

JSBIN:https://jsbin.com/kumudufaje/edit?html,output

<!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">
			<Panel id="myPanel">
			</Panel>
		</mvc:View> 
        </script>


		<script>
			// define a new (simple) Controller type
			sap.ui.controller("my.own.controller", {
                onInit: function(oEvent){
                  
                  //aggregation binding
                  var oSelect = new sap.m.Select({
                    items: {
                      path: 'order>/options',
                      template: new sap.ui.core.Item({
                        key: {
                          path: 'order>key'
                        },
                        text: {
                          path: 'order>value'
                        }
                      })
                    },
                    change: this.onSelection1Change.bind(this)
                  });
                  
                  this.getView().byId("myPanel").addContent(oSelect);
                  
                  
                  for(var i=0; i<3; i++){
                    //hardcoded items
                    var oSelect2 = new sap.m.Select({
                      items: [
                        new sap.ui.core.ListItem({
                          key: '',
                          text: ''
                        }),
                        new sap.ui.core.ListItem({
                          key: {path:'order>/Type/' + i + '/DefaultValue'},
                          text: {path:'order>/Type/' + i + '/DefaultValue'}
                        }),
                      ],
                      change: this.onSelection2Change.bind(this)
                    });
                    
                    this.getView().byId("myPanel").addContent(oSelect2);
                  }
                },
              
                onSelection1Change(oEvent){
                  var oContext = oEvent.getSource().getSelectedItem().getBindingContext('order')
                  console.log(oContext); //This prints the binded context
                  
                  var oModel = oContext.getModel();
                  console.log(oModel); //This prints the whole model
                  
                  var oSelectedEntry = oModel.getProperty(oContext.getPath());
                  console.log(oSelectedEntry); //This prints the data
                },
              
                onSelection2Change(oEvent){
                  var sKey = oEvent.getSource().getSelectedItem().getKey();
                  console.log(sKey); //This prints the selected item key
                  
                  var sValue = oEvent.getSource().getSelectedItem().getKey();
                  console.log(sValue); //This prints the selected item value
                  
                  var oKeyBinding = oEvent.getSource().getSelectedItem().getBinding('key')
                  console.log(oKeyBinding); //This prints the binding of the key property, if any
                  
                  if(oKeyBinding){
                    var oModel = oKeyBinding.getModel();
                    console.log(oModel); // This prints the model binded key property, if any
                  }
                }
			});
	
	
	
			/*** THIS IS THE "APPLICATION" CODE ***/
			// create some dummy JSON data
			var data = {
				options: [{
                  key: '1',
                  value: 'option 1'
                },{
                  key: '2',
                  value: 'option 2'
                },{
                  key: '3',
                  value: 'option 3'
                }],
              
                Type:[
                  {DefaultValue: 'Default Value1'},
                  {DefaultValue: 'Default Value2'},
                  {DefaultValue: 'Default Value3'}
                ]
			};
			var oJSONModel = new sap.ui.model.json.JSONModel();
			oJSONModel.setData(data);
			
			// instantiate the View
			var myView = sap.ui.xmlview({viewContent:jQuery('#view1').html()}); // accessing the HTML inside the script tag above
          
			myView.setModel(oJSONModel, "order");
			// put the View onto the screen
			myView.placeAt('content');
		</script>
	
	</head>
	<body id='content' class='sapUiBody'>
	</body>
</html>

答案 1 :(得分:0)

oEvent.getSource().getSelectedItem().getBindingContext()

您完全正确。请注意,oEvent.getSource()指向sap.m.Select。您需要另一个getSelectedItem()才能进入所选的sap.ui.core.Item