简体   繁体   English

如何通过sapui5中的控制器过滤列表

[英]How to filter a list via controller in sapui5

I am pretty new to SAPUI5 and have a little problem with my App. 我是SAPUI5的新手,我的App有点问题。 I set up a Master-Detail Application which shows a list of Customers in the Master View. 我设置了一个主从应用程序,该应用程序在主视图中显示了一个客户列表。 My goal is to filter this list to any property of my oData Service (I am using the Northwind Webservice). 我的目标是将该列表筛选为oData服务的任何属性(我正在使用Northwind Web服务)。

Here you can see a snippet with the list of my view (MasterView.xml): 在这里,您可以看到带有我的视图列表的代码段(MasterView.xml):

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

And here is what I have done in my controller (Master.controller.js): 这是我在控制器(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);

        },

This was all set up automatically by SAP Web IDE. 所有这些都是由SAP Web IDE自动设置的。 I only changed the code inbetween the comments //Tryout Filter and //End Tryout 我只在注释// Tryout Filter和// End Tryout之间更改了代码

When i want to run my application, debugger says: "Cannot read property 'filter' of undefined" because oBinding is undefined. 当我想运行我的应用程序时,调试器会说:“无法读取未定义的属性'filter'”,因为oBinding是未定义的。 I hope any of you can help me. 希望大家能帮助我。

Thanks a lot and best regards 非常感谢和最诚挚的问候

Solved this problem by getting into the sap lifecycle methods. 通过使用树液生命周期方法解决了此问题。 onBeforeRendering() was the function I used in my Master.Controller.js onBeforeRendering()是我在Master.Controller.js中使用的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM