我正在尝试的 jsbin 上有以下代码。我需要获取模型“答案”中提到的“Id”属性的值。直到现在,在尝试时我才明白我需要使用 select 事件来提醒选定的值。我也尝试使用event.getSource().getSelectedItem().getBindingContext().getObject().Id
但我无法获得“Id”属性值。您可以在 jsbin 下方找到我正在尝试的代码:http ://jsbin.com/peciqaq/1/edit?html,output
您必须Id
从绑定模型中获取。
将您的onSelectRadio
事件处理程序更改为以下内容:
if(!event.getParameter("selected")) return;
var oModel = event.getSource().getModel();
var sPath = event.getSource().getBindingContext().getPath()
alert(oModel.getProperty(sPath+"/Id"));
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="UTF-8">
<title>List example</title>
<script id="sap-ui-bootstrap"
type="text/javascript"
src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-theme="sap_bluecrystal"
data-sap-ui-libs="sap.ui.commons, sap.m"
data-sap-ui-xx-bindingSyntax="complex">
</script>
<!-- XML-based view definition mode="SingleSelectMaster" -->
<script id="view1" type="sapui5/xmlview">
<mvc:View
controllerName="local.controller"
xmlns:core="sap.ui.core"
xmlns:f="sap.ui.layout.form"
xmlns:l="sap.ui.layout"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m">
<List
headerText="Awesome List"
items="{/answers}"
id="singleChoice"
selectionChange="onSelectionChange">
<items>
<InputListItem label="{answerText}">
<RadioButton select="onSelectRadio"/>
</InputListItem>
</items>
</List>
</mvc:View>
</script>
<script>
// Controller definition
sap.ui.controller("local.controller", {
onInit: function() {
var oData = {
"answers": [{
"Id":1,
"answerText": "Cats"
}, {
"Id":2,
"answerText": "Rabbits"
}, {
"Id":3,
"answerText": "Dogs"
}, {
"Id":4,
"answerText": "Hamsters"
}]
};
sap.ui.getCore().setModel(new sap.ui.model.json.JSONModel(oData));
},
/*onSelectionChange: function(event) {
alert(event.getSource().getSelectedItem().getBindingContext().getObject().Id);
console.log(JSON.stringify(event.getSource().getSelectedItem().getBindingContext().getObject()));
},*/
onSelectRadio: function(event){
if(!event.getParameter("selected")) return;
var oModel = event.getSource().getModel();
var sPath = event.getSource().getBindingContext().getPath()
alert(oModel.getProperty(sPath+"/Id"));
//alert(this.byId("singleChoice").getSelectedItem());
//alert(event.getSource().getSelectedItem().getBindingContext().getObject().Id);
}
});
// Instantiate the View, assign a model
// and display
var oView = sap.ui.xmlview({
viewContent: jQuery('#view1').html()
});
oView.placeAt('content');
</script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句