admin管理员组文章数量:1399269
I have a UIComponent with this view:
<mvc:View controllerName="myponents.webmap.controller.Map"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:html="">
<html:div id='myDiv' style='height:inherit'></html:div>
<Button id="helloWorld" text="Say Hello" press=".onShowHello" />
</mvc:View>
In the View controller, I can get the Button and its id from
this.getView().byId("helloWorld").sId
but I can't get the ID of the div using the byId()
method.
this.getView().byId("myDiv"); // returns undefined
How can I get the ID of the div in order to access the element? I need the ID of the div so I can append some additional 3rd party controls to it.
I have a UIComponent with this view:
<mvc:View controllerName="my.ponents.webmap.controller.Map"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:html="http://www.w3/1999/xhtml">
<html:div id='myDiv' style='height:inherit'></html:div>
<Button id="helloWorld" text="Say Hello" press=".onShowHello" />
</mvc:View>
In the View controller, I can get the Button and its id from
this.getView().byId("helloWorld").sId
but I can't get the ID of the div using the byId()
method.
this.getView().byId("myDiv"); // returns undefined
How can I get the ID of the div in order to access the element? I need the ID of the div so I can append some additional 3rd party controls to it.
Share Improve this question edited Feb 3, 2020 at 9:53 Boghyon Hoffmann 18.1k14 gold badges93 silver badges205 bronze badges asked May 11, 2016 at 16:40 user1025184user1025184 573 silver badges7 bronze badges 03 Answers
Reset to default 5You can use the createId
to convert your lokal ID to a global one and use that with getElementById
:
// After rendering
var divId = this.createId("myDiv"); // this == controller instance
document.getElementById(divId).whatEver
There are some possible solutions. Let's see the preferred one from UI5 point of view. Your view:
<mvc:View
controllerName="yourcontroller"
xmlns="sap.m"
xmlns:layout="sap.ui.layout"
xmlns:mvc="sap.ui.core.mvc"
xmlns:html="http://www.w3/1999/xhtml">
<Button press="btn" />
<layout:VerticalLayout id="layout">
</layout:VerticalLayout>
</mvc:View>
You can any use any layout, which fits your requirements, and of course you can set layout properties as well. In your controller, the event handler of the button:
btn: function(oEvent) {
var oLayout = this.getView().byId("layout");
oLayout.addContent(new sap.m.Text({text: "test"}));
}
-o-
If your api supports only a div container, you should use some jQuery tricks:
var oDiv = $('[id*="myDiv"]');
However, with this solution you will lose the UI5 specific control handling.
To answer your question how to get the ID of the HTML element (div) - you can also assemble the ID in your controller like this:
var sHTMLElementID = this.getView().sId.concat("--".concat("viewDiv"));
So UI5 still handles the view's id and prefixes.
本文标签: javascriptUI5 How to access html element defined inside viewStack Overflow
版权声明:本文标题:javascript - UI5: How to access html element defined inside view? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744116919a2591557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论