Dee*_*pak 7 parameter-passing sapui5
我无法将数据从XML视图发送到控制器.在JS视图中很容易实现.
例如:-
在JS视图中: -
var btn = new sap.m.Button({
text:"click",
tap:function(){
callFunction(oEvent, "mycustomString");
}
});
Run Code Online (Sandbox Code Playgroud)
如何使用XML视图实现相同的功能.
<Button text="click" tap="callFunction"/>
Run Code Online (Sandbox Code Playgroud)
以上只会传递事件而不是"mycustomString".我该怎么做?
Ben*_*rth 17
您可以通过app:myData在视图中添加自定义数据参数来执行此操作:
视图
<mvc:View
...
xmlns:app="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1"
...
>
<Button text="click" tap="callFunction" app:mydata="mycustomString"/>
...
Run Code Online (Sandbox Code Playgroud)
调节器
callFunction: function(oControlEvent) {
console.log("data: " + oControlEvent.getSource().data("mydata"));
}
Run Code Online (Sandbox Code Playgroud)
日志 data: mycustomString
请参阅https://scn.sap.com/thread/3538029
Tii*_*iJ7 14
这是一个老问题,但从1.56 版开始,现在原生支持直接在 XML 视图中将参数传递给处理函数。
新的事件处理程序参数语法
当事件处理程序被分配到 XML 视图中的控制事件时,您现在还可以指定可以传递给事件处理程序的参数。参数可以是静态值,也可以是绑定甚至表达式。此功能有助于减少控制器代码并避免不必要的控制器方法,并将控制器逻辑与所需输入值的检索分开。
所以你现在可以简单地这样做:
<Button text="click" tap=".callFunction($event, 'mycustomString')" />
Run Code Online (Sandbox Code Playgroud)
请注意,只要您传递至少一个参数,事件本身将不再自动传递,因此您需要手动传递$event,就像我上面所做的那样。但是,还有其他语法可以直接传递事件参数、源代码控制和数据绑定,因此您可能根本不需要事件。
此功能的文档可在演示工具包中在线获取。
简短演示:
<Button text="click" tap=".callFunction($event, 'mycustomString')" />
Run Code Online (Sandbox Code Playgroud)
sap.ui.define([
"sap/ui/core/mvc/Controller"
], function(Controller) {
"use strict";
return Controller.extend("myController", {
callFunction: function(sButtonText, sVal) {
alert("Clicked " + sButtonText + " with value " + sVal);
}
});
});
sap.ui.xmlview({
viewContent: $('#myView').html()
}).placeAt('content');Run Code Online (Sandbox Code Playgroud)