如何将参数传递给 XML 视图 SAP UI5 中的事件处理程序 [英] How to pass parameters to a event handlerr in XML Views SAP UI5

查看:46
本文介绍了如何将参数传递给 XML 视图 SAP UI5 中的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将数据从 XML 视图发送到控制器.在 JS 视图中很容易实现.

I am having trouble sending data from the XML view to controller. It is easily achievable in JS views.

例如:-

在JS视图中:-

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

如何使用 XML 视图实现相同的效果.

How do I achieve the same using XML Views.

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

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

The above would only pass the event and not the "mycustomString". How to I do this?

推荐答案

这是个老问题,但是 从 1.56 版起 现在原生支持直接在 XML 视图中将参数传递给处理函数.

This is an old question, but as of version 1.56 there is now native support for passing parameters to handler functions directly in XML Views.

新的事件处理程序参数语法

当事件处理程序被分配来控制 XML 视图中的事件时,您现在还可以指定可以传递给事件处理程序的参数.参数可以是静态值,也可以是绑定甚至表达式.此功能有助于减少控制器代码并避免不必要的控制器方法,并将控制器逻辑与所需输入值的检索分开.

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.

所以你现在可以简单地这样做:

So you can now simply do this:

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

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

Note that as soon as you pass at least one parameter, the event itself will no longer be passed automatically, so you need to manually do so by passing $event, as I've done above. However, there is also additional syntax to pass event parameters, the source control, and data bindings directly, so you will likely not need the event at all.

此功能的文档可在 演示套件中在线获取.

The documentation for this feature is available online in the 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>

这篇关于如何将参数传递给 XML 视图 SAP UI5 中的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆