0

我正在使用 phonegap 中的条形码扫描仪插件扫描条形码。获得值后,我想在文本字段中显示它。以下是我的文本字段代码:

{
     xtype : 'textfield',
      id : 'barcodetextfield',
     width : 300,
     margin : '0 0 0 10',
     labelWidth : '40%',
     label : 'Enter Barcode'                                             
}

在控制器中,我试图在文本字段中设置条形码值。

window.plugins.barcodeScanner.scan(function(result) {
    this.getBarcodebox.setValue(""+result.text);
 }, function(error) {
     alert("Scanning failed: " + error);
 });

我把 ref 放在控制器中,如下所示:

barcodebox : '#barcodetextfield',

这是我的整个控制器代码:::

Ext.define('MyApp.controller.MyController', {
extend : 'Ext.app.Controller',
config : {
    refs : {
        TitlePanel : 'login',
        dashboardpanel : 'dashboard',
        ConsumerSignup : 'consumersignup',
        AddtoWishlog : 'addwishlog',
        wishlogsummary : 'wishlogsummarylist',
        FeedbackSummary : 'feedbacksummarylist',
        ConsumerSignin : 'Consumersignin',
        barcodebox : '#barcodetextfield',
        labelid : 'title'
    },
    views : [ 'TitlePanel', 'dashboardpanel', 'ConsumerSignup',
            'AddtoWishlog', 'wishlogsummary', 'FeedbackSummary',
            'ConsumerSignin' ],

    control : {
        "#LoginBtn" : {
            tap : 'onLoginButtonTap'
        },

        "#wishloghomebutton" : {
            tap : 'onWishlogHomeButtonTap'
        },

        "#feedbacksummaryhomebutton" : {
            tap : 'onFeedbackHomeButtonTap'
        },

        "#wishlogbtn" : {
            tap : 'onWishlogButtonTap'
        },

        "#Feedbackbtn" : {
            tap : 'onFeedbackButtonTap'
        },

        "#signup" : {
            tap : 'onSignupButtonTap'
        },

        "#capturebtn" : {
            tap : 'onCaptureButtonTap'
        },

        "#consumersignuphomebutton" : {
            tap : 'onConsumerSignupHomeButtonTap'
        },

        "#selectphoto" : {
            tap : 'onSelectPhotoButtonTap'
        },

        "#scanbutton" : {
            tap : 'onScanButtonTap'
        }

    }
},

slideLeftTransition : {
    type : 'slide',
    direction : 'left'
},
slideRightTransition : {
    type : 'slide',
    direction : 'right'
},

onLoginButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideLeftTransition);
    // this.getLabelid.setHtml('Dashboard');
},

onWishlogButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getWishlogsummary(),
            this.slideLeftTransition);
},

onFeedbackButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getFeedbackSummary(),
            this.slideLeftTransition);
},

onSignupButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getConsumerSignup(),
            this.slideLeftTransition);
},

onWishlogHomeButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideRightTransition);
},

onFeedbackHomeButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideRightTransition);
},

onConsumerSignupHomeButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideRightTransition);
},

onCaptureButtonTap : function(button, e, options) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality : 100,
        destinationType : Camera.DestinationType.FILE_URI
    });

    function onPhotoURISuccess(imageURI) {
        // console.log(imageURI);
        var largeImage = document.getElementById('capturedimage');
        largeImage.style.display = 'block';
        largeImage.src = imageURI;
    }

    function getPhoto(source) {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, {
            quality : 100,
            destinationType : destinationType.FILE_URI,
            sourceType : source
        });
    }
    function onFail(message) {
        alert('It is failed: ' + message);
    }

},

onSelectPhotoButtonTap : function(button, e, options) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality : 50,
        destinationType : navigator.camera.DestinationType.FILE_URI,
        sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY
    });

    function onPhotoURISuccess(imageURI) {
        // console.log(imageURI);
        var largeImage = document.getElementById('capturedimage');
        largeImage.style.display = 'block';
        largeImage.src = imageURI;
    }

    function getPhoto(source) {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, {
            quality : 100,
            destinationType : destinationType.FILE_URI,
            sourceType : source
        });
    }
    function onFail(message) {
        alert('It is failed: ' + message);
    }
},

onScanButtonTap : function(button, e, options) {
    window.plugins.barcodeScanner.scan(function(result) {
        this.getBarcodebox().setValue(""+result.text);

// alert("我们得到了一个条形码\n" + // "结果:" + result.text); }, function(error) { alert("扫描失败:" + error); }); } });

当我在对话框中显示值时,它显示正确。但无法在文本框中设置。我如何设置条形码?请帮忙。

4

2 回答 2

2

应该:

    this.getBarcodebox.setValue(""+result.text);

不是方法调用吗?

    this.getBarcodebox().setValue(""+result.text);

这当然是假设您在控制器中添加barcodebox了 a :ref

    refs: {
        barcodebox: '#barcodtext'
    }

注意id: #barcodtext(缺少'e')中的拼写错误,并确保它等于ref选择器指定的值。

- 编辑

看起来这this超出了您的回调范围,因此不引用您的控制器。您可以尝试以下方法:

window.plugins.barcodeScanner.scan(function(result) {
    var controller = Ext.app.Application.getController('MyController');
    controller.getBarcodebox.setValue(""+result.text);
}, function(error) {
    alert("Scanning failed: " + error);
});
于 2012-07-25T16:09:40.903 回答
1

而不是barcodebox : '#barcodetextfield',refs尝试中使用getCmp()

var bc = Ext.getCmp('barcodetextfield');

在那之后bc.setvalue(''+result.text);

我希望这有帮助..

于 2012-07-26T08:12:46.297 回答