【问题标题】:SAPUI5: Not able to access items(or Elements) from Array(getting from List)SAPUI5:无法从数组访问项目(或元素)(从列表中获取)
【发布时间】:2017-04-18 21:25:47
【问题描述】:

此代码来自 Controller。这里我试图从 List(XML View) 中获取所有项目

onAfterRendering: function(oEvent) {

            var items = this.getView().byId("list").getItems("listitems");

            console.dir(items);




        }

此代码来自 XMLview,其中使用 Odata 填充列表

<List id="list"
            items="{ path: '/MYPROJECTSSet', sorter: { path: 'MpId', descending: false },groupHeaderFactory: '.createGroupHeader', parameters: { operationMode: 'Client' } }"
            busyIndicatorDelay="{masterView>/delay}" noDataText="{masterView>/noDataText}"
            mode="{= ${device>/system/phone} ? 'None' : 'SingleSelectMaster'}" growing="true" growingScrollToLoad="true"
            updateFinished="onUpdateFinished" selectionChange="onSelectionChange" >
            <infoToolbar>
                <Toolbar active="true" id="filterBar" visible="{masterView>/isFilterBarVisible}" press="onOpenViewSettings">
                    <Title id="filterBarLabel" text="{masterView>/filterBarLabel}"/>
                </Toolbar>
            </infoToolbar>
            <items>
                <ObjectListItem id="listitems" type="{= ${device>/system/phone} ? 'Active' : 'Inactive'}" press="onSelectionChange" title="{MpId}" intro="{Description}">
                    <firstStatus>
                        <ObjectStatus  id="icon" icon="{ parts: [ {path: 'ActualData'}, {path: 'PlannedData'} ], formatter : '.formatter.getIcon'}"/>
                    </firstStatus>
                </ObjectListItem>

            </items>
        </List>

下面是我进入控制台的结果数组。我无法使用索引访问此数组的元素

还有其他方法可以从 List 中获取所有项目吗? 或者 访问数组元素的其他方式?

Click here to see resulted array

【问题讨论】:

    标签: javascript sapui5


    【解决方案1】:

    在您的情况下,您可以获得以下数据:

    console.log(items[0].mProperties);
    

    或:

    var aData = (oList.getItems()).map(function(oItem){           
       console.log(oItem.getBindingContext().getObject());
    });
    

    getBindingContext 方法需要一个模型名称。否则它将使用默认模型。如果使用带名称的 OData 模型,则应将其传递给 getBindingContext 方法调用。

    Here 是简单的演示,按下按钮。

    【讨论】:

    • 嘿@Jaro,感谢您在我尝试您的第一个解决方案时的回复 console.log(items[0].mProperties); 它给了我以下错误 Uncaught (承诺)TypeError:无法读取未定义的属性“mProperties”,第二个解决方案也不起作用
    • 请把这个 onAfterRendering: function(oEvent) { 改成 onInit: function(oEvent) { 并将我的台词放到这个函数中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2013-07-22
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多