加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

参数传递 – 如何在XML视图SAP UI5中将参数传递给事件处理程序

发布时间:2020-12-16 07:44:25 所属栏目:百科 来源:网络整理
导读:我无法将数据从 XML视图发送到控制器.在JS视图中很容易实现. 例如:- 在JS视图中: – var btn = new sap.m.Button({ text:"click",tap:function(){ callFunction(oEvent,"mycustomString"); }}); 如何使用XML视图实现相同的功能. Button text="click" tap="
我无法将数据从 XML视图发送到控制器.在JS视图中很容易实现.

例如:-

在JS视图中: –

var btn = new sap.m.Button({
    text:"click",tap:function(){
          callFunction(oEvent,"mycustomString");
    }
});

如何使用XML视图实现相同的功能.

<Button text="click" tap="callFunction"/>

以上只会传递事件而不是“mycustomString”.
我该怎么做?

这是一个老问题,但 as of version 1.56现在支持直接在XML视图中将参数传递给处理函数.

New Event Handler Parameter Syntax

When event handlers are assigned to control events in XML views,you can now also specify parameters which can be passed to the event handler. The parameters can be static values as well as bindings and even expressions. This feature helps to reduce controller code and avoid unnecessary controller methods,and separates the controller logic from the retrieval of the required input values.

所以你现在可以这样做:

<Button text="click" tap=".callFunction($event,'mycustomString')" />

请注意,只要您传递至少一个参数,事件本身将不再自动传递,因此您需要通过传递$event手动执行此操作,如上所述.但是,还有其他语法可以直接传递事件参数,源代码控制和数据绑定,因此您可能根本不需要该事件.

该功能的文档可在Demo Kit在线获得.

简短演示:

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');
<html>

  <head>
    <meta charset="utf-8">
    <script id='sap-ui-bootstrap' src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js' data-sap-ui-libs='sap.m'></script>
    <script id="myView" type="sapui5/xmlview">
      <mvc:View controllerName="myController" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m">
        <Button text="Button 1" press=".callFunction(${$source>/text},'ABCDEF')" />
        <Button text="Button 2" press=".callFunction(${$source>/text},'UVWXYZ')" />
      </mvc:View>
    </script>
  </head>

  <body class='sapUiBody'><div id='content'></div></body>

</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读