如何从 XML 视图中的扩展集合绑定 OData $count [英] How to Bind OData $count from expanded collection in an XML view
问题描述
这可能是一个基本问题,但我无法在 XML 视图中绑定 OData 计数.
May be this is a basic question, but I have trouble binding the OData count in XML view.
在以下示例中,我想绑定来自 OData 模型的产品数量.
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
推荐答案
我认为目前不可能- $count 是一个 OData 查询选项,ODataListBinding 中的等价物是长度,例如 Products.length 我想不出绑定到它的方法
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
选项 1 - 最简单的方法,创建一个读取产品总数的列表绑定,它执行同步调用并仅返回 $count
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>
选项 2 - 使用扩展并返回非常小的数据集,在这种情况下只有 CategoryName 和 ProductID,这里的警告是您是否必须通过表分页来获取完整列表
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>
这篇关于如何从 XML 视图中的扩展集合绑定 OData $count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!