cancel
Showing results for 
Search instead for 
Did you mean: 

Uncaught TypeError: Cannot read property 'setVisible' of undefined

Former Member
0 Kudos

Im fairly new to SAPUI5 and when I click on button I get the error in the title what I did in Is I used the SAP web IDE to create new MVC project .

in the main view JS I put

createContent : function(oController) {

var btn = new sap.m.Button({ id:"myBtn", text : "Content Button" }); return new sap.m.Page({ title: "TitleT", content: [ btn ] }); 

}

in the Main controller JS I put the following code

onInit: function() { var that = this; window.setTimeout(function() { that.byId("myBtn").setVisible(true); }, Math.random() * 10000); }, onPress: function() { this.byId("pressMeButton").setText("I got pressed");

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

You can manage the control with onInt() and onAfterRendering()  methods.

> Step 1 :  set a property to manager business rules in onInit() method

     onInit: function (oEvent) {

       d.flag = true;  // d define by global var var d = {};

     },

> Step 2  :  get property and  manage  visibility ou destroy properties in onAfterRendering :

     onAfterRendering : function (oEvent) {

          var n = this.getView().getViewName();

          if(n.includes('requirement.edit')){

       

               var s = d.flag;

               this._readOnlyItem("ID_CONTROL", )

          }

     },

     // helper to manager UI Control

     _readOnlyItem: function (k,s){

            try {

                 (s)? this.getView().byId(k).destroy():null;

            }

            catch (e) {

                 console.log("oups...  an error occurred while processing your request : " + e) ;

           }

       },

Former Member
0 Kudos

Hi Mike,

Please read this blog ->

And as for the solutions:

Kimmo's Solution

View code


var btn = new sap.m.Button(this.createId('myBtn'), {

    text: "Content Button"

});

return new sap.m.Page({

    title: "Title",

    content: [btn],

    press:[oController.onPress,oController]

});

Controller code


onInit: function() {

        var that = this;

        window.setTimeout(function() {

            that.byId("myBtn").setVisible(true);

        }, Math.random() * 10000);

    }, onPress: function() {

        this.setText("I got pressed");

        }

Another solution I could think of

View code


var btn = new sap.m.Button({

    id: "myBtn",

    text: "Content Button"

});

return new sap.m.Page({

    title: "Title",

    content: [btn],

    press: [oController.onPress, oController]

});

Controller code


onInit: function() {

        var that = this;

        window.setTimeout(function() {

            sap.ui.getCore().byId("myBtn").setVisible(true);

        }, Math.random() * 10000);

    },

     onPress: function() {

        sap.ui.getCore().byId("myBtn").setText("I got pressed");

}

Both the above solutions could be understood by reading the blog.

Regards,

Naren L Naik

Former Member
0 Kudos

In View you need to add one more tag as "l:VerticalLayout"

<l:Grid defaultSpan="L12 M12 S12"

  width="auto" id="gridId">

  <l:content>

  <l:VerticalLayout id="vlContent">

  <f:SimpleForm id="SimpleFormChange354"

Your form has to be inside this tag. Now in Controller use command like this

var oGrid = this.getView().byId("vlContent");

  oGrid.setVisible(false);

Thus instead of hiding Grid we are hiding VerticalLayout. This will work for sure.

Former Member
0 Kudos

Hello Mike,

You need a reference to your view Object and not your controller to get the Controls on the view.

So your code should look like:


onInit: function() {

     var that = this;

     window.setTimeout(

          function(){

               that.getView().byId("myBtn"),setVisible(true);

          }, Math.random() * 10000

     );

}

with best regards

Florian Hafner

Former Member
0 Kudos

thanks Florian,

I've change it like you suggest but I got the same error,any other idea?

former_member189945
Contributor
0 Kudos

Hi Mike,


When you use getView.byId, view appends it's id into the param id. You can try adding view.createId("myBtn") when constructing the button. That way the button should have the id on context of the view also.


Regards,

Kimmo