视图加载后触发 BarcodeScannerButton

Fire BarcodeScannerButton after View loads

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

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

我做的第一件事就是调用按钮本身。但我的第一个问题是按钮不接受 id 作为参数。它告诉我应用程序不能接受重复的 ID。所以我所做的只是寻找按钮 ID。我能够找到它(例如 _button9),但每当我通过 sap.ui.getCore.byId() 调用它时,有时它会 returns "undefined." 这就是为什么我不能调用 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() {}

});

用于放置 fire() 方法。您是否尝试显示 pop-up 条形码 reader?类似于应用 "SD_SO_CRE" 的 pop-up(客户选择对话框在主视图之前加载)。 他们没有用 fire() 解决任务...