如何在SAPUI5中动态设置文本值?

时间:2022-04-03 12:52:39

I want to set a text value by taking a value from another page or another input area. Here is my text field. I tried many combinations for this.byId("thisOne")., but they didn't work.

我想通过从另一个页面或另一个输入区域获取值来设置文本值。这是我的文字字段。我为this.byId(“thisOne”)尝试了很多组合,但它们没有用。

 this.byId("thisOne").setValue("Some thing");

another way:

  sap.ui.getCore().byId("thisOne")....

The text element:

文字元素:

<Text text="" id ="thisOne"/>

My XML file:

我的XML文件:

<mvc:View
    xmlns:mvc="sap.ui.core.mvc" 
    xmlns="sap.m" controllerName="App.view.Take"
    xmlns:l="sap.ui.layout"
    xmlns:f="sap.ui.layout.form">
    <Page showHeader="false">
          <Label text="thisOne" />
          <Input text="" id="thisOne" />
          <Button type="Accept" text="Accept" ></Button>
          <Button type="Reject" text="Reject" ></Button>
   </Page>
</mvc:View>

3 个解决方案

#1


If you specify an ID explicitly for a control, within an XML view, the actual ID will be prepended with the ID of the XML view, for example the Input control might have an actual ID of "__xmlview0--thisOne".

如果为控件明确指定ID,则在XML视图中,实际ID将以XML视图的ID为前缀,例如,Input控件的实际ID可能为“__xmlview0 - thisOne”。

Best practice is to use the XML View's byId function to get the ID of a control contained within it. So for example in your controller attached to the view (App.view.Take.controller.js in your case) you could do this:

最佳实践是使用XML View的byId函数来获取其中包含的控件的ID。因此,例如在附加到视图的控制器(在您的情况下为App.view.Take.controller.js)中,您可以这样做:

this.getView().byId("thisOne").setValue("Some thing");

#2


Note that setValue will NOT work. setValue is not the supported method for sap.m.Text Control. setValue is supported for sap.m.Input (or any other control which inherit properties from sap.m.Inputbase )

请注意,setValue不起作用。 setValue不是sap.m.Text Control支持的方法。 sap.m.Input(或从sap.m.Inputbase继承属性的任何其他控件)支持setValue

var oValue = sap.ui.getCore().byId("inputId").getValue();

Then

sap.ui.getCore().byId("thisOne").setText(oValue);

Refer to sap.m.Text and sap.m.Input for more details.

有关更多详细信息,请参阅sap.m.Text和sap.m.Input。

#3


If you want to get your textfield and then add a value to it. You have to do this:

如果要获取文本字段,然后为其添加值。你必须这样做:

sap.ui.getCore().byId("thisOne").setValue("Some thing");

#1


If you specify an ID explicitly for a control, within an XML view, the actual ID will be prepended with the ID of the XML view, for example the Input control might have an actual ID of "__xmlview0--thisOne".

如果为控件明确指定ID,则在XML视图中,实际ID将以XML视图的ID为前缀,例如,Input控件的实际ID可能为“__xmlview0 - thisOne”。

Best practice is to use the XML View's byId function to get the ID of a control contained within it. So for example in your controller attached to the view (App.view.Take.controller.js in your case) you could do this:

最佳实践是使用XML View的byId函数来获取其中包含的控件的ID。因此,例如在附加到视图的控制器(在您的情况下为App.view.Take.controller.js)中,您可以这样做:

this.getView().byId("thisOne").setValue("Some thing");

#2


Note that setValue will NOT work. setValue is not the supported method for sap.m.Text Control. setValue is supported for sap.m.Input (or any other control which inherit properties from sap.m.Inputbase )

请注意,setValue不起作用。 setValue不是sap.m.Text Control支持的方法。 sap.m.Input(或从sap.m.Inputbase继承属性的任何其他控件)支持setValue

var oValue = sap.ui.getCore().byId("inputId").getValue();

Then

sap.ui.getCore().byId("thisOne").setText(oValue);

Refer to sap.m.Text and sap.m.Input for more details.

有关更多详细信息,请参阅sap.m.Text和sap.m.Input。

#3


If you want to get your textfield and then add a value to it. You have to do this:

如果要获取文本字段,然后为其添加值。你必须这样做:

sap.ui.getCore().byId("thisOne").setValue("Some thing");