【问题标题】:UI5 - get the count for each list item in the master sectionUI5 - 获取主部分中每个列表项的计数
【发布时间】:2016-04-27 11:33:02
【问题描述】:

我对 SAPUI5 很陌生。因此,我有一个场景:

主从页面(来自通过 SAP Web IDE 提供的主从模板,视图是 XML)显示来自我的模型(模拟数据)的项目。

主部分绑定到实体Interests。详细信息部分从实体Messages 获取其信息。我在它们之间有引用约束,它工作得很好。点击我的主部分中的项目会在详细信息部分的表格中显示相应的项目。

主章节列表:

<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>

主部分需要显示一个数值,该数值等于实体Messages中显示的项目总数(即等于详细信息部分中显示的行数)。经过一番研究(最终来自SAPUI5 - How to Bind Odata $count to a XML view),我了解了formatter 属性并添加了它。

我的格式化程序是这样写的:

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

我尝试记录 oValue,但它似乎返回为 undefined

是否有办法获取主列表中每个项目的计数。

【问题讨论】:

    标签: data-binding sapui5


    【解决方案1】:

    对于Master的实体InterestsSet,假设Key属性为InterestID

    所以在你的代码中你有这个:

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

    使用这个:

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

    现在,控制器中的函数 .productCount 应该是这样的:

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

    或者这个:

    productCount: function(InterestID){
        uri = "/InterestSet(InterestID=' + InterestID + ')/YourNameNavigationsToMessages/$count";
        return this.getView().getModel().read(uri);
    }
    

    【讨论】:

    • 感谢您的指点。我更正了对“InterestsID”的引用。使用第一个提到的函数:productCount: function(InterestID){ return this.getView().byId("lineItemsList").getItems().length;} 我得到一个错误“Fake XHR onreadystatechange handler throw exception: Cannot read property 'getItems' of undefined”。我尝试记录'this',它返回为'EventProvider com.otmm.controller.Master'。
    • 使用第二个函数productCount: function(InterestID){var uri = "/InterestsSet(InterestsID='"+InterestID +"')/Messages/$count"; return this.getView().getModel().read(uri);} 对每个项目进行调用,但每个都返回一个 404。被调用的示例 URI:localhost:50481/here/goes/your/serviceurl/…。我认为'/Messages/'(即YourNameNavigationsToMessages)可能是问题所在。
    • 我也尝试过productCount: function(InterestID){ return sap.ui.getCore().byId("lineItemsList").getItems().length;},它返回相同的“Fake XHR onreadystatechange handler throw exception: Cannot read property 'getItems' of undefined”错误。 'lineItemsList' 是详细信息部分中列表的 id。
    • 兴趣实体和消息实体之间是否有导航/关联?是什么名字?您应该在“YourNameNavigationsToMessages”中使用该名称。例如,如果您调用 (...)/InterestSet(InterestID='123'),其中 (..) 是 oData 服务的 URI,“123”是随机 ID(但您应该使用有效 ID ),你得到数据了吗?如果是,我猜“/InterestSet(InterestID=' + InterestID + ')/YourNameNavigationsToMessages/$count” 应该可以工作...
    • 我试过了,没有数据返回。我使用“InterestsMessages”作为名称。我想这可能行不通,因为我使用的是模拟数据(也根据stackoverflow.com/questions/23816274/… 的答案:"...我认为目前不可能 - $count 是 OData 查询选项,ODataListBinding 中的等价物是长度,例如 Products.length 我想不出一种绑定它的方法...")。这里也没有类似查询的答案:scn.sap.com/thread/3876965.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多