【问题标题】:How to filter a list via controller in sapui5如何通过 sapui5 中的控制器过滤列表
【发布时间】:2018-04-15 07:53:35
【问题描述】:

我对 SAPUI5 很陌生,我的应用程序有点问题。我设置了一个主从应用程序,它在主视图中显示客户列表。我的目标是将此列表过滤到我的 oData 服务的任何属性(我正在使用 Northwind Web 服务)。

在这里你可以看到一个带有我的视图列表(MasterView.xml)的sn-p:

<List
            id="list"
            items="{
                path: '/Customers',
                sorter: {
                    path: 'CompanyName',
                    descending: false
                },
                groupHeaderFactory: '.createGroupHeader'
            }"
            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
                    type="{= ${device>/system/phone} ? 'Active' : 'Inactive'}"
                    press="onSelectionChange"
                    title="{CompanyName}"
                    numberUnit="{CustomerID}">
                </ObjectListItem>
            </items>
        </List>

这是我在控制器 (Master.controller.js) 中所做的:

onInit : function () {
            // Control state model
            var oList = this.byId("list"),
                oViewModel = this._createViewModel(),
                // Put down master list's original value for busy indicator delay,
                // so it can be restored later on. Busy handling on the master list is
                // taken care of by the master list itself.
                iOriginalBusyDelay = oList.getBusyIndicatorDelay();
            // Tryout Filter
            var equals = FilterOperator.EQ;
            var aFilterFoo = [];
            aFilterFoo.push(new Filter("Country", equals, "Germany"));
            var oBinding = oList.getBinding("items");
            oBinding.filter(aFilterFoo); 
            // End tryout Filter

            this._oList = oList;
            // keeps the filter and search state
            this._oListFilterState = {
                aFilter : [],
                aSearch : []
            };


            this.setModel(oViewModel, "masterView");
            // Make sure, busy indication is showing immediately so there is no
            // break after the busy indication for loading the view's meta data is
            // ended (see promise 'oWhenMetadataIsLoaded' in AppController)
            oList.attachEventOnce("updateFinished", function(){
                // Restore original busy indicator delay for the list
                oViewModel.setProperty("/delay", iOriginalBusyDelay);
            });

            this.getView().addEventDelegate({
                onBeforeFirstShow: function () {
                    this.getOwnerComponent().oListSelector.setBoundMasterList(oList);
                }.bind(this)
            });

            this.getRouter().getRoute("master").attachPatternMatched(this._onMasterMatched, this);
            this.getRouter().attachBypassed(this.onBypassed, this);

        },

这一切都是由 SAP Web IDE 自动设置的。我只更改了 cmets //Tryout Filter 和 //End Tryout 之间的代码

当我想运行我的应用程序时,调试器说:“无法读取未定义的属性‘过滤器’”,因为 oBinding 未定义。 我希望你们中的任何人都可以帮助我。

非常感谢和最好的问候

【问题讨论】:

  • onInit 期间列表是否可能尚未绑定,因此oBinding 是undefined?也许您应该将代码放在onBeforeRenderingonAfterRendering 或任何路由处理程序(_onRouteMatched)中。
  • 用 onBeforeRendering 解决了 :) 非常感谢!!

标签: javascript model-view-controller controller sapui5


【解决方案1】:

通过进入 sap 生命周期方法解决了这个问题。 onBeforeRendering() 是我在 Master.Controller.js 中使用的函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多