天天看點

SAP UI5應用裡搜尋功能的實作

在一個包含了list的XML視圖裡,使用SearchField标簽頁定義一個搜尋按鈕。點選之後,執行的事件處理函數為handleSearch:

<mvc:View controllerName="sapcp.cf.tutorial.app.controller.View1" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m">
    <Shell id="shell">
        <App id="app">
            <pages>
                <Page id="page" title="{i18n>title}">
                    <subHeader>
                        <Bar>
                            <contentLeft>
                                <SearchField search="handleSearch"/>
                            </contentLeft>
                        </Bar>
                    </subHeader>
                    <content>
                        <List id="List" items="{/Products}">
                            <ObjectListItem type="Navigation" press="handleListItemPress" title="{ProductName}" number="{= ((${UnitPrice} * 100) / 100).toFixed(2) }"
                                numberUnit="{i18n>currency}">
                                <attributes>
                                    <ObjectAttribute text="{QuantityPerUnit}"/>
                                </attributes>
                                <firstStatus>
                                    <ObjectStatus text="{= ${Discontinued}? 'Discontinued' : 'Available' }" state="{= ${Discontinued}? 'Error' : 'Success' }"/>
                                </firstStatus>
                            </ObjectListItem>
                        </List>
                    </content>
                </Page>
            </pages>
        </App>
    </Shell>
</mvc:View>           

在視圖的控制器裡實作這個搜尋函數:

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/m/MessageBox"
], function (Controller, MessageBox) {
    "use strict";

    return Controller.extend("sapcp.cf.tutorial.app.controller.View1", {
        onInit: function () {

        },

        // show in a pop-up which list element was pressed
        handleListItemPress: function (oEvent) {
            var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
            var selectedProductId = oEvent.getSource().getBindingContext().getProperty("ProductID");
            oRouter.navTo("Detail", {
                productId: selectedProductId
            });
        }
    });
});           

測試:

搜尋能夠按照期望的工作:

本文來自雲栖社群合作夥伴“汪子熙”,了解相關資訊可以關注微信公衆号"汪子熙"。