简体   繁体   中英

How to filter a list via controller in sapui5

I am pretty new to SAPUI5 and have a little problem with my 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).

Here you can see a snippet with the list of my view (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):

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. I only changed the code inbetween the comments //Tryout Filter and //End Tryout

When i want to run my application, debugger says: "Cannot read property 'filter' of undefined" because oBinding is undefined. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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