SAPUI5 - 如何将Odata $ count绑定到XML视图

c_y*_*c_y 13 sapui5

可能这是一个基本问题,但我在XML视图中绑定Odata计数时遇到问题.

比方说,在下面的例子中,我想绑定Odata模型中的产品数量.

<List items="{/Categories}"} >  
  <ObjectListItem
    title="{CategoryName}"
    number="{path : 'Products/$count'}"
    numberUnit="Products"/>
</List>
Run Code Online (Sandbox Code Playgroud)

每个类别都需要显示相应类别中的产品数量.....如

/Categories(1)/Products/$count
/Categories(2)/Products/$count
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助.

Eri*_*len 11

我有类似的问题.虽然我对我的解决方案并不感到兴奋,但它使用表达式绑定并且无需单独的格式化程序即可运行:

<List items="{/Categories}"} >  
  <ObjectListItem 
    title="{CategoryName}"
    number="{= ${Products}.length }"
    numberUnit="Products" />
</List>
Run Code Online (Sandbox Code Playgroud)

与@Jasper_07一样,您仍然需要包含Products在扩展中,但是您忽略了大部分数据.


Jas*_*_07 7

我不认为它目前可能 - $ count是一个OData查询选项,ODataListBinding中的等价物是长度,例如Products.length我想不出一种绑定它的方法

您可以使用格式化程序以两种方式实现计数

选项1 - 最简单的,创建一个读取产品总数的列表绑定,它执行同步调用并仅返回$ 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>
Run Code Online (Sandbox Code Playgroud)

选项2 - 使用扩展,并返回一个非常小的数据集,在这种情况下,只有类别名称和产品ID,这里需要说明的是你是否必须由合格表分页获得完整列表

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>
Run Code Online (Sandbox Code Playgroud)