0

I am very new to SAPUI5. I have a scenario thus:

A master-detail page (from the master-detail template available via SAP Web IDE, views are XML) displays items from my model(mockdata).

The master section is bound to entity Interests. The detail section gets its information from entity Messages. I have referential constraints between them and it works perfectly alright. Tapping on items in my master section displays the corresponding items in the table in the details section.

The master section list:

<List
  id="list"
  items="{
    path: '/InterestsSet',
    sorter: {
      path: 'InterestShortDescription',
      descending: false
    },
    groupHeaderFactory: '.createGroupHeader'
  }"
  busyIndicatorDelay="{masterView>/delay}"
  noDataText="{masterView>/noDataText}"
  mode="{= ${device>/system/phone} ? 'None' : 'SingleSelectMaster'}"
  growing="true"
  growingScrollToLoad="true"
  updateFinished="onUpdateFinished"
  selectionChange="onSelectionChange">
  <items>
    <ObjectListItem 
      type="{= ${device>/system/phone} ? 'Active' : 'Inactive'}"
      press="onSelectionChange"
      title="{InterestShortDescription}"
      number="{path :'MessagesSet', formatter:'.productCount'}">
    </ObjectListItem>
  </items>
</List>

The Master section needs to display a number value that equals the total number of items that is displayed in Entity Messages(i.e. equal to the number of rows displayed in the details section). After some research (ultimately from SAPUI5 - How to Bind Odata $count to a XML view)i came to know about the formatter property and added it.

My formatter reads thus:

productCount: function(oValue){
    if (oValue) {
      var sPath = this.getBindingContext().getPath() + '/MessagesID';
      var oBindings = this.getModel().bindList(sPath);
      return oBindings.getLength();
    }
}

I tried logging oValue, but it seems to be returning as undefined.

Is there anyway to get the count for each item in the master list.

Community
  • 1
  • 1
arunmenon
  • 489
  • 3
  • 9
  • 18

1 Answers1

1

For the entity of the Master, InterestsSet, suppose that the Key property is InterestID.

So in your code instead you have this:

number="{path :'MessagesSet', formatter:'.productCount'}"

use this:

number="{path :'InterestID', formatter:'.productCount'}"

Now, in the controller the function .productCount should be like this:

productCount: function(InterestID){
    return this.getView().byId("idListInMessagesView").getItems().length;
}

Or this:

productCount: function(InterestID){
    uri = "/InterestSet(InterestID=' + InterestID + ')/YourNameNavigationsToMessages/$count";
    return this.getView().getModel().read(uri);
}
solrac
  • 629
  • 3
  • 16
  • Thanks for the pointer. I corrected the reference to 'InterestsID'. Using the first mentioned function: `productCount: function(InterestID){ return this.getView().byId("lineItemsList").getItems().length;}` i get an error "Fake XHR onreadystatechange handler threw exception: Cannot read property 'getItems' of undefined". I tried logging 'this' which returns as 'EventProvider com.otmm.controller.Master'. – arunmenon Apr 28 '16 at 07:07
  • Using the second function `productCount: function(InterestID){var uri = "/InterestsSet(InterestsID='"+InterestID +"')/Messages/$count"; return this.getView().getModel().read(uri);}` makes calls for each item but each returns a 404. Example URI that is called: http://localhost:50481/here/goes/your/serviceurl/InterestsSet(InterestsID='1')/Messages/$count. I think the '/Messages/'(i.e.YourNameNavigationsToMessages) is possibly the problem. – arunmenon Apr 28 '16 at 07:11
  • I also tried `productCount: function(InterestID){ return sap.ui.getCore().byId("lineItemsList").getItems().length;}` which returns the same "Fake XHR onreadystatechange handler threw exception: Cannot read property 'getItems' of undefined" error. 'lineItemsList' is the id of the List in the detail section. – arunmenon Apr 28 '16 at 07:37
  • Do you have a navigation/association between Interests entity and Messages Entity? What is the name? You should use that name in "YourNameNavigationsToMessages". And if you call, for example (...)/InterestSet(InterestID='123') where (..) is your URI for the oData service, and "123" is a random ID (but you should use a valid ID), do you get data for that? If yes, I guess that "/InterestSet(InterestID=' + InterestID + ')/YourNameNavigationsToMessages/$count" should work... – solrac Apr 28 '16 at 08:21
  • I tried, no data is being returned. I am using "InterestsMessages" as the name. I guess this may not work since i am using mockdata (also as per the answer at http://stackoverflow.com/questions/23816274/sapui5-how-to-bind-odata-count-to-a-xml-view : *"...I dont think its currently possible - $count is an OData query option, the equivalent in ODataListBinding is length, eg Products.length I cant think of a way to bind to it..."*). No answer to a similar query here as well: https://scn.sap.com/thread/3876965. – arunmenon Apr 28 '16 at 10:42