Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind OData $count from expanded collection in an XML view

Tags:

sapui5

May be this is a basic question, but I have trouble binding the OData count in XML view.

In the following example, I want to bind the count of products from the OData model.

<List items="{/Categories}"} >  
  <ObjectListItem
    title="{CategoryName}"
    number="{path : 'Products/$count'}"
    numberUnit="Products"/>
</List>

Each category needs to display count of products in the respective category as in

/Categories(1)/Products/$count
/Categories(2)/Products/$count
like image 422
c_y Avatar asked May 22 '14 20:05

c_y


2 Answers

I had a similar issue. Although I am not thrilled with my solution, it uses expression binding and works without the need for a separate formatter:

<List items="{/Categories}"} >  
  <ObjectListItem 
    title="{CategoryName}"
    number="{= ${Products}.length }"
    numberUnit="Products" />
</List>

Like @Jasper_07, you still need to include Products in the expand, but you are ignoring most of the data coming back.

like image 115
Erik Allen Avatar answered Oct 04 '22 23:10

Erik Allen


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

you can achieve the count in a couple of ways using a formatter

option 1 - the simplest, create a list binding which reads the total number of products, it does a synchronous call and returns only the $count

function productCount(oValue) {
    //return the number of products linked to Category // sync call only to get $count
    if (oValue) {
        var sPath = this.getBindingContext().getPath() + '/Products';
        var oBindings = this.getModel().bindList(sPath);
        return oBindings.getLength();
    }
};

<List items="{/Categories}"} >  
 <ObjectListItem 
    title="{CategoryName}"
    number="{path : 'CategoryName',formatter:'productCount'}"
    numberUnit="Products" 
 </ObjectListItem>
</List>

option 2 - use an expand and return a very small set of data, in this case only CategoryName and ProductID, the caveat here is whether you have to by pass table paging to get full list

function productCount(oValue) {
    //read the number of products returned
    if (oValue) {
        return oValue.length;
    }
};

<List items="{/Categories,parameters:{expand:'Products', select:'CategoryName,Products/ProductID'}}">  
 <ObjectListItem 
    title="{CategoryName}"
    number="{path : 'Products',formatter:'productCount'}"
    numberUnit="Products" 
 </ObjectListItem>
</List>
like image 26
Jasper_07 Avatar answered Oct 05 '22 00:10

Jasper_07