Sapui5 如何在XML视图中从扩展集合绑定OData$count

Sapui5 如何在XML视图中从扩展集合绑定OData$count,sapui5,Sapui5,这可能是一个基本问题,但在XML视图中绑定OData计数时遇到了问题 在下面的示例中,我想绑定OData模型中的产品数量 每个类别都需要显示相应类别中的产品数量,如中所示 /Categories(1)/Products/$count /Categories(2)/Products/$count 我认为目前不可能 -$count是一个OData查询选项,ODataListBinding中的等效项是length,例如Products.length我想不出绑定它的方法 使用格式化程序可以通过几种

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

在下面的示例中,我想绑定OData模型中的产品数量


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

/Categories(1)/Products/$count
/Categories(2)/Products/$count

我认为目前不可能 -$count是一个OData查询选项,ODataListBinding中的等效项是length,例如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>
函数productCount(oValue){
//返回链接到Category//sync call以获取$count的产品数
if(椭圆形){
var sPath=this.getBindingContext().getPath()+'/Products';
var oBindings=this.getModel().bindList(sPath);
返回oBindings.getLength();
}
};

嗯。。我有完全相同的要求,不想从@jasper执行聪明的解决方案,因为它将从oData服务加载所有产品集合

这就是我解决问题的方法:

看法
  • 使用控制器
  • 给你的名单一个ID
  • 对列表的事件使用函数
  • 
    
    控制器
  • 实现
    countProducts
    功能
  • 使用jQuery为每个列表项请求$count—请注意URL是如何将模型的服务URL与项的绑定上下文连接起来生成的
  • 由于jQuery使用异步请求,当您得到第一个响应时,您的
    for
    将完成。因此,它可以用来避免用AJAX响应填充最后一个列表项
  • countProducts:函数(e){
    var m=sap.ui.getCore().getModel();
    var items=this.byId(“list”).getItems();
    对于(var item_index=0;item_index
    我也有类似的问题。虽然我对我的解决方案并不满意,但它使用表达式绑定,无需单独的格式化程序即可工作:

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

    像@Jasper_07一样,您仍然需要在扩展中包含
    产品,但是您忽略了返回的大部分数据。

    我使用Manifest.json、Component.js和Controller.js解决了另一个类似问题

    首先,我在App.view.xml中定义了Id,例如:

    <Title id="titleId" text="" level="H2"/>
    
    接下来,在init:function()的component.js中,我将:

    此代码读取Manifest.json并获取名为AXXX的oDataService的Url

    最后,我在App Controller中创建了一个函数,例如:

    countCustomersInAXXX : function (oEvent) {
    
        var suffix = 'Customers/$count';
        var oDataServiceUrl = localStorage.getItem('oDataServiceUrl');
        var oDataServiceUri = oDataServiceUrl.concat(suffix);
        console.log('App.controller.js: oDataServiceUri', oDataServiceUri);
    
        var count = $.ajax({type: "GET", url: oDataServiceUri, async: false}).responseText;
        console.log('App.controller.js: countCustomersInAXXX:' , count);
    
        this.getView().byId("titleId").setText(count);
    }
    
    此代码获取客户数量并在titleId中设置值

    要启动此过程,您可以使用一个按钮或一个事件,在本例中,我使用此表属性:

    updateFinished="countCustomersInAXXX"
    
    var oDataServiceUrl = this.getMetadata().getManifestEntry("sap.app").dataSources["AXXX"].uri;
    console.log("oDataServiceUrl = ", oDataServiceUrl);
    localStorage.setItem('oDataServiceUrl', oDataServiceUrl); 
    
    countCustomersInAXXX : function (oEvent) {
    
        var suffix = 'Customers/$count';
        var oDataServiceUrl = localStorage.getItem('oDataServiceUrl');
        var oDataServiceUri = oDataServiceUrl.concat(suffix);
        console.log('App.controller.js: oDataServiceUri', oDataServiceUri);
    
        var count = $.ajax({type: "GET", url: oDataServiceUri, async: false}).responseText;
        console.log('App.controller.js: countCustomersInAXXX:' , count);
    
        this.getView().byId("titleId").setText(count);
    }
    
    updateFinished="countCustomersInAXXX"