Getting an element defined in a core:html by its ID












3















I used this line to define a canvas element in my view.xml:



<core:HTML content="&lt;div class=&quot;wrapper col-6&quot;&gt;&lt;canvas id=&quot;
myChart&quot;width=&quot;800&quot;height=&quot;400&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;">
</core:HTML>


Now I want to get the element in the controller but the typical this.getView().byId("myChart") doesn't seem to work even though the site successfully loads a canvas with the ID.



Is there a way to get those types of elements defined inside a core:HTML tag for the controller?



If not, is there a different way to create a canvas or other HTML elements so that I can refer to them with an ID / use them in the controller?










share|improve this question





























    3















    I used this line to define a canvas element in my view.xml:



    <core:HTML content="&lt;div class=&quot;wrapper col-6&quot;&gt;&lt;canvas id=&quot;
    myChart&quot;width=&quot;800&quot;height=&quot;400&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;">
    </core:HTML>


    Now I want to get the element in the controller but the typical this.getView().byId("myChart") doesn't seem to work even though the site successfully loads a canvas with the ID.



    Is there a way to get those types of elements defined inside a core:HTML tag for the controller?



    If not, is there a different way to create a canvas or other HTML elements so that I can refer to them with an ID / use them in the controller?










    share|improve this question



























      3












      3








      3








      I used this line to define a canvas element in my view.xml:



      <core:HTML content="&lt;div class=&quot;wrapper col-6&quot;&gt;&lt;canvas id=&quot;
      myChart&quot;width=&quot;800&quot;height=&quot;400&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;">
      </core:HTML>


      Now I want to get the element in the controller but the typical this.getView().byId("myChart") doesn't seem to work even though the site successfully loads a canvas with the ID.



      Is there a way to get those types of elements defined inside a core:HTML tag for the controller?



      If not, is there a different way to create a canvas or other HTML elements so that I can refer to them with an ID / use them in the controller?










      share|improve this question
















      I used this line to define a canvas element in my view.xml:



      <core:HTML content="&lt;div class=&quot;wrapper col-6&quot;&gt;&lt;canvas id=&quot;
      myChart&quot;width=&quot;800&quot;height=&quot;400&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;">
      </core:HTML>


      Now I want to get the element in the controller but the typical this.getView().byId("myChart") doesn't seem to work even though the site successfully loads a canvas with the ID.



      Is there a way to get those types of elements defined inside a core:HTML tag for the controller?



      If not, is there a different way to create a canvas or other HTML elements so that I can refer to them with an ID / use them in the controller?







      sapui5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 9:10









      Boghyon Hoffmann

      5,77252455




      5,77252455










      asked Nov 21 '18 at 21:20









      NicoNico

      184




      184
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Like Jorg said, byId is for retrieving controls. So if you were to put an id on the HTML control, you could retrieve that control and then call getDomRef() on it to get the outermost element, which in your example would be the div. If you'd further only put the canvas inside the HTML control, you'd get that.



          If you're accessing the id of the canvas directly, like Jorg suggested, you'll run into trouble if you're going to use the view twice inside a page, because the id of the canvas isn't unique anymore.



          There is a third and IMHO preferable option, that is to use html directly inside the view. First you'll need to declare a namespace for it, like




          xmlns:html="http://www.w3.org/1999/xhtml"




          preferably right on your View element.



          Then you can write html directly in your xml view like this:




          <html:div class="wrapper col-6">
          <html:canvas id="mycanvas" width="800" height="400"></html:canvas>
          </html:div>



          This way you're getting a proper (unique) id for your canvas and can access it as part of the view's dom with this.getView().getDomRef("-mycanvas"). Note the extra leading dash, because of internal id generation inconsistency in UI5. Also note that getDomRef() is considered protected, but I doubt it will change. Finally, remember that you can only get a domref for rendered controls, so you'll probably want to access it from an afterRendering event.






          share|improve this answer
























          • A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

            – Andre F
            Nov 22 '18 at 16:18



















          6














          The element created in this way is not 'registered' in UI5 framework like the other controls. byId() only checks the internal register.



          You can use jQuery or standard JavaScript to fetch the element though, like $('#myChart') or document.querySelector('#myChart').



          You will find the code for this in Core.js or Core-dbg.js.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420623%2fgetting-an-element-defined-in-a-corehtml-by-its-id%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Like Jorg said, byId is for retrieving controls. So if you were to put an id on the HTML control, you could retrieve that control and then call getDomRef() on it to get the outermost element, which in your example would be the div. If you'd further only put the canvas inside the HTML control, you'd get that.



            If you're accessing the id of the canvas directly, like Jorg suggested, you'll run into trouble if you're going to use the view twice inside a page, because the id of the canvas isn't unique anymore.



            There is a third and IMHO preferable option, that is to use html directly inside the view. First you'll need to declare a namespace for it, like




            xmlns:html="http://www.w3.org/1999/xhtml"




            preferably right on your View element.



            Then you can write html directly in your xml view like this:




            <html:div class="wrapper col-6">
            <html:canvas id="mycanvas" width="800" height="400"></html:canvas>
            </html:div>



            This way you're getting a proper (unique) id for your canvas and can access it as part of the view's dom with this.getView().getDomRef("-mycanvas"). Note the extra leading dash, because of internal id generation inconsistency in UI5. Also note that getDomRef() is considered protected, but I doubt it will change. Finally, remember that you can only get a domref for rendered controls, so you'll probably want to access it from an afterRendering event.






            share|improve this answer
























            • A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

              – Andre F
              Nov 22 '18 at 16:18
















            3














            Like Jorg said, byId is for retrieving controls. So if you were to put an id on the HTML control, you could retrieve that control and then call getDomRef() on it to get the outermost element, which in your example would be the div. If you'd further only put the canvas inside the HTML control, you'd get that.



            If you're accessing the id of the canvas directly, like Jorg suggested, you'll run into trouble if you're going to use the view twice inside a page, because the id of the canvas isn't unique anymore.



            There is a third and IMHO preferable option, that is to use html directly inside the view. First you'll need to declare a namespace for it, like




            xmlns:html="http://www.w3.org/1999/xhtml"




            preferably right on your View element.



            Then you can write html directly in your xml view like this:




            <html:div class="wrapper col-6">
            <html:canvas id="mycanvas" width="800" height="400"></html:canvas>
            </html:div>



            This way you're getting a proper (unique) id for your canvas and can access it as part of the view's dom with this.getView().getDomRef("-mycanvas"). Note the extra leading dash, because of internal id generation inconsistency in UI5. Also note that getDomRef() is considered protected, but I doubt it will change. Finally, remember that you can only get a domref for rendered controls, so you'll probably want to access it from an afterRendering event.






            share|improve this answer
























            • A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

              – Andre F
              Nov 22 '18 at 16:18














            3












            3








            3







            Like Jorg said, byId is for retrieving controls. So if you were to put an id on the HTML control, you could retrieve that control and then call getDomRef() on it to get the outermost element, which in your example would be the div. If you'd further only put the canvas inside the HTML control, you'd get that.



            If you're accessing the id of the canvas directly, like Jorg suggested, you'll run into trouble if you're going to use the view twice inside a page, because the id of the canvas isn't unique anymore.



            There is a third and IMHO preferable option, that is to use html directly inside the view. First you'll need to declare a namespace for it, like




            xmlns:html="http://www.w3.org/1999/xhtml"




            preferably right on your View element.



            Then you can write html directly in your xml view like this:




            <html:div class="wrapper col-6">
            <html:canvas id="mycanvas" width="800" height="400"></html:canvas>
            </html:div>



            This way you're getting a proper (unique) id for your canvas and can access it as part of the view's dom with this.getView().getDomRef("-mycanvas"). Note the extra leading dash, because of internal id generation inconsistency in UI5. Also note that getDomRef() is considered protected, but I doubt it will change. Finally, remember that you can only get a domref for rendered controls, so you'll probably want to access it from an afterRendering event.






            share|improve this answer













            Like Jorg said, byId is for retrieving controls. So if you were to put an id on the HTML control, you could retrieve that control and then call getDomRef() on it to get the outermost element, which in your example would be the div. If you'd further only put the canvas inside the HTML control, you'd get that.



            If you're accessing the id of the canvas directly, like Jorg suggested, you'll run into trouble if you're going to use the view twice inside a page, because the id of the canvas isn't unique anymore.



            There is a third and IMHO preferable option, that is to use html directly inside the view. First you'll need to declare a namespace for it, like




            xmlns:html="http://www.w3.org/1999/xhtml"




            preferably right on your View element.



            Then you can write html directly in your xml view like this:




            <html:div class="wrapper col-6">
            <html:canvas id="mycanvas" width="800" height="400"></html:canvas>
            </html:div>



            This way you're getting a proper (unique) id for your canvas and can access it as part of the view's dom with this.getView().getDomRef("-mycanvas"). Note the extra leading dash, because of internal id generation inconsistency in UI5. Also note that getDomRef() is considered protected, but I doubt it will change. Finally, remember that you can only get a domref for rendered controls, so you'll probably want to access it from an afterRendering event.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 16:05









            Rolf GrossmannRolf Grossmann

            463




            463













            • A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

              – Andre F
              Nov 22 '18 at 16:18



















            • A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

              – Andre F
              Nov 22 '18 at 16:18

















            A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

            – Andre F
            Nov 22 '18 at 16:18





            A much better implementation for the html canvas on the XML view. Maybe you can expand on why the extra leading dash is needed in this case? Thanks

            – Andre F
            Nov 22 '18 at 16:18













            6














            The element created in this way is not 'registered' in UI5 framework like the other controls. byId() only checks the internal register.



            You can use jQuery or standard JavaScript to fetch the element though, like $('#myChart') or document.querySelector('#myChart').



            You will find the code for this in Core.js or Core-dbg.js.






            share|improve this answer






























              6














              The element created in this way is not 'registered' in UI5 framework like the other controls. byId() only checks the internal register.



              You can use jQuery or standard JavaScript to fetch the element though, like $('#myChart') or document.querySelector('#myChart').



              You will find the code for this in Core.js or Core-dbg.js.






              share|improve this answer




























                6












                6








                6







                The element created in this way is not 'registered' in UI5 framework like the other controls. byId() only checks the internal register.



                You can use jQuery or standard JavaScript to fetch the element though, like $('#myChart') or document.querySelector('#myChart').



                You will find the code for this in Core.js or Core-dbg.js.






                share|improve this answer















                The element created in this way is not 'registered' in UI5 framework like the other controls. byId() only checks the internal register.



                You can use jQuery or standard JavaScript to fetch the element though, like $('#myChart') or document.querySelector('#myChart').



                You will find the code for this in Core.js or Core-dbg.js.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 23:20

























                answered Nov 22 '18 at 2:00









                JorgJorg

                5,58633150




                5,58633150






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420623%2fgetting-an-element-defined-in-a-corehtml-by-its-id%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen