cancel
Showing results for 
Search instead for 
Did you mean: 

Help with accessing View Content from Controller

Former Member
0 Kudos

Hi,

I have been playing around a bit with the MVC structure for SAPUI5 projects and have come a bit stuck! I am hoping someone can help me out. It seems like a really stupid question so my apologies upfront but I can't work out what's going on so am hoping someone else can see what I am doing wrong.

Here is the scenario:

  • I created a new SAPUI5 Application Project using the Eclipse Wizard. I added one View using the wizard of type JS.

  • Then in that View I added a very simple Text View and set the text to "Set in View"

sap.ui.jsview("mvc101.view1", {

      getControllerName : function() {

         return "mvc101.view1";

      },

      createContent : function(oController) {

                var oTextView1 = new sap.ui.commons.TextView("tView1");

                oTextView1.setText("Set in View");

                return oTextView1;

      }

});

  • Then in the controller I try to access the Text View as follows and set the text to be "Set in Controller"

onInit: function() {

             this.byId("tView1").setText("Set in Controller");

   },

But when I run and debug this I get an error in the console saying that this.byId("tView1") is undefined:

However when I use firebug I can see tView1 in the content aggregation of the View:

Help... What am I doing wrong here? Can anyone see my mistake?

Thanks,

Simon

Update: When I use a JSON view type as follows it works fine (no undefined errors) to access the TextView using the this.byId() method:

View2:

{

    "Type":"sap.ui.core.mvc.JSONView",

    "controllerName":"mvc101.view2",

    "content": [{

              "Type" : "sap.ui.commons.TextView",

              "id" : "tView2",

              "text" : "Set in View2"

   }]

}

And the onInit method of the Controller:

onInit: function() {

             this.byId("tView2").setText("Set in Controller2");

   }

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello Simon,

please set the 'id' property by using < this.createId("..." ) >. in my case this helped.

Greetings,

chris

Former Member
0 Kudos

Thanks for your reply Chris... can you tell me where to use "this.createId("")... do you mean in the View or the Controller... ?

Former Member
0 Kudos

hy simon,

try this in 'createContent' of the View  :

var oTextView1 = new sap.ui.commons.TextView(this.createId("tView1")); 

Former Member
0 Kudos

Thanks Chris,

That worked! I thought I would just be able to set the id to "tView1" by passing that string to the constructor... I would never have thought to use the this.createId("") method. In this case what is the "this" referring to, is it the JS View object?

Thanks so much for your help.

Simon

Answers (1)

Answers (1)

Former Member
0 Kudos

Good Morning Simon,

in this case 'this' is referring to the view object really, but also the controller has in his standard methods where refer to itself ( 'onInit', 'onAfterRendering' etc) the createId() method which "Converts a view local id to a globally unique one by prepending the view id".

http://www.spyvee.com/SAPHTML5_DemoKit/#docs/api/symbols/sap.ui.core.mvc.Controller.html#createId

If you need this method in another context then you can get it from the generated view.

var oView = sap.ui.getCore().getElementById("<viewId>");

var oController = null;

if(oView != null)

       oController = oView.getController();

next you can use creatId by calling

oController.createId("");

have a nice day

chris

Former Member
0 Kudos
Christoph Lehmann and Simon Kemp

Hey friends , i have created a table by Consuming JASON data from my local host webservice, now i want to know,,,,,, When i click my Row of the table i want to Display the Row Text To another view < like a detailed list>

Do you have any idea friends ........I have created a

table( oTable )   Adding columns ...

oTable.addColumn(new sap.ui.table.Column({

            label: new sap.ui.commons.Label({text: "NscItem"}),

            template: new sap.ui.commons.Link().bindProperty("text", "NscItem").bindProperty("href", "href"),

            sortProperty: "NscItem"}));

and did ( var oModel = new sap.ui.model.json.JSONModel(); )

Then .... 

var aData = 

        jQuery.ajax({

           url: "http://localhost/client.json",  // for different servers cross-domain restrictions need to be handled

           dataType: "json",

           success: function(data, textStatus, jqXHR) { // callback called when data is received

            oModel.setData(data);             // fill the received data into the JSONModel

           },

           error: function(jqXHR, textStatus, errorThrown) {

           }

        });

        oTable.setModel(oModel);

        oTable.bindRows({ / });  

I get he data in my Table friends ,,,,, but guide me how to make a detaile view in another view When i click the Row... ?

And i totaly dnt have idea to create another view......i know how to create a new view but .......Getting the data and the Codes in the ONit Part Guide me friends