在View加载后触发BarcodeScannerButton

时间:2016-01-11 11:17:40

标签: sapui5 sap-fiori

我在我的Fiori / UI5应用程序上实现了一个扫描仪按钮。我使用sap.ndc.BarcodeScannerButton并在控制器上创建了该按钮(我似乎无法在我的view.xml上创建按钮)。

无论如何,我需要在视图加载后触发此按钮。我有一个主 - 细节应用程序。扫描仪按钮位于主视图上。

我做的第一件事就是按下按钮本身。但我的第一个问题是该按钮不接受id作为参数。它告诉我应用程序不能接受重复的ID。所以我所做的只是寻找按钮ID。我能够找到它(例如_button9),但每当我通过sap.ui.getCore.byId()调用它时,有时会返回"未定义。"这就是我无法拨打firePress();

的原因

我遇到的另一个问题是放置此firePress()方法的位置。我试图将它放在方法onAfterRendering()上,假设由于未定义的按钮,我无法调用方法firePress()。我已尝试将其放在其他方法上,例如在使用方法attachRequestCompleted成功调用数据之后。没运气。

以下是代码

/*
 * Copyright (C) 2009-2014 SAP SE or an SAP affiliate company. All rights reserved
 */
jQuery.sap.require("sap.ca.scfld.md.controller.ScfldMasterController");
jQuery.sap.require("ui.s2p.srm.sc.create.util.Formatter");
jQuery.sap.require("sap.ndc.BarcodeScannerButton");

var counter = 0;

sap.ui.controller("ui.s2p.srm.sc.create.SRM_SC_CREExtension.view.S2Custom", {
    onInit: function() {
        sap.ca.scfld.md.controller.ScfldMasterController.prototype.onInit.call(this);
        this.oBundle = this.oApplicationFacade.getResourceBundle();
        this.isRoot = true;
        this.oRouter.attachRouteMatched(function(e) {
            if (e.getParameter("name") === "master" && !this.isRoot && Object.keys(e.getParameter("arguments")).length === 0) {
                var d = sap.ui.core.routing.History.getInstance().getDirection("shoppingCartCheckout/" + this.tempCartId);
                if (d === "Unknown") {
                    this.isRoot = true;
                    this._oControlStore.oMasterSearchField.clear()
                } else {
                    if (this.getList() !== null) {
                        var i = this.getList().getSelectedItem();
                        if (i !== null) {
                            //alert("setListGo");
                            this.setListItem(i);
                        }
                    }
                }
            }
            this.isRoot = (this.isRoot) ? false : this.isRoot;

        }, this);

        //  alert(sap.ui.getCore().byId("productScanButton"));
        this.onBarcodeScanning();
        this.setEmptyCart(true);
        this.showAllProducts(); //added by salduam to show all products
    },

    backToList: function() {
        //alert("back");
    },

    getDefaultUserSettings: function(r) {
        var o = function(D, R) {
            this.tempCartId = D.results[0].TEMP_CART_ID;
            if (!jQuery.device.is.phone) {
                if (r) {
                    this.oRouter.navTo("noData", {
                        viewTitle: "DETAIL_TITLE",
                        languageKey: "NO_ITEMS_AVAILABLE"
                    }, true)
                } else {
                    this.navToEmptyView()
                }
            }
        };
        var d = this.oApplicationFacade.getODataModel("getdefusrset");
        d.read("DefaultUserSettings?ts=" + Date.now(), null, null, true, jQuery.proxy(o, this), jQuery.proxy(this.onRequestFailed, this))
    },

    applySearchPatternToListItem: function(i, f) {

        if (f.substring(0, 1) === "#") {
            var t = f.substr(1);
            var d = i.getBindingContext().getProperty("Name").toLowerCase();
            return d.indexOf(t) === 0
        } else {
            return sap.ca.scfld.md.controller.ScfldMasterController.prototype.applySearchPatternToListItem.call(null, i, f)
        }
    },

    getHeaderFooterOptions: function() {
        var o = {
            sI18NMasterTitle: "MASTER_TITLE",
            buttonList: []
        };
        return o
    },

    isBackendSearch: function() {
        return true
    },

    //call startReadListData with parameter wildcard
    showAllProducts: function(e) {
        var startSearchText = "*";
        this.startReadListData(startSearchText);
        //alert("called");

    },

    applyBackendSearchPattern: function(f, b) {
        //added by salduam
        //if search field is blank, automatically call showAllProducts
        if (f == "") {
            this.showAllProducts()
        };

        if (f != "" && f != null) {
            this.startReadListData(f)
        } else {
            this.setEmptyCart(false)
        }
    },

    startReadListData: function(f) {

        var o = function(D, r) {
            var m = new sap.ui.model.json.JSONModel(D.results);
            this.getView().setModel(m);
            this.getList().destroyItems();
            this.getList().bindAggregation("items", {
                path: "/",
                template: this.oTemplate.clone(),
                filter: [],
                sorter: null
            });
            this.registerMasterListBind(this.getList());
        };
        var e = encodeURIComponent(f);
        //console.log("EEEE-----"+ e);
        var d = this.oApplicationFacade.getODataModel();
        //console.log(d);
        d.read("CATALOG_ITEM?$filter=startswith(description,'" + e + "')&$top=20", null, null, true, jQuery.proxy(o, this), jQuery.proxy(this.onRequestFailed,
            this));
    },

    setListItem: function(i) {
        //  alert("onClick");
        var b = i.getBindingContext();

        var m = b.oModel.oData[parseInt(b.sPath.split('/')[1])];
        this.oRouter.navTo("detail", {
            tempCartId: this.tempCartId,
            contextPath: b.getPath().substr(1)
        }, true);
        var c = sap.ui.core.Component.getOwnerIdFor(this.oView);
        var C = sap.ui.component(c);
        C.oEventBus.publish("ui.s2p.srm.sc.create", "refreshDetail", {
            data: m
        });

    },

    setEmptyCart: function(r) {

        var e = new sap.ui.model.json.JSONModel({
            results: []
        });
        this.oRouter.navTo("noData", {
            viewTitle: "DETAIL_TITLE",
            languageKey: "NO_ITEMS_AVAILABLE"
        }, true);
        this.getView().setModel(e);
        this.oTemplate = new sap.m.ObjectListItem({
            type: "{device>/listItemType}",
            title: "{matnr}",
            press: jQuery.proxy(this._handleItemPress, this),
            number: "{parts:[{path:'itm_price'},{path:'itm_currency'}],formatter:'ui.s2p.srm.sc.create.util.Formatter.formatPrice'}",
            numberUnit: "{itm_currency}",
            attributes: [new sap.m.ObjectAttribute({
                text: "{description}"
            })],
        });
        this.getList().bindAggregation("items", {
            path: "/results",
            template: this.oTemplate,
            filter: [],
            sorter: null,
        });
        this.registerMasterListBind(this.getList());
        this.getDefaultUserSettings(r)
    },

    onRequestFailed: function(e) {
        jQuery.sap.require("sap.ca.ui.message.message");
        sap.ca.ui.message.showMessageBox({
            type: sap.ca.ui.message.Type.ERROR,
            message: e.message,
            details: e.response.body
        })
    },

    onExit: function() {},

    onBarcodeScanning: function(oEvent) {
        var productScanButton = new sap.ndc.BarcodeScannerButton({
            provideFallback: "{/btnFallback}",
            width: "100%",
            scanSuccess: function(oEvent) {
                var barcodeID = oEvent.getParameter("text");
                sap.m.MessageToast.show(barcodeID);
                var searchField = sap.ui.getCore().byId("__field3");
                searchField.setValue(barcodeID);

                searchField.fireSearch();
            }
        });
        this.getView().byId("barCodeVBox").addItem(productScanButton);
    },

    onAfterRendering: function(oEvent) {},

    onBeforeRendering: function() {}

});

1 个答案:

答案 0 :(得分:0)

用于放置fire()方法。您是否正在尝试显示弹出式条形码阅读器?类似于应用程序“SD_SO_CRE”的弹出窗口(其中客户选择对话框在主视图之前加载)。 他们没有用fire()来解决任务......