Skip to Content
avatar image
-1
Former Member

Problem with working with different data model in a sapui5 project

Hello everyone, I hope you can help me.

I have problems with the data model and the data binding,

I assign the models to the view from the onInit function of each view, but when navigating between views, created averse is no longer executed onInit, and the data binding is done with the last model created, as you could get a separate model for each model? I need to display a table with data from the moment it is entered into the view.

Attached images with controllers code and views.

Thanks in advance.

BookingHistory.controller

BookingHistory.View

Binding data in BookingHistory Table

ClientTariff.controller

ClienteTariff.view

ClienteTariff Table

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Mar 11, 2017 at 07:30 AM

    Hello Julio,

    You are accessing the model at component and using it in both the views but with different alias. That would be the reason, the model at component is updated with the new data in second view and showing the same in first view(incorrect though).

    Usually if we want to access model globally we use the model at component. However, in your case both views should have different data. So declare the model in the view controller itself and assign it to the view for both the views.

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

    model.setData(jsonBookingHistory);

    this.getView().setModel(model,"modelBookingHistory");

    You need to remove below code from your controller.

    Similarly do it in other view.

    This should work.

    Regards,

    Gowrinath

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 10, 2017 at 08:43 PM

    not able to follow u

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      I hope to explain better, already with the code that I have put.

      I'm working with the manifest.json for routing.
      My goal is that when browsing to a view in this case BookingHistory and ClientTariff,
      Show the json model data in their respective tables.

      When I run for the first time, I navigate to the ClientTariff view, it runs onInit and the table is populated with the model.
      Then navigate to the BookingHistory view, run onInit and the table is populated with the model.

      Here comes the problem, then I navigate again to ClientTariff and the onInit is no longer executed and the table is filled with the BookingHistory model as it was the last one that was filled in the model, some properties of json match that is why some are shown Data, but still are not correct and the fields are not filled in the table.

      I hope you have let me understand, please tell me what else you need to know about this context so you can help me please.
      Thanks in advance

  • Mar 11, 2017 at 06:18 PM

    Hi,

    I think you can move code from onInit to RoutePatternMatched event of router, that code will execute every time whenever that particular view will be navigated.

    Hope this help you.

    -Akhilesh

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 13, 2017 at 09:49 PM

    Many thanks to Gowrinath Gundu and Akhilesh Upadhyay, the two suggestions satisfy my need, and I have given two alternatives that I can apply in my project according to the ideal. Thank you very much again.

    Add comment
    10|10000 characters needed characters exceeded