聚合绑定仅显示最后一项

时间:2015-11-24 07:49:46

标签: sapui5

您好我正在尝试将聚合绑定到Table控件上,但结果行会重复显示数据集中的最后一个结果。详细说明,该表包含正确的行数和列数,但每行重复相同的数据。

执行OData读取调用并将结果放入JSON模型并将该模型绑定到表上,但这看起来不必要地昂贵。

绑定部分

var filter = new Filter("itemID", sap.ui.model.FilterOperator.EQ, this.item.itemID);  
this._template = this._template ? this._template : sap.ui.xmlfragment("InventoryListItem", this.getView().getController());
this.inventoryList.bindAggregation("items", {
    path: "oDataModel>/InventoryUsages",
    filters: [filter],
    template: this._template,
    parameters: {
      select: 'inventoryID,memberID,fName,lName,condition,purchasedAt,price'
   }
});

模板:

<core:FragmentDefinition
xmlns="sap.m"
xmlns:core="sap.ui.core"
xmlns:l="sap.ui.layout">
<ColumnListItem>
    <cells>
        <Text text="{path: 'oDataModel>inventoryID'}" />
        <Text text="{parts:[{path:'oDataModel>fName'}, {path:'oDataModel>lName'}], formatter:'.nameFormater'} " />
        <Text text="{oDataModel>purchasedAt}" />
        <RatingIndicator maxValue="5" class="sapUiSmallMarginBottom" value="{
            path: 'oDataModel>condition', formatter: '.conditionFormat'}"
            change="onRatingPress"/>
        <Text text="{path:'oDataModel>price', formatter: '.priceFormatter'}" />
        <Button text="Remove"
            icon="sap-icon://delete"
            press="onRowDelete"
            class="appBtn"/>
    </cells>
</ColumnListItem>

该表显示正确的行数和列数,但数据错误。它将显示每行的最后一个结果。当表增长时,表将显示集合中的下一个最后结果。有谁知道我怎么解决这个问题?

答案:事实证明我正在对没有主键的视图进行OData调用。渲染出现故障,因此仅出于某种原因显示最后的结果。

3 个答案:

答案 0 :(得分:4)

好吧,我找到了我自己的问题的答案,结果是因为我正在对没有主键的视图进行OData调用。如果它没有主键,它将会出现故障并显示数据集的最后结果。 (如果您使用的是ASP.net web api,您只需将[key]注释放在模型中即可解决此问题)。

答案 1 :(得分:0)

我怀疑实例化一个片段(!)作为模板可能会破坏这里;因为它被实例化一次,所以你继续使用对相同控件的引用,这将最终显示模型中的最后一项。

更好地使用函数并返回ColumnListItem控件的新实例

修改

我在下面提供了一个工作示例(点击“运行代码段”以查看它是否正常工作)。

因为使用片段作为聚合模板似乎有点偏离我的口味,这就是我将如何进行绑定。

有些观点:

  1. 表格的ColumnsColumnListItems直接在视图中定义为聚合。不需要单独的模板,片段或工厂函数,一切都在视图中(它应该在哪里;))
  2. 过滤器的值介于1到10之间,适用于Input字段的更改。
  3. 过滤器本身只是一个简单的功能,然后在必要时重新应用
  4. 希望这有帮助!

    sap.ui.controller("view.initial", {
        
        onInit: function() {
            var model = new sap.ui.model.json.JSONModel();
            model.setData({
                filterValue : 4,
                someArray : [{"itemID":4,"inventoryID":323,"memberID":440,"fName":"Victoria","lName":"Hutchings","condition":4,"purchasedAt":"1992-01-18T21:53:09.291Z","price":96},{"itemID":1,"inventoryID":83,"memberID":83,"fName":"Connie","lName":"Monuteaux","condition":4,"purchasedAt":"2001-07-28T01:05:35.935Z","price":99},{"itemID":3,"inventoryID":591,"memberID":399,"fName":"Cecelia","lName":"Peschke","condition":1,"purchasedAt":"2063-07-03T22:41:15.159Z","price":17},{"itemID":8,"inventoryID":523,"memberID":417,"fName":"Ginger","lName":"Vasquez","condition":5,"purchasedAt":"1967-02-27T09:24:18.631Z","price":67},{"itemID":6,"inventoryID":310,"memberID":573,"fName":"George","lName":"Dixon","condition":4,"purchasedAt":"2050-05-04T12:46:00.850Z","price":6},{"itemID":2,"inventoryID":749,"memberID":887,"fName":"Subha","lName":"Gunn","condition":3,"purchasedAt":"1952-04-08T01:51:08.544Z","price":40},{"itemID":7,"inventoryID":46,"memberID":546,"fName":"Delena","lName":"Ramirez","condition":4,"purchasedAt":"2051-04-05T07:03:10.635Z","price":62},{"itemID":0,"inventoryID":734,"memberID":775,"fName":"Bola","lName":"Shah","condition":4,"purchasedAt":"1986-09-17T23:29:15.318Z","price":97},{"itemID":8,"inventoryID":550,"memberID":546,"fName":"Kassaundra","lName":"Cowen","condition":4,"purchasedAt":"1954-11-25T09:37:03.604Z","price":83},{"itemID":9,"inventoryID":469,"memberID":692,"fName":"April","lName":"Garcia","condition":2,"purchasedAt":"1909-07-07T21:40:21.919Z","price":1},{"itemID":0,"inventoryID":777,"memberID":88,"fName":"Suzanne","lName":"Foxen","condition":5,"purchasedAt":"2026-04-04T13:18:20.715Z","price":21},{"itemID":5,"inventoryID":401,"memberID":956,"fName":"Angelo","lName":"Spellicy","condition":2,"purchasedAt":"2044-10-13T19:17:29.321Z","price":51},{"itemID":10,"inventoryID":799,"memberID":216,"fName":"Sonny","lName":"Gergely","condition":3,"purchasedAt":"1947-01-07T01:12:29.721Z","price":33},{"itemID":4,"inventoryID":175,"memberID":552,"fName":"Sara","lName":"Trautman","condition":0,"purchasedAt":"1992-08-17T22:24:36.123Z","price":52},{"itemID":6,"inventoryID":488,"memberID":580,"fName":"Penny","lName":"Bettencourt","condition":4,"purchasedAt":"1915-04-25T05:17:05.226Z","price":52},{"itemID":7,"inventoryID":713,"memberID":812,"fName":"Greg","lName":"Mulvehill","condition":0,"purchasedAt":"1970-05-03T22:47:42.502Z","price":50},{"itemID":1,"inventoryID":978,"memberID":401,"fName":"Kathe","lName":"Krugel","condition":0,"purchasedAt":"1942-02-09T21:40:24.584Z","price":96},{"itemID":2,"inventoryID":686,"memberID":216,"fName":"Jody","lName":"Gage","condition":5,"purchasedAt":"2010-11-25T16:43:41.214Z","price":86},{"itemID":4,"inventoryID":686,"memberID":110,"fName":"Shelly","lName":"Knoepfel","condition":1,"purchasedAt":"1989-06-29T04:58:29.195Z","price":37},{"itemID":4,"inventoryID":977,"memberID":350,"fName":"LaDagea","lName":"Doerfler","condition":1,"purchasedAt":"1959-03-18T15:10:34.816Z","price":2}]
            });
            sap.ui.getCore().setModel(model);
        },
    
        onAfterRendering: function() {
            this.applyFilter();
        },
    
        applyFilter: function() {
            var allFilter=[];
            allFilter.push(new sap.ui.model.Filter("itemID", sap.ui.model.FilterOperator.EQ, this.getView().getModel().getProperty("/filterValue")));
    
            var oTable = this.getView().byId("myTable");
            var oBinding = oTable.getBinding("items");
            oBinding.filter(allFilter, sap.ui.model.FilterType.Application);
        }
    });
    
    var oView = sap.ui.xmlview({
      viewContent: jQuery("#view1").html()
    })
    
    oView.placeAt("uiArea");
    <script id="sap-ui-bootstrap"
        src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
        data-sap-ui-theme="sap_bluecrystal"
        data-sap-ui-xx-bindingSyntax="complex"
        data-sap-ui-libs="sap.m"></script>
    
    <div id="uiArea"></div>
    
    <script id="view1" type="ui5/xmlview">
        <core:View
          xmlns:core="sap.ui.core"
          xmlns:mvc="sap.ui.core.mvc"
          xmlns="sap.m"
          controllerName="view.initial"
          xmlns:html="http://www.w3.org/1999/xhtml">
            <Input value="{/filterValue}" change="applyFilter" />
            <Table id="myTable" items="{/someArray}">
                <columns>
                    <Column>
                        <Label text="inventoryID" />
                    </Column>
                    <Column>
                        <Label text="memberID" />
                    </Column>
                    <Column>
                        <Label text="fName" />
                    </Column>
                    <Column>
                        <Label text="lName" />
                    </Column>
                    <Column>
                        <Label text="condition" />
                    </Column>
                    <Column>
                        <Label text="purchasedAt" />
                    </Column>
                    <Column>
                        <Label text="price" />
                    </Column>
                </columns>
                <items> 
                    <ColumnListItem>
                        <cells>
                            <Text text="{inventoryID}" />
                            <Text text="{memberID}" />
                            <Text text="{fName}" />
                            <Text text="{lName}" />
                            <RatingIndicator maxValue="5" value="{condition}" />
                            <Text text="{purchasedAt}" />
                            <Text text="{price}" />
                        </cells>
                    </ColumnListItem>
                </items>
            </Table>
        </core:View>
    </script>

答案 2 :(得分:0)

与客户端模型相反,ODataModel将条目存储为 path-context 对(路径为"/<EntitySet-name>(<KeyPredicate>)")的 map

  

ODataModel

mContexts
  /Customers('ALFKI'): context 
  /Customers('BLAUS'): context 
  /Customers('FRANK'): context 
  ...

在实现OData服务(或建模实体数据关系)时,请确保在EntitySet中:

  • 所有实体均已分配主键
  • 所有实体都是唯一可识别的(没有重复的键谓词)。

否则,新加载的具有不同内容但具有相同键谓词的条目将替换先前的条目,这解释了UI5为什么“仅显示最后一项”:主键缺失或它们都相同(无法唯一标识)。